From owner-freebsd-questions Mon Jul 5 19:51:39 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 599BF15396 for ; Mon, 5 Jul 1999 19:51:33 -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 VAA25757; Mon, 5 Jul 1999 21:51:30 -0500 (CDT) Message-Id: <4.1.19990705214214.00a74b40@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 21:51:26 -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: <37816b0f.6e09.0@actrix.gen.nz> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG At 02:33 PM 7/6/99 +0000, Dan Langille wrote: >Burke, > >I've modified the script and added some debugging. Mostly because I don't know >what I'm doing. Here's the current script and the output. I have no idea= why >it hangs. > >I managed to borrow a perl manual at work and found more about the= parameters >to send. I added a zero after looking at man 2 send. I don't know if that >was correct or not. absolutely correct, I had to look that one up too. > >cheers. > >#!/usr/bin/perl ># > >use Socket; >use MIME::Base64; > ># ------------------------------------------------------------------------ ># globals ># ------------------------------------------------------------------------ > >$routerAddress =3D "192.168.1.254"; >$username =3D "MadeHardBy"; >$password =3D "Telecom"; > >$IPFilename =3D "myip.txt"; > ># ------------------------------------------------------------------------ ># syslog ># ------------------------------------------------------------------------ > >sub syslog( $ ) >{ > my $msg =3D shift( @_ ); > > system("logger -i -t $msg"); >} > ># ------------------------------------------------------------------------ ># FetchM10Address ># ------------------------------------------------------------------------ > >sub FetchM10Address() >{ > my $encoded; > my $httpRequest; > my $httpResponse; > my $sin; > > > >print "DYNDNS start\n"; > $encoded =3D encode_base64( "$username:$password" ); > $encoded =3D "$username:$password"; >print "DYNDNS 2nd\n"; > $httpRequest =3D "GET /shell/show+ip+interfaces 1.1\r\n" > . "Accept: text/*, text/html\r\n" > . "Authorization: Basic $encoded\r\n"; . "Authorization: Basic $encoded\r\n" . "\r\n"; we need another line here I forget that and http request must end with a blank line to seperate the headers from the data (we do not have). =20 >print "DYNDNS 3rd $httpRequest\n"; > $httpResponse =3D ""; > >print "DYNDNS 4th\n"; > socket( SH, PF_INET, SOCK_STREAM, getprotobyname('tcp') ) || die "can= not >create socket, $!\n"; >print "DYNDNS 5th\n"; > $sin =3D sockaddr_in( 80, inet_aton($routerAddress) ); >print "DYNDNS 6th\n"; > connect( SH, $sin ) || die "can not connect to router, $!\n"; >print "DYNDNS 7th\n"; > send( SH, $httpRequest, 0 ); this is correct ------------^ >print "DYNDNS 8th\n"; > while () > { >print "DYNDNS loop\n"; > $httpResponse =3D $httpResponse . $_; > } >print "DYNDNS 9th\n"; > close( SH ); > > $httpResponse =3D~ m/inet ([\d.]+) netmask 0 peer/; >print "DYNDNS 10th\n"; > return $1; >} > ># ------------------------------------------------------------------------ ># getLastIP ># ------------------------------------------------------------------------ > >sub getLastIP() >{ > my $OldIP =3D ""; > > if (open( FILE,"<$IPFilename" )) > { > $OldIP =3D; > chomp( $OldIP ); > close FILE; > } > > return $OldIP; >} > > ># ------------------------------------------------------------------------ ># writeIP ># ------------------------------------------------------------------------ > >sub writeIP( $ ) >{ > $ip =3D 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 =3D FetchM10Address(); >$lastIP =3D getLastIP(); > > >print "currentIP =3D $currentIP, lastIP =3D $lastIP\n"; > >if ($currentIP !=3D $lastIP) -----------------^ this is incorrect (as was the .ne.) !=3D compares numbers and we are comparing string so we need ne (no periods, my fortran background is= showing) >{ > print "change detected\n"; > > writeIP( $currentIP ); > system "/home/dan/dns_update.sh"; > syslog( "The IP Address has changed to $currentIP" ); >} > >syslog( "DYNDNS stoped" ); > > >$ perl get_ip.sh >DYNDNS >DYNDNS start >DYNDNS 2nd >DYNDNS 3rd GET /shell/show+ip+interfaces 1.1 >Accept: text/*, text/html >Authorization: Basic MadeHardBy:Telecom > >DYNDNS 4th >DYNDNS 5th >DYNDNS 6th >DYNDNS 7th >DYNDNS 8th >=F2^C >- >Dan Langille Here is the script i just debugged using my apache server (I don't have a Nokia M10). #!/usr/bin/perl # use Socket; use MIME::Base64; # ------------------------------------------------------------------------ # globals # ------------------------------------------------------------------------ $routerAddress =3D "192.168.1.254"; #$routerAddress =3D "127.0.0.1"; $username =3D "MadeHardBy"; $password =3D "Telecom"; $IPFilename =3D "myip.txt"; # ------------------------------------------------------------------------ # syslog # ------------------------------------------------------------------------ sub syslog( $ ) { my $msg =3D shift( @_ ); system("logger -i -t $msg"); } # ------------------------------------------------------------------------ # fetchM10Address # ------------------------------------------------------------------------ sub fetchM10Address() { my $encoded; my $httpRequest; my $httpResponse; my $sin; $encoded =3D encode_base64( "$username:$password" ); $httpRequest =3D "GET /shell/show+ip+interfaces 1.1\r\n" . "Accept: text/*, text/html\r\n" . "Authorization: Basic $encoded\r\n" . "\r\n"; $httpResponse =3D ""; socket( SH, PF_INET, SOCK_STREAM, getprotobyname('tcp') ) || die "can not create socket, $!\n"; $sin =3D sockaddr_in( 80, inet_aton($routerAddress) ); connect( SH, $sin ) || die "can not connect to router, $!\n";; $i =3D send( SH, $httpRequest, 0 ); print "$i bytes sent\n"; while () { $httpResponse =3D $httpResponse . $_; } print "$httpResponse\n"; close( SH ); $httpResponse =3D~ m/inet ([\d.]+) netmask 0 peer/; return $1; } # ------------------------------------------------------------------------ # getLastIP # ------------------------------------------------------------------------ sub getLastIP() { my $OldIP =3D ""; if (open( FILE,"<$IPFilename" )) { $OldIP =3D ; chomp( $OldIP ); close FILE; } return $OldIP; } # ------------------------------------------------------------------------ # writeIP # ------------------------------------------------------------------------ sub writeIP( $ ) { $ip =3D 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 =3D fetchM10Address(); $lastIP =3D getLastIP(); print "currentIP =3D $currentIP, lastIP =3D $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" ); To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message