ToC Home Issues Hearts Links

Issue #4, November 2005

Slack Essence-2: What is my IP address?

Author: Lew Pitcher

I have had need, on occasion, to determine the IP address assigned to my PPP connection by my ISP, but have been reluctant to code the usually complex scripts nececssary to extract this information from the results of the 'ifconfig' command.

Fortunate for lazy me, there's an easy way to determine the PPP IP address, and it's (more or less) built right in to the pppd daemon. I simply had the ppp daemon put the IP address in a file, so I could read it. Here's the trick: the ppp daemon runs a script called /etc/ppp/ip-up whenever the IP connection to the ISP is established, and another script (called /etc/ppp/ip-down) when the IP connection is broken.

The /etc/ppp/ip-up and /etc/ppp/ip-down scripts are invoked with several parameters, including

Minor additions to these two scripts will give us exactly the information we need: an indicator showing whether an IP address has been assigned or not, the value of the assigned IP address, and the date and time when the IP address was assigned.

Here's how we do it:

In /etc/ppp/ip-up (or /etc/ppp/ip-up.local, if your installation supports it) we add these lines:

# Create sentinal file
rm /var/run/$1.ip
echo $4 >/var/run/$1.ip

and in /etc/ppp/ip-down (or /etc/ppp/ip-down.local, if your installation supports it) we add:

# Delete sentinal file
rm /var/run/$1.ip

When pppd establishes an IP session with your ISP, it invokes /etc/ppp/ip-up, giving it the interface name as parameter $1 and the assigned IP address as parameter $4. The additional lines in /etc/ppp/ip-up will create a file using the interface name, and load that file with the IP address. Assuming that you establish an IP session across (say) interface ppp0, the /etc/ppp/ip-up script will build a file called /var/run/ppp0.ip, and put the assigned IP address into it.

When pppd terminates the IP session, it invokes /etc/ppp/ip-down, giving it the interface name as parameter $1. The additional lines in /etc/ppp/ip-down will use this parameter to select and delete the file that records the IP address for that interface.

Now, when the interface is down, and we do not have an IP address, there will be no /var/run/*.ip file for the interface.

When the interface is up, and we don't have an IP address, there will still be no /var/run/*.ip file for the interface.

However, when the interface is up and we have an IP address, there will be a /var/run/*.ip file, and that file will carry as its contents the IP address assigned to the interface. Further, the "last modification date" (mtime) on the file will indicate when the file was written, and thus indicate the time that the IP address was assigned.

Knowing this, we can write a simple script that tells us the IP address of any ppp interface:

#!/bin/bash
IFACE=ppp0

[ "$1" != "" ] && IFACE=$1
IFACE_UC=`echo $IFACE | tr 'a-z' 'A-Z'`

if [ -f /var/run/$IFACE.ip ]
then
   SINCE=`ls -l /var/run/$IFACE.ip | awk '{ print $6,"@",$7 }'`
   echo "Yes, $IFACE_UC is UP since $SINCE with IP address " \
        `cat /var/run/$IFACE.ip`
   exit 0
else
   echo "No, $IFACE_UC is DOWN"
   exit 1
fi

In use, this script generates results like

   lpitcher@merlin:~$ is_ppp_up
   Yes, PPP0 is UP since 2005-10-28 @ 16:55 with IP address  10.99.99.99

   lpitcher@merlin:~$ is_ppp_up ppp1
   No, PPP1 is DOWN

Of course, the script can be expanded on to include all those sorts of things that one wants in a robust command (like argument checking and better functionality), but in it's basic form, the script provides the answer to the age-old question "What's my IP address?"

There are other methods that you can use to determine your PPP IP address (like greping and awking the results of an "ifconfig ppp0" command, or running a custom program that reads the PPP interfaces IP addresses directly), but these methods don't really satisfy my needs.

I have often used pppd's "dial-on-demand" option to establish my internet connection, and (in that case) pppd is always up. That means that ppp0 is always up, but it usually has the wrong IP address. For instance, when I wrote this, I was not connected to the internet. But, ifconfig reported

   lpitcher@merlin:~$ /sbin/ifconfig ppp0
   ppp0      Link encap:Point-to-Point Protocol
             inet addr:64.99.99.99  P-t-P:64.8.99.1
             Mask:255.255.255.255
             UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
             RX packets:34520 errors:29 dropped:0 overruns:0 frame:29
             TX packets:32103 errors:0 dropped:0 overruns:0 carrier:0
             collisions:0 txqueuelen:10

Note that the ifconfig results indicate that my ppp0 IP address was 64.99.99.99. When I started up ppp0, ifconfig reported that it had IP address 10.10.10.10

In both cases, the IP address reported by ifconfig (and by any other program parsing the parameters of the ppp0 interface) is not the address that ppp0 will be assigned when I connect. The only time that ifconfig or another program will accurately report the correct IP address is when ppp0 is connected to my ISP. All other times, the address reported will be wrong.

So, to avoid getting the wrong results when my IP connection is down, I use the ip-up/ip-down scripts to record the actual IP address assigned at the time of connection (or whenever the IP address is renegotiated).



BerliOS Logo