Sun At Home Notes: Determining Your Remote IP Address

If you have set up your Sun workstation to be able to be telnet'ed to from the internet, there is one more piece of information you need. The IP address to use to get to the machine. If you have set up port forwarding on your router/firewall properly, this is the IP address assigned to you by your ISP. If you have a fixed IP address, this is not a problem, you just have to get and remember the number. If your ISP changes your IP number every time you power up or at periodic intervals, this can be a problem. The following technique (examples follow) can be used to get around this problem.

Diagram Showing Finding Remote IP Addr

On your Sun, you will want to run a cron job which checks for your external IP address and makes it available in some way. Assuming you are behind your firewall and are using Network Address Translation (NAT), this is not as straightforward as you would like. The IP address you see, is your internal address on the 192.168 network. Your external IP address is something else. You have to get something program which views you from the outside to tell you your IP address. One way of doing this is with a .shtmlweb page. The SHTML language supplies the web page with environment variables, one of which is the remote clients IP address. Most ISP's provide their customers with web page space and most also support SHTML.

What your cron job is going to do is read the SHTML web page, decide if the IP address has changed and FTP the web page, with the IP address expanded, back to the web site under a different name. You can then query that web page from any browser and see your IP address.

Example

The SHTML Page

The following web page will display the IP address of the person viewing the page.


<html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Get External IP Address</title>
</head>
<body>
<!--#echo var="REMOTE_ADDR" -->
</body></html>

The Shell Script to Run on Your Sun at Home

The following shell script will copy the SHTML page to your /tmp directory then copy the



cd /tmp
rm -f /tmp/CURRENT_ADDR.html /tmp/CURRENT_ADDR_old.html
/usr/bin/wget --cache=off -o/dev/null -O- http://www.swlink.net/~styma/REMOTE_ADDR.shtml > /tmp/CURRENT_ADDR.html 
if [ $? != 0 ]
then
   exit 1
fi
NEW_IP=`/bin/grep -v '^<' CURRENT_ADDR.html | /bin/sed  -e 's/\r//'`


OLD_IP=`/usr/bin/wget --cache=off -O- http://www.swlink.net/~styma/CURRENT_ADDR.html  | /bin/grep -v '^<' | /bin/sed  -e 's/\r//'` 


# If the IP addresses do not match, update the web site file CURRENT_ADDR.html
if [ "X$NEW_IP" != "X$OLD_IP" -a "X$NEW_IP" != "X"  -a "X$OLD_IP" != "X" ]
then
ftp -n youur.isp.web.site << !!!!
user yourid  yourpassword
cd public_html
put CURRENT_ADDR.html
quit
!!!!
fi

The program wget can be downloaded from a number of sources on the internet

On The Other End

Once the above code is up and running, you can get your IP address from any web browser if you know the URL to your CURRENT_ADDR.html page. You can get fancier if you have update authority for /etc/hosts or C:\windows\hosts on the machine you use at work. You can run a shell script from a cron job to update /etc/hosts with the data from the CURRENT_ADDR.html page. The following shell script will update /etc/hosts. It is assuming you refer to your Sun At Home as mysun in /etc/hosts.



#!/usr/bin/ksh -p
#  This shell is run from a root cron job to update /etc/hosts
#  with the current IP address of node mysun which is a my
#  personal Sun Solaris node.
#
#set -xv
# Get the ip address of the machine from the web page in my
# personal web area on my ISP.  This file is generated by a cron
# job running on mysun.
#
/usr/bin/wget --cache=off -o/dev/null  http://www.swlink.net/~styma/CURRENT_ADDR.html 
if [ $? != 0 ]
then
   exit 1
fi
NEW_IP=`/bin/grep -v '^<' /tmp/CURRENT_ADDR.html  | /bin/sed  -e 's/\r//'`

# Pull the IP address from mysun from /etc/hosts
OLD_IP=`/usr/bin/cat /etc/hosts | /usr/bin/fgrep 'mysun' | /usr/bin/sed -e 's/^  *//' -e 's/[	 ].*//'`

# If the old IP address was extracted, then check it.
if [ $? = 0 ]
then
   # If the IP addresses do not match, update /etc/hosts 
   if [ $NEW_IP != $OLD_IP ]
   then
      /usr/bin/rm -fr /tmp/hosts
      /usr/bin/sed -e "s/$OLD_IP/$NEW_IP/"  /etc/hosts > /tmp/hosts
      if [ $? = 0 ]
      then
         /usr/bin/rm /etc/hosts.old
         /usr/bin/mv /etc/hosts /etc/hosts.old
         if [ $? = 0 ]
         then
            /usr/bin/mv /tmp/hosts /etc/hosts
            if [ $? != 0 ]
            then
               /usr/bin/mv /etc/hosts.old /etc/hosts
            fi
         fi
      fi
   fi
fi

In both the above shell scripts, you may need to replace \r with a real x'0d'.

Other Options

You could do this in Perl. I will do this later, or you can send me the script and I will add it to the web page.



Back to Sun At Home Home Page
Last Maintained, 05/02/2006 by R. E. Styma