![]() |
||||||
|
I've got my number |
||||||
|
Gadgets
|
I'm going to assume you obtain your IP address through a dial-up to an
Internet Service Provider. If you don't, that's OK. Just know what device
your connection is on. On Linux and other Unices, you can determine whether
you are connected (and, at what address) by issuing the ifconfig
command. This command frequently lives in /sbin (YMMV), and you may have to
give it the full path. When you do run it, you should see something like the
following:
My PPP connection to the Internet is called internally ppp0 by my OS. Were I connected through my Ethernet device, it would be eth0. Look for something similar. The two things to look for in this report are the presence of ppp0, whose absence would indicate I'm not connected, and the numbers following the term "inet addr." Let's construct a Perl script that checks to see whether we're on the Internet and, if so, at what address.
Notice the "back ticks" in the first real line of code. They cause a command to run, catching its output (which would normally go to STDOUT) in the variable on the left side of the equation. This would be 14 lines of output we saw earlier when we ran ifconfig from the command line. The next line of code checks for the presence of the "ppp0" string, saving it, if matched, in the $1 variable. Whatever follows is caught in the $2 variable and becomes all that's left of $ip. Then it's matter of seeing whether anything was matched by "ppp0." If not, there is no connection. Otherwise, we know our Internet address is in what is now in $ip, and that it's comprised of 4 dot-separated sets of numbers, each from 1 to 3 digits long. We also know that our Internet address immediately follows the string "inet addr:", so we include that to ensure we don't get the address of the machine we're connected to. Using Perl regular expression matching, it's easy to strip the IP address right out. << Back Next >>
|
|||||
|
Home | Gadgets | Code | Links | Reads | Contact Copyright © 1999, 2001, 2002 by John H. Byrd All rights reserved. |