Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 6 Jul 1999 08:34:27 +1200
From:      "Dan Langille" <junkmale@xtra.co.nz>
To:        Anton Berezin <tobez@plab.ku.dk>, Burke Gallagher <burke@mcs.net>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: running frequent cron perl scripts
Message-ID:  <19990705203750.BEYX282564.mta1-rme@wocker>
In-Reply-To: <19990705170738.D5735@lion.plab.ku.dk>
References:  <19990705111124.ZBJY282564.mta1-rme@wocker>; from Dan Langille on Mon, Jul 05, 1999 at 11:08:02PM %2B1200

next in thread | previous in thread | raw e-mail | index | archive | help
On 5 Jul 99, at 17:07, Anton Berezin wrote:

> On Mon, Jul 05, 1999 at 11:08:02PM +1200, Dan Langille wrote:
> > I'm running cron job to start up a perl script.  The script accesses the
> > ADSL modem to see if the external IP address has changed.  If it has, it
> > runs another job to update the DNS.  The ADSL modem has an http
> > interface which has the details I need.
> 
> > The perl script takes about 35 seconds to run on this old 486.  But I'm
> > guessing that most of that is overhead.  The first bit of the script is:
> 
> > #!/usr/bin/perl
> > $| = 1;
> > use CGI;
> > use CGI::Carp (fatalsToBrowser);
> > require LWP;
> 
> > Is there a way to speed this process up?  The above bits seem to take up
> > most of the time.
> 
> You should definitely remove use CGI::Carp here.  And use CGI - almost
> definitely.  Which capabilities of CGI.pm do you need, anyway?

Ummm, errr, I don't know.

> I guess that $| = 1 is alright here, but...  Depending on the amount of
> output to STDOUT this script does, this statement can considerably slow
> things down.
> 
> Are you doing intelligent things with modem's http interface?  In most
> cases, LWP::Simple is enough.

Ummm, errr, I don't know.
 
> And finally, the fact you presented us with just a few first lines of your
> script suggests that you think that the startup overhead is the major
> slowing factor.  Are you sure of it?  I mean, judging from your use of
> CGI::Carp in non-cgi environment, you are not very good in Perl, so the
> possibility that script does something in, uhmm, not the most efficient
> fashion is far from remote.  :-)  Show us more...

Really?  Can ya tell?  Is it that obvious?  <grin>

Yes, this is my first script.  And it's not even mine.  I obtained it from 
someone else who wrote it for their ADSL connection.  All I've done is 
modify it slightly.

The script obtains the IP address from the modem, compares it to the 
previously obtained address, then optionally calls a routine if the IPs 
have changed.

cheers.

#!/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.

system("logger -i -t DYNDNS script start");

$| = 1;
use CGI;
use CGI::Carp (fatalsToBrowser);
require LWP;

## Set the username and password for the M10
$user = "MadeHardBy";
$pass = "Telecom";

## Set the URL of the page containing the current IP for the M10
$URL = "http://192.168.1.254/shell/show+ip+interfaces";

## Set the left and right hand side of the Regular Expression that will 
find
## the IP within the page
$lhs = "inet ";
$rhs = " netmask 0 peer";

##  set the following to the name of a program you want run if its a new IP
$RunIfNew = "/home/dan/dns_update.sh";

$IPFilename = "myip.txt";
$ua = LWP::UserAgent->new;
$req = HTTP::Request->new(GET,$URL,$h);
$req->authorization_basic($user,$pass);
$page = $ua->request($req);
$page->content() =~ /$lhs(\d*?\.\d*?\.\d*?\.\d*?)$rhs/;
$ip = $+;
##&page_header;
&get_OldIP;

if ($ip !~ ?^$OldIP$?) {
##    print "<B>NEW ";
    &write_OldIP;
}
##print "$ip";
##&page_footer;
if ($ip !~ ?^$OldIP$?) {
    exec $RunIfNew;
##    print "NOTE: IP has changed.\n";
        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{
    if (open(FILE,"<$IPFilename")) {
        $OldIP = <FILE>;
        close FILE;
    } else {
        &write_OldIP;
    }
}

sub write_OldIP{
    if (open(FILE,">$IPFilename")) {
        print FILE $ip;
        close FILE;
    } else {
        print "<STRONG>Error:</STRONG> couldn't write to file $IPFilename: 
$!\n";
    }


}

sub page_header{
  print<<HTML;
   <HTML><HEAD><TITLE>Nokia M10 IP Address Finder</TITLE></HEAD>
    <BODY BGCOLOR="#FFFFFF">
    <CENTER><FONT SIZE=5 FACE=ARIAL>
HTML
}

sub page_footer{
  print<<HTML;
  </FONT>
  </CENTER><P>
  </BODY></HTML>
HTML
}
--
Dan Langille - DVL Software Limited
The FreeBSD Diary     - http://www.FreeBSDDiary.org/freebsd/
NZ FreeBSD User Group - http://www.nzfug.nz.freebsd.org/
The Racing System     - http://www.racingsystem.com/racingsystem.htm


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?19990705203750.BEYX282564.mta1-rme>