old-www/LDP/LG/issue53/stagner.html

348 lines
14 KiB
HTML

<!--startcut ==============================================-->
<!-- *** BEGIN HTML header *** -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<title>Connecting to Your Home Computer LG #53</title>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!-- *** END HTML header *** -->
<A HREF="http://www.linuxgazette.com/">
<H1><IMG ALT="LINUX GAZETTE" SRC="../gx/lglogo.jpg"
WIDTH="600" HEIGHT="124" border="0"></H1></A>
<!-- *** BEGIN navbar *** -->
<A HREF="sipos.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
<IMG ALT=""
SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom" >
<A HREF="index.html"><IMG ALT="[ Table of Contents ]"
SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
<A HREF="../index.html"><IMG ALT="[ Front Page ]"
SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
<A HREF="../faq/index.html"><IMG ALT="[ FAQ ]"
SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
<A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue53/stagner.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
<IMG ALT=""
SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom" >
<A HREF="ward.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
<!-- *** END navbar *** -->
<P>
<!-- A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue53/stagner.html">
<FONT SIZE="+2"><EM>Talkback:</EM> Discuss this article with peers</FONT></A -->
<!--endcut ============================================================-->
<H4>
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>
<P> <HR> <P>
<!--===================================================================-->
<center>
<H1><font color="maroon">Connecting to Your Home Computer</font></H1>
<H4>By <a href="mailto:restagner@netscape.net">Robert Stagner</a></H4>
</center>
<P> <HR> <P>
<!-- END header -->
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?
<P>
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:
<UL>
<LI>you have root permissions (usually the case, if your running this from home).
<LI>you have copies of <A HREF="misc/stagner/ppp-on.txt">ppp-on</A>, <A HREF="misc/stagner/ppp-off.txt">ppp-off</A>, and <A HREF="misc/stagner/ppp-on-dialer.txt">ppp-on-dialer</A> shell scripts. These can
usually be found in the /usr/doc/ppp-2.3.7/scripts/ directory.
<LI>you have successfully connected and disconnected to the Internet using the
ppp-on and ppp-off scripts.
<LI>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).
</UL>
<P>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 &quot;dialer&quot; script
is the second part of a two tier step in establishing a connection to your
ISP. The first, is the ppp-on script.
<P>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:
<BR>
<BR>
<CENTER>
<TABLE BORDER=0 CELLASPACING=0 CELLPADDING=4 WIDTH="80%">
<TR>
<TD BGCOLOR="FFFFCC">
<PRE>
<CODE>
#!/bin/sh
#
# These are the parameters. Change as needed.
TELEPHONE=<FONT COLOR="red"><B>your phonenum </B></FONT># The telephone number for the connection
ACCOUNT=<FONT COLOR="red"><B>your account </B></FONT> # The account name for logon (as in 'George Burns')
PASSWORD=<FONT COLOR="red"><B>your password </B></FONT> # 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 <FONT COLOR="red"><B>/dev/ttyS3 115200</B></FONT> \
asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP \
noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT
</CODE>
</PRE>
</TD>
</TR>
<TR>
<TD BGCOLOR="#FFFFFF">
<FONT COLOR="red"><B>Red Text</B></FONT> - code that needs to be altered to meet your needs.
</TD>
</TR>
</TABLE>
</CENTER>
<P>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
<a href="misc/stagner/sendMeIP.pl.txt">script</a> in your /usr/local/bin directory with a
permissions setting of 0755. The purpose of this script is to:
<UL>
<LI>Provide a connection/disconnection to the Internet via a
shell program (/usr/sbin/ppp-on, or ppp-off).
<LI>Capture the dynamically generated IP address from the ISP service.
<LI>Send the returned IP Address to a predefined e-mail address so that
a remote connection can be made to your home computer.
</UL>
<P>The Perl script begins with the following lines:
<BR>
<BR>
<CENTER>
<TABLE BORDER=0 CELLASPACING=0 CELLPADDING=4 WIDTH="80%">
<TR>
<TD BGCOLOR="FFFFCC">
<PRE>
<CODE>
# 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 &quot;Usage: sendMeIP.pl &lt;connect | disconnect&gt;\n&quot;;
exit -1;
}
# Call and execute connection/disconnection to the Internet
&amp;pppConnection($ARGV[0]);
# Caputre the dynamically created remote IP Address
$assignedIPAddress = &amp;captureIPAddress;
# Send the IP Address via e-mail
&amp;sendMail($assignedIPAddress);
</CODE>
</PRE>
</TD>
</TR>
</TABLE>
</CENTER>
<BR>
<BR>
<P>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 -- &amp;pppConnection) to the Internet. Capture
the IP Address (&amp;captureIPAddress) by searching the output of a call to
/sbin/ifconfig and finally send off the e-mail (&amp;sendMail). The call to execute
ifconfig actually happens in the &amp;captureIPAddress subroutine. Let us have a
look at this code:
<BR>
<BR>
<CENTER>
<TABLE BORDER=0 CELLASPACING=0 CELLPADDING=4 WIDTH="80%">
<TR>
<TD BGCOLOR="FFFFCC">
<PRE>
<CODE>
sub captureIPAddress {
my $captureIFCONFIG;
$captureIFCONFIG = qx#/sbin/ifconfig#;
if ($captureIFCONFIG =~ /inet addr:(\d+\.\d+.\d+\.\d+)\s*P-t-P.*/) {
my $assignedIPAddress = &quot;$1&quot;;
print &quot;IP Address capture successful: $assignedIPAddress.\n&quot;;
return ($assignedIPAddress);
}
elsif (($captureIFCONFIG !~ /inet addr:(\d+\.\d+.\d+\.\d+)\s*P-t-P.*/)
&amp;&amp; ($COUNT &lt; 60)) {
$COUNT++;
sleep 1;
&amp;captureIPAddress;
}
else {
print &quot;Houston, we have a problem in capturing the IP Address.\n&quot;;
exit -2;
}
}
... [code removed for clarity]
sub sendMail {
my $assignedIPAddress = shift;
my $mailService = <FONT COLOR="red"><B>location of your mail program: mine is "/usr/sbin/sendmail"</B></FONT>;
my $from = <FONT COLOR="red"><B>"Your Name &lt;yourLocalEmail\@host.com&gt;"</B></FONT>;
my $to = <FONT COLOR="red"><B>"Your Name &lt;yourWorkEmail\@host.com&gt;"</B></FONT>;
die "Trouble sending mail: $!" unless (-e $mailService);
open(SENDMAIL, "|$mailService -oi -t") or die "Trouble sending mail:
$!";
print SENDMAIL &lt;&lt;"EOMAIL";
From: $from
To: $to
Subject: An Important Message About <FONT COLOR="red"><B>your.computer</B></FONT>
Use the following IP Address to establish a connection with <FONT COLOR="red">
<B>your.computer</B></FONT> : $assignedIPAddress
EOMAIL
close(SENDMAIL);
print "The e-mail message has been sent.\n";
}
</CODE>
</PRE>
</TD>
</TR>
<TR>
<TD BGCOLOR="#FFFFFF">
<FONT COLOR="red"><B>Red Text</B></FONT> - code that needs to be altered to meet your needs.
</TD>
</TR>
</TABLE>
</CENTER>
<BR>
<BR>
<P>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 &quot;inet addr:&quot; followed by an IP Address and then followed by &quot;P-t-P&quot;.
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.
<P>Finally, you'll need to configure the Perl script to point to your email
program, as well as set up the &quot;From&quot; and &quot;To&quot; e-mail addresses contained in
the &amp;sendMail subroutine. Simply reassign the $mailService, $from, and $to
variables to match your case.
<P>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:
<BR>
<BR>
<PRE>
<CODE>
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
</CODE>
</PRE>
<P>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.
<P>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.
<!-- *** BEGIN copyright *** -->
<P> <hr> <!-- P -->
<H5 ALIGN=center>
Copyright &copy; 2000, Robert Stagner<BR>
Published in Issue 53 of <i>Linux Gazette</i>, May 2000</H5>
<!-- *** END copyright *** -->
<!--startcut ==========================================================-->
<!-- P --> <HR> <!-- P -->
<A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue53/stagner.html">
<FONT SIZE="+2"><EM>Talkback:</EM> Discuss this article with peers</FONT></A>
<P>
<!-- *** BEGIN navbar *** -->
<A HREF="sipos.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
<IMG ALT=""
SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom" >
<A HREF="index.html"><IMG ALT="[ Table of Contents ]"
SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
<A HREF="../index.html"><IMG ALT="[ Front Page ]"
SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
<A HREF="../faq/index.html"><IMG ALT="[ FAQ ]"
SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
<A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue53/stagner.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
<IMG ALT=""
SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom" >
<A HREF="ward.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
<!-- *** END navbar *** -->
</BODY></HTML>
<!--endcut ============================================================-->