Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 05 Jul 1999 21:51:26 -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.19990705214214.00a74b40@pop.ce.mediaone.net>
In-Reply-To: <37816b0f.6e09.0@actrix.gen.nz>

next in thread | previous in thread | raw e-mail | index | archive | help
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 (<SH>)
>   {
>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<FILE>;
>      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 (<SH>)
   {
      $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 <FILE>;
      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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4.1.19990705214214.00a74b40>