#!/usr/bin/perl
use warnings;
use strict;
# Declarations of scalars and such
my ($oldIPAddress, $currentIPAddress);
my $logFilename = 'iplog_perl';
# change directory to root or inform user of his none-rootness
chdir '/root/logs/' or die "Are you root, dude?: $!", "\n";
# open the IP log file to read last IP from it
open my $IPLOG, '<', $logFilename;
# read from IPLOG handle and get the last IP address saved
while ( <$IPLOG> ) {
$oldIPAddress = $_;
chomp $oldIPAddress;
print "The old IP address was: $oldIPAddress", "\n";
}
# close filehandle
close $IPLOG;
# calls 'ifconfig' and stores output in $output
my $output = `ifconfig`;
# $output operations
my @lines = split /\n/, $output;
# goes through @lines until $line matches the regex
for my $line ( @lines ) {
if ( $line =~ /\s+inet addr:(.*).P-t-P/ ) {
$currentIPAddress = $1;
if ( not defined $currentIPAddress ) {
die 'Are you sure your connection is up?', "\n";
}
print "Current IP address is: $currentIPAddress", "\n";
}
}
# checks
if ( $oldIPAddress =~ /$currentIPAddress/) {
print 'Warning: Old and new IP addresses match. Reconnect.', "\n";
}
else {
print 'Success. IP addresses are different.', "\n";
}
# logs current IP
open $IPLOG, ">", $log_filename;
print $IPLOG $ip_address;
close $IPLOG;