Date: Mon, 05 Jul 1999 18:36:58 -0500 From: Burke Gallagher <burke@mcs.net> To: dan.langille@dvl-software.com Cc: Anton Berezin <tobez@plab.ku.dk>, freebsd-questions@FreeBSD.org Subject: Re: running frequent cron perl scripts Message-ID: <4.1.19990705183047.00a739b0@pop.ce.mediaone.net> In-Reply-To: <199907052211.RAA09963@metis.host4u.net> References: <19990705232139.D8704@lion.plab.ku.dk> <19990705203750.BEYX282564.mta1-rme@wocker>
next in thread | previous in thread | raw e-mail | index | archive | help
I have to agree with Anton, the original perl script is awful. I also agree with his fixups 100%. Here is my cleaned up version. #!/usr/bin/perl # # Program Name: fetch2.cgi # # Nokia M10 address fetcher # # I hope that this program is useful to you. I make no claim as to this # software's stability or value. It is offered as is. Use at your own risk. require LWP; system "logger -i -t DYNDNS script start"; $username = "MadeHardBy"; # user name and password for the M10 $password = "Telecom"; $URL = "http://192.168.1.254/shell/show+ip+interfaces"; # Set the URL of the page containing the current IP for the M10 $RunIfNew = "/home/dan/dns_update.sh"; # name of a program to be run if IP changes $IPFilename = "myip.txt"; # name of the file that hold the previous IP address $ua = LWP::UserAgent->new(); $req = HTTP::Request->new( GET, $URL ); $req->authorization_basic( $username, $password ); $response = $ua->request( $req ); $response->content() =~ /inet ([\d.]+) netmask 0 peer/; $currentIP = $1; $oldIP = get_OldIP(); if ($ip .ne. $oldIP) { write_OldIP( $ip ); system $RunIfNew; system "logger -i -t DYNDNS The IP Address has changed to $ip"; } system "logger -i -t DYNDNS script stop"; ## End of program. sub get_OldIP { my $ip = ""; if (open( FILE, "<$IPFilename" )) { $ip = <FILE>; chomp( $ip ); close FILE; } return $ip; } sub write_OldIP( $ ) { my $ip = shift( @_ ); if (open( FILE, ">$IPFilename" )) { print FILE $ip; close FILE; } else { print "<STRONG>Error:</STRONG> couldn't write to file $IPFilename: $!\n"; } } Now here is a version that uses sockets directly. #!/usr/bin/perl # use Socket; use MIME::Base64; # ------------------------------------------------------------------------ # globals # ------------------------------------------------------------------------ $routerAddress = "192.168.1.254"; $username = "MadeHardBy"; $password = "Telecom"; $IPFilename = "myip.txt"; # ------------------------------------------------------------------------ # syslog # ------------------------------------------------------------------------ sub syslog( $ ) { my $msg = shift( @_ ); system("logger -i -t $msg"); } # ------------------------------------------------------------------------ # fetchM10Address # ------------------------------------------------------------------------ sub fetchM10Address() { my $encoded; my $httpRequest; my $httpResponse; my $sin $encoded = encode_base64( "$username:$password" ); $httpRequest = "GET /shell/show+ip+interfaces 1.1\r\n" . "Accept: text/*, text/html\r\n" . "Authorization: Basic $encoded\r\n"; $httpResponse = ""; socket( SH, PF_INET, SOCK_STREAM, getprotobyname('tcp') ) || die "can not create socket, $!\n"; $sin = sockaddr_in( 80, inet_aton($routerAddress) ); connect( SH, $sin ) || die "can not connect to router, $!\n";; send( SH, $httpRequest ); while (<SH>) { $httpResponse = $httpResponse . $_; } close( SH ); $httpResponse =~ m/inet ([\d.]+) netmask 0 peer/; return $1; } # ------------------------------------------------------------------------ # getLastIP # ------------------------------------------------------------------------ sub getLastIP() { my $OldIP = ""; if (open( FILE,"<$IPFilename" )) { $OldIP = <FILE>; chomp( $OldIP ); close FILE; } return $OldIP; } # ------------------------------------------------------------------------ # writeIP # ------------------------------------------------------------------------ sub writeIP( $ ) { $ip = shift( @_ ); if (open( FILE, ">$IPFilename" )) { print FILE $ip; close FILE; } else { print "Error: couldn't write to file $IPFilename: $!\n"; } } # ------------------------------------------------------------------------ # main # ------------------------------------------------------------------------ print "DYNDNS\n"; syslog( "DYNDNS start" ); $currentIP = FetchM10Address(); $lastIP = getLastIP(); print "currentIP = $currentIP, lastIP = $lastIP\n"; if ($currentIP .ne. $lastIP) { print "change detected\n"; writeIP( $currentIP ); system "/home/dan/dns_update.sh"; syslog( "The IP Address has changed to $currentIP" ); } syslog( "DYNDNS stoped" ); Burke Gallagher burke@mcs.net To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4.1.19990705183047.00a739b0>