NSLU2-Linux
view · edit · print · history

Scroll down to 'UPDATE 07/06/2008' to see a script that will send your dynamic ip address as a html web page to a web server at a static address (of your choice).

Here's what I did. Feel free to make improvements, especially in Step 1. If there's a way to get this info from a router directly instead of indirectly via a website as described, then I'm all ears! :^) I'm also interested in a way of directly scripting a Lynx session so that a domain name service could be updated automatically when the IP changes. This would allow a slug to host a site like "www.myslug.net" or something like that. It comes down to passing in the passwords when prompted.... Any "expect" experts out there? :^)


Step 1:

Set up a page on a web hosting site that has some sort of active server side scripting technology, be it ASP, PHP, or whatever, that can read the server variable "REMOTE_ADDR" to get the IP address assigned to your gateway/router/cablemodem/modem. I used .ASP, since my web host works with that, and here it is:


ASP version:

 <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
 <head><title>IP</title></head>
 <body><% Response.Write Request.ServerVariables("REMOTE_ADDR") %></body>

PHP version (courtesy of tman):

 <html>
 <head>
 <title>IP</title>
 </head>
 <body><?php echo $_SERVER['REMOTE_ADDR']; ?></body>
 </html>

Perl version (courtesy of Roy Silvernail):

 #!/usr/bin/perl
 use Carp;
 use CGI;
 $q = new CGI;
 print "Content-type: text/plain\n\n".$ENV{'REMOTE_ADDR'}."\n";

Or you could just use the automation service at whatismyip.com, since they provide a page that does exactly this already. See http://whatismyip.com/automation.asp(approve sites)

The script portions will look different in some other language, but the HTML ought to be the same, since all you want on the page is the IP address....


Step 2:

This script generates a tiny web page with the links to your site based on your gateway's IP when passed your gateway's IP as a parameter:


 #!/bin/sh
 if [ -n "$1" ]
 then
   url=$1
 else
   url=ERROR
 fi

 if [ -n "$2" ]
 then
   port=$2
 else
   port=80
 fi

 cat > dyn-ip.html << EOF
 <html>
  <head>
   <title>Link to My uNSLUng NSLU2's Web Site</title>
  </head>
  <body bgcolor="#000033">
   <table width="420" border="2" cellpadding="3" cellspacing="0" bgcolor="#F6F6F6">
    <tr align="center">
     <td>
      <a href="http://$url:$port/">My uNSLUng NSLU2 Homepage</a>
     </td>
    </tr>
    <tr align="center">
     <td>
      <p class="quote">
       This link may be bad from time to time, but it is dynamically 
       updated on the 42nd minute of every hour, so try back then if
       it doesn't work!
      </p>
     </td>
    </tr>
   </table>
  </body>
 </html>
 EOF

Side notes about the above script:

1. It can also be used on the command line to create a small page with a link to any site you wish, simply by passing it the site's address (minus the "http://" part, of course), and with a little modification (taking out the table tags and changing the first output redirect to append), it could be used to collect a list of links....

2. If you have your web server on a port different than 80, add a second command line parameter and pass the port number in


Step 3:

