From af1e5f08b5ec39d1f5152d0ebc09e6da70c35fd0 Mon Sep 17 00:00:00 2001 From: josh Date: Wed, 19 Mar 2008 02:38:01 +0000 Subject: [PATCH] initial import of iptracker.pl git-svn-id: svn://anubis/misc/iptracker@45 bd8a9e45-a331-0410-811e-c64571078777 --- iptracker.pl | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 iptracker.pl diff --git a/iptracker.pl b/iptracker.pl new file mode 100755 index 0000000..d03677c --- /dev/null +++ b/iptracker.pl @@ -0,0 +1,71 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +my $IPTRACKER_BASE = '/etc/iptracker'; +my $CONF_FILE = "$IPTRACKER_BASE/iptracker.conf"; + +my %conf; +$conf{'dev'} = 'eth0'; +$conf{'histfile'} = "$IPTRACKER_BASE/history"; + +# grab conf vars from configuration file if one is present +if (-r $CONF_FILE) +{ + open(CONF, '<', $CONF_FILE); + while (my $line = ) + { + next if ($line =~ /^\s*#/); + if ($line =~ /^\s*([^=]+?)\s*=\s*(.*?)\s*$/) + { + my ($var, $val) = ($1, $2); + $conf{$var} = $val; + } + } + close(CONF); +} + +# grab the last IP address we were assigned, if any +my $lastIP = ''; +if (-r $conf{'histfile'}) +{ + open(HISTFILE, '<', $conf{'histfile'}); + while (my $line = ) + { + if ($line =~ /^\S+\s+(\S+)$/) + { + $lastIP = $1; + } + } + close(HISTFILE); +} + +# get our current IP +my $ipcmd = 'ip address show dev ' . $conf{'dev'}; +my $currentIP = ''; +open(IP, '-|', $ipcmd); +while (my $line = ) +{ + if ($line =~ /^\s*inet\s+(\d+\.\d+\.\d+\.\d+)/) + { + $currentIP = $1; + } +} +close(IP); + +# if we successfully obtained the current IP and it has changed, record this +if ( ($currentIP ne '') && ($currentIP ne $lastIP) ) +{ + my (undef, $min, $hour, $mday, $mon, $year) = localtime(time()); + $mon++; + $year += 1900; + my $date = sprintf("%04d-%02d-%02d_%02d:%02d", + $year, $mon, $mday, $hour, $min); + open(HISTFILE, '>>', $conf{'histfile'}); + print HISTFILE "$date $currentIP\n"; + close(HISTFILE); +} + +exit(0); +