Hi to all, this is my first script that I post here. Is a simple way to replace the DDNS free services.

Basically it stores the IP (retrieved from the italian site www.chisono.it) in a text file (that must exist) and checks it whenever the crond daemon decides to do.

The script should be executed by cron not often (for me a nice time would be 30 minutes) and requires a MTA installed on the system to work, and also the mailx app.

The logit function is stolen from http://lexington.pm.org/meetings/022001.html

Every comment is appreciated...I know that there's a lot to do to improve it (such add the file existence check and a library for smtp support) but is my quick-and-dirty interpretation of perl....

#!/usr/bin/perl use strict; use LWP::Simple; use Sys::Syslog qw( :DEFAULT setlogsock); my $dir = "/root/script/"; my $emailadd = "your\@email.com"; my $fromadd = "ipwatch\@yourmachine"; chdir $dir; my $current_ip = get('http://www.chisono.it/ip.asp') || logit('err', " +Cannot retrieve the IP address: $!\n"); my ($a1, $a2, $a3, $a4) = split /\./, $current_ip; open FILE, "myip.txt" or logit('err', "Cannot open the file: $!\n"); my $readip = <FILE>; my ($b1, $b2, $b3, $b4) = split /\./, $readip; if ($readip eq $current_ip) { logit('info', "Address not changed\n"); } else { logit('info', "Address changed: old IP: $readip ; New IP: $curr +ent_ip\n"); getstore('http://www.chisono.it/ip.asp', 'myip.txt'); system("mailx -s \"IP changed!\" -a \"From: IPWatch <$fromadd>\ +" $emailadd < myip.txt") || logit('err', "Cannot send the email!\n"); } sub logit { my ($priority, $msg) = @_; return 0 unless ($priority =~ /info|err|debug/); setlogsock('unix'); openlog($0, 'pid,cons', 'user'); syslog($priority, $msg); closelog(); return 1; }

Replies are listed 'Best First'.
Re: Send your IP
by jwkrahn (Abbot) on May 22, 2009 at 19:21 UTC
    • You should verify that chdir worked.

    • You create the variables $a1 $a2 $a3 $a4 $b1 $b2 $b3 $b4 but you never use them.

    • You log a message if $current_ip could not be obtained but you proceed to the next step as if it had been.

    • You log a message if "myip.txt" could not be opened but you proceed to the next step as if it had been.

    • You compare the two strings $readip and $current_ip which may have extraneous trailing whitespace.

    • Your use of system sends an error message to the log file when it succeeds but no message if it fails.

    Something like this may be better:

    #!/usr/bin/perl use warnings; use strict; use Tie::File; use LWP::Simple; use Sys::Syslog qw( :DEFAULT setlogsock ); my $file = '/root/script/myip.txt'; my $emailadd = 'your@email.com'; my $fromadd = 'ipwatch@yourmachine'; my $current_ip = get( 'http://www.chisono.it/ip.asp' ) or do { logit( 'err', "Cannot retrieve the IP address\n" ); exit 1; }; $current_ip =~ s/\s+$//; tie my @read_ip, 'Tie::File', $file or do { logit( 'err', "Cannot open $file: $!\n" ); exit 2; }; if ( $read_ip[ 0 ] eq $current_ip ) { logit( 'info', "Address not changed\n" ); exit 0; } logit( 'info', "Address changed: old IP: $read_ip[0] ; New IP: $curren +t_ip\n" ); $read_ip[ 0 ] = $current_ip; untie @read_ip; # 0 == system qq(mailx -s "IP changed!" -a "From: IPWatch <$fromadd>" +$emailadd < myip.txt) or do { # # Update -- changed "< myip.txt" to "< $file" # 0 == system qq(mailx -s "IP changed!" -a "From: IPWatch <$fromadd>" $e +mailadd < $file) or do { logit( 'err', "Cannot send the email!\n" ); exit 3; }; sub logit { my ( $priority, $msg ) = @_; return unless $priority =~ /^(?:info|err|debug)$/; setlogsock( 'unix' ); openlog( $0, 'pid,cons', 'user' ); syslog( $priority, $msg ); closelog(); }
      Now it works very very well...Thank you! But I'm not sure is anymore "quick-and-dirty"...:-)