# wanip.pl Mar 29 2002 by David Efflandt # DOS/Win version (Activestate Perl) # # Gets WAN IP from modified /begin.htm on DI-704[P]. # Only returns output when IP changes or error (for cron mail). # Optional local file logging. # # NOTE: Requires modified firmware to display WAN IP on /begin.htm # without logging in. Firmware modification only changes # "Web Configuration" string to "Web Config IP $XI" in BIN file. # # Perl Modules Used: # vars, LWP # Var declarations (do not change) my ($ip, $newip); # main variables that can be in separate require file use vars qw ($di704 $ipfile $bg $poll $tick $tock $logfile); ### User Variables ### # DI-704 IP (or IP:88 when WAN access is activated) $di704 = '192.168.0.1'; # default # File to store previous IP (full path) $ipfile = "wiplast.txt"; # Continuous loop if any string or positive number on commandline. # If changed from 0 to 1 below, use 0 on commandline to run once. $bg = shift or 0; # Poll interval in seconds if background (daemon) mode #$poll = 15; # DEBUG $poll = 300; # sec # How often to announce WAN IP in logs (alive status) # One less than multiple of $poll #$tick = 29; # DEBUG $tick = 3599; # sec # Subroutine to run if IP changes (full path) - use backticks # sub newip { return `/path_to/noip`; } # example sub newip { return `echo $ip (newip cmd example)`; } # DEBUG test # Log file full path (comment out to not use, also see syslog below) $logfile = "waniplog.txt"; # Alternate file to override above variables (ignored if not readable) $rcfile = "waniprc.txt"; ### End User Variables ### # Name of this script if ($0 =~ m|[/\\:]([^/\\:]+)$|) { $id = $1; } else { $id = $0; } # Get $rcfile variables if it exists and readable if (-r $rcfile) { eval `type $rcfile`; warn $@ if $@; } sub mylog { my $msg = shift; chomp $msg; if ($logfile) { if (open(LOG,">> $logfile") || open(LOG,"> $logfile")) { print LOG localtime() . " $id: $msg\n"; close LOG; } } print localtime() . " $id: $msg\n"; } # Initialize use LWP::UserAgent; $ua = new LWP::UserAgent; $tock = 0; sub checkip { my $req = new HTTP::Request GET => "http://$di704/begin.htm?RC=\@"; my $res = $ua->request($req); if ($res->is_success) { @lines = split /\n/, $res->content; $ip = ''; foreach $line (@lines) { # find IP if ($line =~ /[^\d]?(\d+\.\d+\.\d+\.\d+)[^\d]+/) { $ip = $1; last; } } return "Can't parse IP from /begin.htm" unless $ip; # Check if IP changed my $newip; if ((-s $ipfile) && open(IP, "+< $ipfile")) { if ("$ip\n" ne ) { seek IP,0,0; truncate IP,0; print IP "$ip\n"; $newip = 1; } close IP; } elsif (open (IP, "> $ipfile")) { print IP "$ip\n"; $newip = 1; close IP; } else { mylog("Can't store current IP: $!"); } if ($newip) { if ($ip eq '0.0.0.0') { mylog("$ip down"); } else { mylog("new IP $ip"); # Do sub and log result if any $_ = newip; # Comment next line if program does own syslog mylog($_) if ($_); } $tock = time + $tick; } else { if (time > $tock) { $tock = time + $tick; mylog("WAN $ip"); } } sleep $poll if $bg; return 0; } else { return "Could not access $di704"; } } if ($bg) { mylog("Poll $di704 $poll/$tick sec"); while (1) { mylog($_) if $_ = checkip; } } else { mylog("Polling $di704"); checkip; } # To pause after right click launch unless ($bg) { print "Hit enter to end\n"; ; }