initial import of iptracker.pl
git-svn-id: svn://anubis/misc/iptracker@45 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
parent
6a17a48922
commit
af1e5f08b5
71
iptracker.pl
Executable file
71
iptracker.pl
Executable file
@ -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 = <CONF>)
|
||||
{
|
||||
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 = <HISTFILE>)
|
||||
{
|
||||
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 = <IP>)
|
||||
{
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user