Connecting to Your Home Computer LG #53

Rate this post

« Linux Gazette…making Linux just a little more fun!« 

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:

  • you have root permissions (usually the case, if your running this from home).
  • you have copies of ppp-on, ppp-off, and ppp-on-dialer shell scripts. These can usually be found in the /usr/doc/ppp-2.3.7/scripts/ directory.
  • you have successfully connected and disconnected to the Internet using the ppp-on and ppp-off scripts.
  • your ISP has provided you with access to a local telephone number (I suppose this is not really required, but who wants to pay phone charges).

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:

# 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= # Local IP address if known. Dynamic =
REMOTE_IP= # Remote IP address if desired. Normally
NETMASK= # The proper netmask if needed # Export them so that they will be available at 'ppp-on-dialer' time.
# 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.)
# 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.
Lire aussi...  The Back Page LG #63

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:

  • Provide a connection/disconnection to the Internet via a shell program (/usr/sbin/ppp-on, or ppp-off).
  • Capture the dynamically generated IP address from the ISP service.
  • Send the returned IP Address to a predefined e-mail address so that a remote connection can be made to your home computer.

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: \n";
        exit -1;

# Call and execute connection/disconnection to the Internet

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

# Send the IP Address via e-mail

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 "; my $to = "Your Name "; die "Trouble sending mail: $!" unless (-e $mailService); open(SENDMAIL, "|$mailService -oi -t") or die "Trouble sending mail:
$!"; print SENDMAIL