ALINK="#FF0000">

LINUX GAZETTE

[ Prev ] [ Table of Contents ] [ Front Page ] [ FAQ ] [ Talkback ] [ Next ]

Talkback: Discuss this article with peers

"Linux Gazette...making Linux just a little more fun!"


Connecting to Your Home Computer

By Robert Stagner


A couple of weeks back, I was at work typing away on my NT box (please, no flames on this, I have no other choice : -), when I suddenly realized that a Perl script that I had been updating on my home machine (a Linux box running Red Hat 6.0), needed to be transferred to my computer at work. Since I do not have a 24/7 connection to the Internet for my Linux box, I was out of luck. I could not just FTP to my home machine and download the script. Or could I?

I suppose I could set up a DSL or cable modem connection, but I personally find that a 56K PPP connection is a lot less financially burdensome -- at least for the time being. Anyhow, I thought about how I might somehow get the Linux box on the Internet while unattended (i.e. just after I left for work each morning). My solution, although involving several scripts and a cronjob, can be set up for a PPP connection rather quickly. Once tested and configured properly, the end result will provide you with a means of automatically connecting to the Internet and then sending an e-mail off to your work address containing the dynamically generated IP Address assigned to your home computer by your ISP. The only assumptions to get this up and running are the following:

Other than these assumptions, configuring your system to automatically dial-up your ISP are pretty straight forward. The first step to get this up and running is to copy over (as root) the ppp-on and ppp-off shell scripts to the /usr/sbin/ directory. Make sure the permissions are set to 0755 for each file. Then, copy over ppp-on-dialer to the /etc/ppp/ directory. The "dialer" script is the second part of a two tier step in establishing a connection to your ISP. The first, is the ppp-on script.

Next, edit the ppp-on script by assigning the appropriate telephone number, account and password information. Also, remember to assign the appropriate device and speed of your modem. Mine read /dev/ttyS3 and 115200. It should resemble the following once your done:


#!/bin/sh
#
# These are the parameters. Change as needed.
TELEPHONE=your phonenum # The telephone number for the connection
ACCOUNT=your account    # The account name for logon (as in 'George Burns')
PASSWORD=your password  # The password for this account (and 'Gracie Allen')
LOCAL_IP=0.0.0.0        # Local IP address if known. Dynamic = 0.0.0.0
REMOTE_IP=0.0.0.0       # Remote IP address if desired. Normally 0.0.0.0
NETMASK=255.255.255.0   # The proper netmask if needed 

# Export them so that they will be available at 'ppp-on-dialer' time.
export TELEPHONE ACCOUNT PASSWORD
#
# This is the location of the script which dials the phone and logs
# in.  Please use the absolute file name as the $PATH variable is not
# used on the connect option.  (To do so on a 'root' account would be
# a security hole so don't ask.)
#
DIALER_SCRIPT=/etc/ppp/ppp-on-dialer
#
# Initiate the connection

exec /usr/sbin/pppd debug lock modem crtscts /dev/ttyS3 115200 \
	asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP \
	noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT 

Red Text - code that needs to be altered to meet your needs.

Now that you have all the shell scripts in the appropriate places, it's time to look at a little Perl script I wrote that calls the connection or disconnection process, as well as send off an e-mail with the IP Address of my Linux box to my NT box at work. Place a copy (as root) of this script in your /usr/local/bin directory with a permissions setting of 0755. The purpose of this script is to:

The Perl script begins with the following lines:


# Counter used to provide a delay while the ppp connection is established
$COUNT = 1;

# Check for the existence of options
if (!$ARGV[0] || $ARGV[0] !~ m/(connect|disconnect)/i) {
        print "Usage: sendMeIP.pl <connect | disconnect>\n";
	exit -1;
}

# Call and execute connection/disconnection to the Internet
&pppConnection($ARGV[0]);

# Caputre the dynamically created remote IP Address
$assignedIPAddress = &captureIPAddress;

# Send the IP Address via e-mail
&sendMail($assignedIPAddress);



