From owner-freebsd-questions Mon Jul 5 16:37: 6 1999 Delivered-To: freebsd-questions@freebsd.org Received: from elmls02.ce.mediaone.net (elmls02.ce.mediaone.net [24.131.128.27]) by hub.freebsd.org (Postfix) with ESMTP id 8A3A614D54 for ; Mon, 5 Jul 1999 16:37:01 -0700 (PDT) (envelope-from burke@mcs.net) Received: from fatman2 (el01-24-131-147-32.ce.mediaone.net [24.131.147.32]) by elmls02.ce.mediaone.net (8.8.7/8.8.7) with SMTP id SAA12750; Mon, 5 Jul 1999 18:36:58 -0500 (CDT) Message-Id: <4.1.19990705183047.00a739b0@pop.ce.mediaone.net> X-Sender: johnburke@pop.ce.mediaone.net X-Mailer: QUALCOMM Windows Eudora Pro Version 4.1 Date: Mon, 05 Jul 1999 18:36:58 -0500 To: dan.langille@dvl-software.com From: Burke Gallagher Subject: Re: running frequent cron perl scripts Cc: Anton Berezin , freebsd-questions@FreeBSD.org In-Reply-To: <199907052211.RAA09963@metis.host4u.net> References: <19990705232139.D8704@lion.plab.ku.dk> <19990705203750.BEYX282564.mta1-rme@wocker> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 = ; chomp( $ip ); close FILE; } return $ip; } sub write_OldIP( $ ) { my $ip = shift( @_ ); if (open( FILE, ">$IPFilename" )) { print FILE $ip; close FILE; } else { print "Error: 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 () { $httpResponse = $httpResponse . $_; } close( SH ); $httpResponse =~ m/inet ([\d.]+) netmask 0 peer/; return $1; } # ------------------------------------------------------------------------ # getLastIP # ------------------------------------------------------------------------ sub getLastIP() { my $OldIP = ""; if (open( FILE,"<$IPFilename" )) { $OldIP = ; 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