Use this script (in a cron job, perhaps) to check your IP on that page you set up in Step 1 and, only if the IP address has changed, upload a web page created "on the fly" by the script in Step 2:


 #!/bin/sh

 # Change the following link to where you put the remote IP script from above.
 ip_page_url=http://www.somewhere-where-your-page-is.com/remote-ip.asp

 #
 # get the current IP of the router
 current_ip=`lynx -accept_all_cookies -dump $ip_page_url`
 #
 echo $current_ip > temp_ip
 #
 # get the old IP of the router
 old_ip=`cat router_ip`
 #
 Set_Led beep1
 sleep 1
 #
 # compare to see if update is necessary
 if [[ $current_ip != $old_ip ]]
 then
 # uncomment following line for user feedback or debugging
 #    echo "$current_ip different from $old_ip so running update script"
    Set_Led beep1
    sleep 1
    Set_Led beep1
 #
    echo $current_ip > /router_ip
 #
    echo `date` : $current_ip >> /share/hdd/data/syslog/router_ip_history
 #
 # uncomment following line for user feedback or debugging
 #   echo "wrote current ip $current_ip to router_ip"
 #
 # uncomment following line for user feedback or debugging
 #   echo "generating dyn-ip.html"
    generate-dyn_ip_html $current_ip
 #
    remote="www.somewhere-where-your-page-is.com"
    localHtmlFile="/root/dyn-ip.html"
    htmlFile="dyn-ip.shtm"
 #
 # uncomment following lines for user feedback or debugging
 #   echo "attempting to put $localHtmlFile to $remote/$htmlFile"
 #   echo "attempting to put $localHtmlFile to $remote/$targetdir/$htmlFile"
 #
    ftp $remote << EOF
 put $localHtmlFile $htmlFile
 exit
 EOF
 #
 # uncomment following line for user feedback or debugging
 #   echo "completed ftp session"
 #
 else
 # uncomment following line for user feedback or debugging
 #   echo "$current_ip same as $old_ip so exiting update script"
    Set_Led beep1
 fi

This script assumes that your files are in your /root directory. Modify the localHtmlFile value as necessary if you put them somewhere else, but make sure you keep the scripts all in the same folder. It also creates two files, "router_ip" and "temp_ip" to hold the previous IP address and the IP address you're comparing it to. If it finds that they are the same, it exits without FTPing the HTML file to your web host.

I hope this helps!


Well, I just couldn't stand this, get yourself an account at dyndns.org and run this as a cronjob (courtesy of Malfi):

 #!/bin/sh

 user=your_dyndns_username
 pass=your_dyndns_password
 host=your_dyndns_hostname

 IP=`wget -qO - http://whatsmyip.smokingmedia.com/ | \
  grep "TITLE" | \
  sed -e "s#^[^0-9]\+\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)[^0-9]\+...\$#\1#g"`
 if [ "`cat /tmp/dyndns.ip`" != "$IP" ]
 then
  wget --no-check-certificate -q -O - \
  https://$user:$pass@members.dyndns.org/nic/update?system=dyndns&hostname=$host&myip=$IP" \
  >> /tmp/dyndns.update.log 2> /dev/null
  echo -n $IP > /tmp/dyndns.ip
 fi

Malfi--

That's good, but where's the fun in using a service? :^)

It should be possible by simply getting the IP from the router/firewall, and posting the IP to the appropriate place to ensure that your domain is looking for you where you currently are....


Lol, I'd say both methods are useful in their own rights.

In the script outlined by Malfi, you'll be able to host a page from your home or wherever you have the setup. This is useful when 1) You don't want to pay someone to host your site. 2) It's ineffective to host your site elsewhere because you host big and constantly changing files, like music, movies, etc.

In the original script, you'll have to host a page somewhere, be it paid or a free service like Geocities. There, a link could point to your setup on a dynamic IP. With the script, a dynamic dns provider is cut out of the equation.

Personally, my vote goes for Malfi's solution, since you can host something with your own top level domain with a free dynamic dns server like hn.org or if you don't mind a sub-domain, many excellent dynamic dns servers like dyndns.org offer this for free (yourname.dyndns.org). If you already have a site, and for whatever reason, you want to link to your setup instead of distributing your setup's address directly to users, you may always do so with a link to "yourname.dyndns.org" or use zero-frame forwarding to hide your actual address.