Essentially, all this does is first set a counter, then check to see if the necessary arguments have been passed to the program. If so, establish a connection (or, disconnection -- &pppConnection) to the Internet. Capture the IP Address (&captureIPAddress) by searching the output of a call to /sbin/ifconfig and finally send off the e-mail (&sendMail). The call to execute ifconfig actually happens in the &captureIPAddress subroutine. Let us have a look at this code:


sub captureIPAddress {
        my $captureIFCONFIG;
        $captureIFCONFIG = qx#/sbin/ifconfig#;

        if ($captureIFCONFIG =~ /inet addr:(\d+\.\d+.\d+\.\d+)\s*P-t-P.*/) {
                my $assignedIPAddress = "$1";
                print "IP Address capture successful: $assignedIPAddress.\n";
                return ($assignedIPAddress);
        }
        elsif (($captureIFCONFIG !~ /inet addr:(\d+\.\d+.\d+\.\d+)\s*P-t-P.*/)
	&& ($COUNT < 60)) {
                $COUNT++;
                sleep 1;
                &captureIPAddress;
        }
        else {
	   print "Houston, we have a problem in capturing the IP Address.\n";
       	   exit -2;         
	}
}


...  [code removed for clarity]


sub sendMail {
        my $assignedIPAddress = shift;
        my $mailService = location of your mail program: mine is "/usr/sbin/sendmail";
        my $from = "Your Name <yourLocalEmail\@host.com>";
        my $to = "Your Name <yourWorkEmail\@host.com>";

        die "Trouble sending mail: $!" unless (-e $mailService);

        open(SENDMAIL, "|$mailService -oi -t") or die "Trouble sending mail:
$!";

        print SENDMAIL <<"EOMAIL";
From: $from
To: $to
Subject: An Important Message About your.computer

Use the following IP Address to establish a connection with 
your.computer : $assignedIPAddress

EOMAIL
        close(SENDMAIL);
        print "The e-mail message has been sent.\n";
}

Red Text - code that needs to be altered to meet your needs.


First, the /sbin/ifconfig command is executed using Perl's equivalence of a backtick (qx function). The output of this command is captured in the $captureIFCONFIG variable. We then attempt to look for a pattern that contains "inet addr:" followed by an IP Address and then followed by "P-t-P". Since it's the address we're concerned with, we store this in $assignedIPAddress. However, if we have not matched the IP Address and the count is less than 60, then we add one to the count variable and put the program to sleep for one second. At this point, we call the same subroutine again and again until we have a pattern match or we've exceeded the count. This gives the /sbin/ifconfig command a chance to write out the appropriate interface information for a PPP connection. Actually, we give the program about 60 seconds to find the IP Address. If we can't find an IP Address, we print out an error message and exit the program.

Finally, you'll need to configure the Perl script to point to your email program, as well as set up the "From" and "To" e-mail addresses contained in the &sendMail subroutine. Simply reassign the $mailService, $from, and $to variables to match your case.

Now we've come to the final step -- setting up your system to automatically execute the Perl script. We will accomplish this through the use of the crontab command. As root, execute the crontab command with the -e option. The -e option puts you in the edit mode. From here add the following two lines and save the file:


00 8 * * 1,2,3,4,5 perl /usr/local/bin/sendMeIP.pl connect
15 12 * * 1,2,3,4,5 perl /usr/local/bin/sendMeIP.pl disconnect

The first of these two lines calls the Perl script at 8:00am, Monday thru Friday. The second line calls the Perl script to disconnect from the Internet at 12:15pm, Monday thru Friday. You can check your work in crontab by executing the crontab command again, but this time passing it the -l option. For further information on crontab, check out the man pages.

Once you have received the e-mail message at work, you should now be able to connect to your machine via telnet and ftp by using the given IP Address.


Copyright © 2000, Robert Stagner
Published in Issue 53 of Linux Gazette, May 2000

Talkback: Discuss this article with peers

[ Prev ] [ Table of Contents ] [ Front Page ] [ FAQ ] [ Talkback ] [ Next ]