72 lines
1.5 KiB
Perl
Executable File
72 lines
1.5 KiB
Perl
Executable File
#!/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);
|
|
|