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