« Linux Gazette…making Linux just a little more fun!«
Table des matières
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:
|
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:
- 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:
|
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:
|