I also like the dyndns solution. My router actually automatically updates my dyndns account to point to its WAN IP address so I don't even really need Malfi's script, but the real beauty comes in after that. I have purchased my own domain name from an ISP and use a free DNS management service (http://www.everydns.net/(approve sites)) to add a CNAME into my dns record so that home.mydnsname.org is aliased to mydnsname.dyndns.org. Thus I have a static and personalised DNS record which always gets me back to my home network. Works like a charm.


I was really confused by this until I realised that for HTTPS you need the 1.10.2-2 version of wget in the wget-ssl ipkg rather than 1.9.1-r1 version in ipkg wget! Now I have a Perl script to ask my router (D-Link DI-524) for it's status page, extract the current IP address, check it, and update my DynDNS if necessary. Brilliant! Steve G


and even more simple: (version with own server and no DynDNS-provider)

I just did it! You do not need a script to get your IP, just upload this to your server and name it ip.shtml

<html> <head></head><body>

<!--#echo var="REMOTE_ADDR" -->

</body> </html>

So www.yourserver.com/ip.shtml should show your IP.

Then use this script to ftp the ip to your server:

File: opt/getip.sh

 #!/bin/sh
 cd /opt
 rm ip.shtml
 wget www.xyz.ch/ip.shtml
 cat ip.shtml >nslu.html
 USER=user
 PASSWD=password
 ftp -n www.xyz.ch <<END
 user $USER $PASSWD
 cd html
 asc
 put nslu.html
 quit
 END

... replace xyz, user, password and: chmod 755 /opt/getip.sh

Now just ad a line to /etc/crontab

...
 1 * * * 0 root    /opt/getip.sh
...

And you can see your IP on www.yourserever.com/nslu.html

If you like to have a direct link to your nslu then change ip.shtml (add html redirection):

<html><head><meta http-equiv="refresh" content="0; URL=http://<!--#echo var="REMOTE_ADDR" -->"> <BODY></html>


Stein: Here is a complete script to update your www.no-ip.com account:

#!/bin/sh

 #
 # File: ~/script/checkIP/checkIP
 # This script checks if you have a new Internet IP address.
 # If so, it will update your profile at www.no.ip.org and 
 # send you a email notification.
 #

 #set -x

 MAILTO="your@address"
 WORKDIR=~/script/checkIP
 USERNAME="username"
 PASSWD="pswd"
 DOMAIN="your.no-ip.org"  

 #
 #Download the html page with the IP and save it to the file: whatismyip
 #
 wget -o $WORKDIR/wget.log -O $WORKDIR/whatismyip whatismyip.com  

 #
 #Isolate the new IP
 #
 NEWIP=$(grep '[0-9].[0-9].[0-9].[0-9]' $WORKDIR/whatismyip |grep displaycopy| cut -d"'" -f 2)
  #
  # NOTE: the "grep '[0-9].[0-9].[0-9].[0-9]" will not always filter just the IP address
  #

 #
 # Get the stored "old" IP address
 #
 OLDIP=$(cat $WORKDIR/oldip) 

 #
 # Compare the files: newip and oldip and act accordingly
 #
 if [ "$NEWIP" = "$OLDIP" ]; then
   echo "OLD IP ($OLDIP)"
 #elif [ "$NEWIP" != '[0-9].[0-9].[0-9].[0-9]' ]; then 
 #Does not seem to work :-(
   #
   # Print error and append ip to wget.log
   #
 #  echo "Error... (NewIP: $NEWIP)" |tee -a $WORKDIR/wget.log  
 #  cat $WORKDIR/wget.log |mail -s "CheckIP? ERROR" $MAILTO
 else
   echo "NEW IP! $NEWIP (Old: $OLDIP)"

   #
   # Update www.no-ip.org
   # For help take a look at: http://www.no-ip.com/integrate/
   wget -O $WORKDIR/no-ip.response.txt \
     "http://dynupdate.no-ip.com/dns?username=$USERNAME&password=$PASSWD&hostname=$DOMAIN&ip=$NEWIP" 

   #
   # Check reply from no-ip.org. "0" and "1" response is good.
   #
   cd $WORKDIR
   test "$(cat no-ip.response.txt|cut -d ":" -f 2)" != "0" && \
     test "$(cat no-ip.response.txt|cut -d ":" -f 2)" != "1" && \
     cat $WORKDIR/no-ip.response.txt |mail -s "IP-change error..." $MAILTO
   #
   # Send email notification
   #
   echo $NEWIP |mail -s "New IP" $MAILTO

   #
   # Stores the new ip
   #
   echo $NEWIP > $WORKDIR/oldip
 fi

 #
 # Clean up
 #
 rm $WORKDIR/wget.log 2> /dev/null
 rm $WORKDIR/no-ip.response.txt 2> /dev/null
 rm $WORKDIR/whatismyip 2> /dev/null

UPDATE 07/06/2008 BUG Fixes and improvements 10/12/2008 The 10/12/2008 update fixes some instances where the updates failed under unusual circumstances. SRS

Here is a script that will send your dynamic ip address as a html web page to a web server at a static address (of your choice). It is originally based on the dnsupd script at the DynDNSupdate page ( http://www.nslu2-linux.org/wiki/HowTo/DynDNSupdate ); Version: 08/03/2006 (the one just following the 'Update: 7/30/2006' entry). Follow the instructions there to create the /var/tmp, and to get the script executable and running every 15 minutes.

You do NOT need any dynamic DNS service, just a server with FTP access. You will need to install the following packages: NCFTP, NCURES, WGET

It has been functioning perfectly for me for a few months now but there is no guarantees of it suitability or correctness.

It has been tested and functions utilizing a variety of IP producing websites. It works perfectly with the "www.whatismyip.com/automation/n09230945.asp" site referenced on this wiki page. The "checkip.dyndns.com" site (as per the script as is) seems quite reliable.

I hope this will be of use to people.

SRS 07/06/2008 BUG Fixes 10/12/2008

#!/bin/ash



# Stan's UpDate IP to server remote Client for the Slug
# created by STAN, a variation of the 'DynDNSupdate' dnsupd script
# Use NO variables the same as the 'DynDNSupdate' dnsupd script --important if might use the  'DynDNSupdate' script sometime(s)
# Version: 10/12/2008 SRS

# Look for ipnerr.ip
if [ ! -f /var/tmp/ipnerr.ip ]
then
# On ipnerr.ip not found.
  # Set up ipnerr.ip for error logging.
  echo "off" > /var/tmp/ipnerr.ip
fi
# Fetch current IP Address.
# can use any ip displayer site you please, -  http://....
/opt/bin/wget --quiet http://checkip.dyndns.com/ --timeout=3 --output-document=/var/tmp/ip.html;
# wget (line above) timeout to recieve site set to 3 seconds, change if problems, originaly not in script line;not specified
# Verify the data is good.
# may need to alter or eliminate INPAT lines below if change http://.... line above
IPNPAT='Current IP Address: *[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*'
# TRY:  -         IPNPAT='[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' for line above for a different http://.... site
#NOTE: the string IPNPAT='*[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' does NOT seem to work when tried with http://ipinfo.info
# (not advised) OR maybe use: -  if [ -f /var/tmp/ip.html ]  - for line below if ELIMINATE both INPAT lines
if grep -q "$IPNPAT" /var/tmp/ip.html;
then
# On good IP data.
  # Check for IP history.
  if [ -f /var/tmp/ipnold.ip ]
  then
  # On history found.
    if [ "`cat /var/tmp/ipnerr.ip`" = "on" ]
    then
      echo "`date +%Y%m%d.%H%M%S` - not updated: Network connection restored." >> /var/log/ipnip_hist.log;
      # Turn ipnerr.ip Off
      echo "off" > /var/tmp/ipnerr.ip
    fi
    # Check for IP Change
    if [ "`cat /var/tmp/ip.html`" = "`cat /var/tmp/ipnold.ip`" ]
    then
    # On no IP change
      #

        # Check for Next day. 
        if [ ! "`cat /var/tmp/ipnday.ip`" = "`date +%x`" ]
        then
        # On day change
          # Log entry
          echo "`date +%Y%m%d.%H%M%S` - No update -" >> /var/log/ipnip_hist.log;
          echo "`date +%x`" > /var/tmp/ipnday.ip;
        fi

    else
    # On IP change
      # Update server with a new ip. 
      /opt/bin/ncftpput -u "yourusername" -p "yourpassword" "yourwebsite.com" /"optionalsubdirectory(s)"/ /var/tmp/ip.html;
      # Log the IP change and notify user.
      echo "`date +%Y%m%d.%H%M%S` - update -" >> /var/log/ipnip_hist.log;
      echo "`date +%x`" > /var/tmp/ipnday.ip;
      rm -f /var/tmp/ipnold.ip;
      cp /var/tmp/ip.html /var/tmp/ipnold.ip;
#     Set_Led beep1
#     Set_Led beep1
#     Set_Led beep1
    fi
  else
  # On No History
    # Initialize client and Update IP to server.
    # Check ipnerr.ip status
    if [ "`cat /var/tmp/ipnerr.ip`" = "on" ]
    then
    # Log Service Restoral.
      echo "`date +%Y%m%d.%H%M%S` - not updated: Network connection restored." >> /var/log/ipnip_hist.log;
    fi
    # Set ipnerr.ip off
    echo "off" > /var/tmp/ipnerr.ip
    /opt/bin/ncftpput -u "yourusername" -p "yourpassword" "yourwebsite.com" /"optionalsubdirectory(s)"/ /var/tmp/ip.html;
    echo "`date +%Y%m%d.%H%M%S` - initialization -" >> /var/log/ipnip_hist.log;
    echo "`date +%x`" > /var/tmp/ipnday.ip;
    cp /var/tmp/ip.html /var/tmp/ipnold.ip;
  fi
else
# On IP read error
  # Check ipnerr.ip status
  if [ ! "`cat /var/tmp/ipnerr.ip`" = "on" ]
  then
  # On ipnerr.ip = off - Log read error. 
    echo "`date +%Y%m%d.%H%M%S` - not updated: read error on ip fetch" >> /var/log/ipnip_hist.log;
  fi
  # Set ipnerr.ip = On to allow only one error log on next run.
  echo "on" > /var/tmp/ipnerr.ip
  # Notify user.
# Set_Led beep2
fi
# If FTP Transfer Failed at any point or on last run then try again
if [ -f /var/tmp/ipnold.ip ]
 then
 /opt/bin/wget --quiet http://yourwebsite.com/ip.html --timeout=3 --output-document=/var/tmp/ipreal.html;
  if [ ! "`cat /var/tmp/ipreal.html`" = "`cat /var/tmp/ipnold.ip`" ]
   then
   #Fix up if IP read error this run
   rm -f /var/tmp/ip.html;
   cp /var/tmp/ipnold.ip /var/tmp/ip.html;
   # On prior FTP transfer failure now try again
   /opt/bin/ncftpput -u "yourusername" -p "yourpassword" "yourwebsite.com" /"optionalsubdirectory(s)"/ /var/tmp/ip.html;
   echo "`date +%Y%m%d.%H%M%S` - FIXED Failed Prior Tranfer -->UPDATED!" >> /var/log/ipnip_hist.log;
  fi
fi 
# end
# Set the interval of the next IP check using a crontab entry.
# Once every 15 minutes is good for a home server.
# updated SRS 10/12/2008


BUG Fixes and improvements 10/12/2008 The 10/12/2008 update fixes some instances where the updates failed under unusual circumstances.

SRS 10/12/2008

view · edit · print · history · Last edited by SRS.
Based on work by SRS, Thomas Reitmayr, balleyne, Dutch, Steve G, Daniel Deremiah, Stein, Psychodad, MattMcNeill, James Fu, tman, and Malfi.
Originally by Dutch.
Page last modified on October 14, 2008, at 06:42 AM