jump to content

S Babu
2001/04/07

I've a home network with a Linux server acting as a gateway to internet via a 56K modem. It runs Samba, DHCP service and SSH. Often I work at home and will need to get information from home network, when I am in the office. This explains how I do that - might be useful for you.

Getting the IP address of your home PC

This has three parts. First you need to get the IP address your ISP assigns you after you dial up. Then that IP address needs to be announced to the machines you want to connect from. This can be done in several ways. You can send an email with the IP address or you can post the IP address to a public server. I host my site on a public server, so I chose to post it there. Whenever you want to connect to your machine at home, you get the IP address and use it!

Getting the IP address

Quite simple! I have a script which dials my modem. A simple shell cut-grep will get the IP address:

        myip=`/sbin/ifconfig|grep P-t-P|cut -f2 -d:|cut -f1 -d' '`

I've only one PPP connection, so grep P-t-P will get the line with my IP address. The two cuts extracts only the IP address.

Posting the IP address

I wrote a small PHP script because my web hosting provider supports PHP. Give it an argument called nip and it will post it in a text file. I call this text file homeipaddress.txt. Here's the code for the script 'myip.php3':

        <html>
        <head><title>Post IP</title></head>
        <body>
        <?
          $ip_file="homeipaddress.txt";
          if (file_exists($ip_file))
          {
            $fp=fopen($ip_file,"r");
            $ip=fgets($fp,20);
            fclose($fp);
          }
          echo "existing ip = $ip<br>";
          if($nip)
          {
            echo "new ip = $nip<br>";
            $fp=fopen($ip_file,"w");
            fputs($fp,$nip);
            fclose($fp);
          }
        ?>
        </body></html>

Then you create a text file in your server called homeipaddress.txt and give it permissions of 666 so that your PHP script can write to it.

Now, we need to call this script with our IP address, after we dial up. Here is my complete dial up script:

        # dial ISP
        /usr/sbin/usernetctl ifcfg-ppp0  up &
        # sleep for a minute
        sleep 60
        # get the IP address
        myip=`/sbin/ifconfig|grep P-t-P|cut -f2-d:|cut -f1-d''`
        # post the IP address
        /usr/bin/lynx 
            --source http://www.mysite.com/myfiles/myip.php3?nip=$myip
            >> /dev/null 2>>/dev/null

Connecting to the server

I use SSH to connect. At work, I use Windows 98 machines. I use PuTTY as my favorite Windows SSH client. Whenever I need to connect, I browse to http://www.mysite.com/myfiles/myip.php3 (or homeipaddress.txt) and get the IP address. The following Python script automates this:

        # getmyip.pyw
        #
        #
        # vsbabu@hotmail.com
        # 4/3/01 - gets my home PC's IP address and updates my hosts file
        # very silent operation - no output at all
        # always replaces the first line in my hosts file with the entry
        # to my home PC
        #
        import urllib
        import string

        #get my ip address
        debug=0
        myip=''
        try:
         f=urllib.urlopen('http://www.mysite.com/myfiles/homeipaddress.txt')
         myip=f.read()
         f.close()
         if debug: print 'Got IP!'
        except:
         if debug: print 'No connection to server'
         myip=''

        if (myip != ''):
         #update my hosts file
         try:
           f=open('c:\\windows\\hosts','r+')
           lines=f.readlines()
           lines[0] = myip + '\tmyhome\tmyhome.mystreet.mystate\n'
           f.seek(0)
           f.write(string.join(lines,''))
           f.close()
           if debug: print 'success'
         except:
           if debug: print 'No success'
           pass

I call it getmyip.pyw so that it won't open an ugly DOS console. Drop it in the Windows task scheduler and schedule it to run every hour or so. Create a dummy first line in C:\windows\hosts, so that the script can overwrite this.

Fire up PuTTY, create a profile to connect to myhome.mystreet.mystate and connect! If your machine is dialled up, you'll get through. This works for me because if I want it to be connected, I can call up my wife and ask her to dial up.

It would be cool if I could dial my phone and dial an additional number which will be intercepted by the modem/Linux, and then an automatic dial-up will happen after 10 seconds.

References