jump to content

S Babu

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':

        <head><title>Post IP</title></head>
          if (file_exists($ip_file))
          echo "existing ip = $ip<br>";
            echo "new ip = $nip<br>";

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
            --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
         if debug: print 'Got IP!'
         if debug: print 'No connection to server'

        if (myip != ''):
         #update my hosts file
           lines[0] = myip + '\tmyhome\tmyhome.mystreet.mystate\n'
           if debug: print 'success'
           if debug: print 'No success'

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.