Re: BASH script to list previous IP Addresses

From: Lew Pitcher (lpitcher_at_sympatico.ca)
Date: 03/20/04


Date: Fri, 19 Mar 2004 22:43:10 -0500


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Circuit Breaker wrote:
| Greets, all...
|
| I have written a simple, short script that runs after I connect to my
| ISP via dial-up. The idea was originally to keep a list of the IP
| addresses I have been assigned as well as the servers I have connected to.
| However, since I wasn't able to get it to keep more than one address in
| the "list". What I wound up doing for this was to grep / cut the IP
| address from the output of `ifconfig ppp0` and store it in a variable,
| then output that variable as "current" then save it in the file to be
| displayed next time as the "previous" address.
|
| What I would like to do is maintain a list of the previous, say, 20 or 30
| addresses and the servers I've connected to, and I figure they'll have to
| be stored in a file.
|
| I've tried using cat, but of course I can't `cat {file} {newdata} >
| {file}` because it tells me that the same file is used as both input and
| output.
|
| I think what's going to wind up happening is that I'll have two dummy
| files. One to store the old address, one to store the new, then a third
| to use `cat file1 file2 > file3` with to store all addresses. But as I
| think about this 'solution', I realize that anytime I cat file1 and file2
| to file3, file3 will get overwritten anyway, and will still contain no
| more information than files 1 and 2 by themselves.
|
| Now, there's got to be a[n easier] way to do this... any ideas?
|
| Here's the part of my script I have so far...
|
| <begin bashing>
|
| PUBLIC_PPP_IP=`ifconfig ppp0 | grep inet | cut -d : -f 2 | cut -d \ -f 1`
| LAST_PUBLIC_PPP_IP=`cat /var/inet/last_public_PPP_IP`
| PPP_SERVER_IP=`ifconfig ppp0 | grep P-t-P | cut -d : -f 3 | cut -d \ -f 1`
| LAST_PPP_SERVER_IP=`cat /var/inet/last_PPP_server_IP`
|
| echo $PUBLIC_PPP_IP > /var/inet/last_public_PPP_IP
| echo $PPP_SERVER_IP > /var/inet/last_PPP_server_IP
|
| echo " "
| echo " Current public PPP address: $PUBLIC_PPP_IP"
| echo " Previous public PPP address: $LAST_PUBLIC_PPP_IP"
| echo " "
| echo " Current PPP server: $PPP_SERVER_IP"
| echo " Previous PPP server: $LAST_PPP_SERVER_IP"
| echo " "
|
| <end bashing>
|
| TIA for any help

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
~ - the name of the logical interface over which the connection has been
~ established (eg ppp0, ppp1)
~ - the name of the physical interface over which the connection has
~ been established (eg ttyS0, ttyS1)
~ - the speed of the interface (it's baud)
~ - the *local* IP address (the address assigned to our side of the
~ connection)
~ - the remote IP address (the address assigned to the ISP side of the
~ connection), and
~ - the value set in the ppp configuration's "ipparm" option

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) interfacee 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
~ if [ -f /var/run/$1.ip ]
~ then
~ echo "Interface $1 is address " `cat /var/run/$1.ip`
~ exit 0
~ else
~ echo "Interface $1 is down"
~ exit 1
~ fi

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?"

Now, what does this have to do with /your/ problem?

Let's modify the /etc/ppp/ip-up script a bit. Originally, I had

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

Let's add a couple of lines to this. We want to
a) add our current IP address and the address of the far end to a file
b) trim the file to the last 20 lines
So, the script now looks like...

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

~ # Add our IP address, and his IP address to the record file.
~ echo $4 $5 >>/var/run/$1.ip.records

~ # save the last 20 lines to a seperate file,
~ tail -20 /var/run/$1.ip.records >/var/run/$1.ip.temp

~ # replace the record file with the trimmed record file
~ mv /var/run/$1.ip.temp /var/run/$1.ip.records

Does this do what you're looking for?

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFAW73MagVFX4UWr64RAgZuAKDltn77ELOnOQ8s+4d+rL/SS9n2vQCfWYHJ
a738ZuTrCMiz1um1Chu8oy4=
=0kDw
-----END PGP SIGNATURE-----



Relevant Pages

  • Re: [SLE] Still having firewall and dialup problems
    ... >>I have to stop and restart the firewall everytime I connect to my ISP. ... >>Since my dialup dies fairly often and has an 8 hour limit where the ISP ... that calls a chat script to start my connection. ...
    (SuSE)
  • Re: PPP ip-up and firestarter
    ... >> up and stop it whenever my connection goes down. ... >> script runs it whenever ppp is up. ... > iptables -L does display the firewall rules. ... Anyway, whenever I get connected to my ISP, I get the ...
    (Debian-User)
  • load balancing, fail-over
    ... Nano-Howto to use more than one independent internet connection. ... The router is load balancing between 2 external independent Internet ... I have written a PERL script to check if a connection is down. ... the down interface with a metric of 19 and pinging 3 different public ...
    (comp.os.linux.networking)
  • Re: C and other programming languages
    ... usually wrapping a C function requires manual effort, ... How is it done in you language ... the interface froim Tcl/Tk to C is somehow better, ... the script language. ...
    (comp.lang.misc)
  • Re: Can the net connection binding order be changed via script or
    ... >>>network and I want to avoid having docked laptops use the wireless connection ... >> Instead of writing a script, and having the script run when docking or undocking ... >-Is there a way to view the automatically set metrics? ... When you adjust the metric for an IP interface, ...
    (microsoft.public.windowsxp.network_web)