All pastes #989719 Raw Edit

Stuff

public text v1 · immutable
#989719 ·published 2008-04-19 17:10 UTC
rendered paste body
#!/usr/bin/perl
use warnings;
use strict;

# Declarations of scalars and such
my ($old_ip_address,$ip_address);
my $log_filename = "iplog_perl";

# change directory to root or inform user of his none-rootness
chdir "/root/logs" or die "Are you root, dude?: $!";

# open the IP log file to read last IP from it
open IPLOG, "<", "$log_filename";

# read from IPLOG handle and get the last IP address saved 
while ( <IPLOG> ) {
	chomp;
	$old_ip_address = $_;
	print "The old IP address was: $old_ip_address", "\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/) {
		unless (defined($1)) {
			die "Are you sure your connection is up?", "\n";
			}
		$ip_address = $1;
		print "Current IP address is: $ip_address", "\n";
	}
}

# checks

if ($old_ip_address =~ /$ip_address/) {
	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;