Here’s a script I wrote in perl (yes perl) that checks your ip and will email you if the ip changes. Because I’m using private IP’s, I need to check via an external server. What I want to know is if my IP changes, because if it does, I probably need to restart my IRC Bot. Because of the private IP mess, I need to use an external to get my external IP. I use a simple PHP script on my server from Fivebean to tell me the IP.
The PHP is:
<?php echo $_SERVER['REMOTE_ADDR']; ?>
Stick that somewhere that’s not on your local LAN.
Now, make a batch file with this
perl ip_checker.pl location_of_previous_php_script file_to_write_some_info_to
Coincidentally, I think it would be best if I maybe removed those option and just put them in the configuration section of the perl script . . .
Copy this and save this within the same place as that bat file, using the name ip_checker.pl. Make sure you have Net::SMTP::SSL installed in your perl modules.
use LWP::Simple;
use Net::SMTP::SSL;
use warnings; use strict;
sub text;
#texting information
#configure whether to send alerts. Email based
################################
######Script configuration######
################################
################################
#####Options Confiugration######
################################
my $send_alert = 'true'; #set true to allow messages to be sent informing you of changes to the local IP
################################
#####Email send information#####
################################
my $email_from = '';
my $email_password = '';
my $send_to= '';
my $email_host ='smtp.gmail.com'; #something link that anyway
################################
##End of Configuration Section##
################################
if (@ARGV ne 2){
die 'Usage: perl ipchecker.pl (URL To Retrieve External IP From) (File to ReadWrite LastCurrent IP From)n';
}
my $ip_bouncer = shift;
my $read_file_location = shift;
my $current_ip = get($ip_bouncer); die "Couldn't get it!" unless $current_ip;
my $old_ip; my $new_ip;
my $read_file; my $print_file;
open OLD_IP, "$read_file_location" or die "$!"; #open with write rights
$old_ip = <OLD_IP>;
close OLD_IP;
open OLD_IP, ">$read_file_location" or die "$!"; #open with write rights
print "Current IP Address: $current_ip n";
print "Last IP Address: $old_ip n";
print OLD_IP $current_ip;
close OLD_IP;
if ($current_ip ne $old_ip){ #ne is a string comparison of equal or not == is numeric comparison
#/&text ($host, $from, $password, $message );
print "Alert! IP address has changedn";
if ($send_alert eq 'true'){
print "Sending Alert!n";
my $desired_message = "IP Change $current_ip";
&text ( $send_to, $email_host, $email_from, $email_password, $desired_message);
print "Alert Sentn";
}
} else {
print "All is welln";
}
sub text { # ($to, $host, $from, $password, $message )
#mostly from http://robertmaldon.blogspot.com/2006/10/sending-email-through-google-smtp-from.html
#modified to suit my needs
my $to = shift;
my $host = shift;
my $from = shift;
my $password = shift;
my $message = shift;
my $smtp;
if (not $smtp = Net::SMTP::SSL->new($host,
Port=> 465,
Debug => 1,
) ) { die "Couldn't connect" };
$smtp->auth($from, $password) || die "Authentication failed!n";
$smtp->mail($from."n" ); # use the sender's address here
$smtp->to($to."n" ); # recipient's address
$smtp->data(); # Start the mail
# Send the header.
$smtp->datasend("To: $to n" );
$smtp->datasend("From: $from n" );
$smtp->datasend("Subject: Server Alertn");
$smtp->datasend('Content-Type: text/plain; charset=ISO-8859-1;' . "nn");
# Send the body.
$smtp->datasend("Automated Server Message n", $message, "n");
$smtp->dataend(); # Finish sending the mail
$smtp->quit; # Close the SMTP connection
}
Schedule every 6 hours or so. I did it with the scheduled jobs thingy in windows server 2003.
Sorry for the poor write up, hit me up in the comments if you need help setting this up or if you have other general questions. Right now, I have to go mess with a wordpress theme in need of fixin . . . .
sub text;
#texting information
#configure whether to send alerts. Email based
################################
######Script configuration######
################################
################################
#####Options Confiugration######
################################
my $send_alert = ‘true’; #set true to allow messages to be sent informing you of changes to the local IP
################################
#####Email send information#####
################################
my $email_from = ”;
my $email_password = ”;
my $send_to= ”;
my $email_host =’smtp.gmail.com’; #something link that anyway
################################
##End of Configuration Section##
################################
if (@ARGV ne 2){
die ‘Usage: perl ipchecker.pl (URL To Retrieve External IP From) (File to ReadWrite LastCurrent IP From)n’;
}
my $ip_bouncer = shift;
my $read_file_location = shift;
my $current_ip = get($ip_bouncer); die “Couldn’t get it!” unless $current_ip;
my $old_ip; my $new_ip;
my $read_file; my $print_file;
open OLD_IP, “$read_file_location” or die “$!”; #open with write rights
$old_ip = <OLD_IP>;
close OLD_IP;
open OLD_IP, “>$read_file_location” or die “$!”; #open with write rights
print “Current IP Address: $current_ip n”;
print “Last IP Address: $old_ip n”;
print OLD_IP $current_ip;
close OLD_IP;
if ($current_ip ne $old_ip){ #ne is a string comparison of equal or not == is numeric comparison
#/&text ($host, $from, $password, $message );
print “Alert! IP address has changedn”;
if ($send_alert eq ‘true’){
print “Sending Alert!n”;
my $desired_message = “IP Change $current_ip”;
&text ( $send_to, $email_host, $email_from, $email_password, $desired_message);
print “Alert Sentn”;
}
} else {
print “All is welln”;
}
sub text { # ($to, $host, $from, $password, $message )
#mostly from http://robertmaldon.blogspot.com/2006/10/sending-email-through-google-smtp-from.html
#modified to suit my needs
my $to = shift;
my $host = shift;
my $from = shift;
my $password = shift;
my $message = shift;
my $smtp;
if (not $smtp = Net::SMTP::SSL->new($host,
Port=> 465,
Debug => 1,
) ) { die “Couldn’t connect” };
$smtp->auth($from, $password) || die “Authentication failed!n”;
$smtp->mail($from.”n” ); # use the sender’s address here
$smtp->to($to.”n” ); # recipient’s address
$smtp->data(); # Start the mail
# Send the header.
$smtp->datasend(“To: $to n” );
$smtp->datasend(“From: $from n” );
$smtp->datasend(“Subject: Server Alertn”);
$smtp->datasend(‘Content-Type: text/plain; charset=ISO-8859-1;’ . “nn”);
# Send the body.
$smtp->datasend(“Automated Server Message n”, $message, “n”);
$smtp->dataend(); # Finish sending the mail
$smtp->quit; # Close the SMTP connection
Great post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!