Re: [SLUG] script to parse and compare an IP address

From: Dylan Hardison (dylanwh@gmail.com)
Date: Sat Apr 01 2006 - 17:07:37 EST


On 4/1/06, Sick Twist <thesicktwist@hotmail.com> wrote:
> >From: Paul M Foster <paulf@quillandmouse.com>
> >Reply-To: slug@nks.net
> >To: slug@nks.net
> >Subject: Re: [SLUG] script to parse and compare an IP address
> >Date: Sat, 01 Apr 2006 12:56:18 -0500
> >
> >Sick Twist wrote:
> >
> >>I am trying to write a script that parses an unknown IP address from a
> >>page of HTML, compares the IP address to a file and overwrites the file if
> >>the saved IP address is different. My question is, what language should I
> >>investigate to accomplish this? Is this able to be done with a bash script
> >>and command line utilities or is there another language like perl or
> >>python that would be better suited for this task? C has always been my
> >>game so I'm less familiar with the strengths and capabilities of some of
> >>the other tools out there. Thanks in advance!
> >>
> >
> >Let the language wars begin! ;-}
> >
> >If this is something that you could run from a web page, I'd suggest PHP.
> >Its syntax is the most similar to C, of the languages you mentioned.
> >
> >I've written code in Perl, but I no longer do. Perl is hard to read, and
> >has odd conventions for naming variables, depending on the type of the
> >variable and the context. However, for doing regexp work, Perl has the
> >simplest interface. It's built in to the language; completely integrated.
> >
> >Python is my choice for this kind of work. However, Python has some quirks,
> >too. First, it has almost no end-of-line delimiters and requires strict
> >indentation, a fact that causes jeers from its detractors. In addition, to
> >do regexp work you must include a statement at the beginning of the file to
> >pull in the regexp module; like an #include directive in C. The method of
> >doing regexp work is more complicated than Perl. But the language is
> >more... orthogonal?... than Perl. Perl's kinda down-and-dirty
> >it'll-figure-out-what-you-mean. Python is more rigorous.
> >Larry Wall (Perl creator) built Perl so that there many many ways to do the
> >same thing, some quite counter-intuitive. Guido Van Rossum (Python's
> >creator) wanted a language with the power of Perl, but with a more
> >regular/orthogonal/rigorous syntax. Fortunately, both these languages don't
> >require the same pre-declaration of variables that C does, and they do
> >garbage collection that C can't.
> >
> >These scripting languages drive me crazy because their syntax for certain
> >common tasks is just different enough from each other and C that you often
> >make stupid mistakes while coding. (else if, elseif, elif, elsif...) This
> >is one of the reasons I prefer PHP when I can use it-- it's close enough to
> >C syntax that I seldom use the wrong keyword, and I still have to terminate
> >lines with a semicolon (unlike Python).
> >
> >If this is a one-shot deal, you might just consider using grep/sed and
> >bash. That's got to be easier than mastering the intricacies of a whole new
> >language.
> >
> >Paul
> >
> >--
> >Paul M. Foster
>
> Yep, one-shot deal. Basically I want a script that will automatically log-in
> to my router (via curl or wget) and download the status page. Then I want to
> parse the page to obtain the router's external IP address and compare it
> with one that was saved previously to determine if it has changed. If so,
> the script should send me an e-mail with the current IP address. This is a
> poor man's substitute for a static IP :)

Aaah, you need passwords? Here, is updated version of script that
handles passwords and usernames (for http basic authentication).
Only thing I changed was fetch_ip.

#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
use HTTP::Status;
use IO::File;

my $url = 'http://whatismyip.com';
my $username = 'foobar';
my $password = 'password';
my $file = 'myipfile';

my $ip = fetch_ip($url, $username, $password);
my $old_ip = fetch_old_ip($file);

update_ipfile($file, $old_ip, $ip);

sub fetch_ip {
        my ($url, $username, $password) = @_;
        my $ua = new LWP::UserAgent;
        my $req = new HTTP::Request (
                GET => $url,
        );
        $req->authorization_basic($username, $password);
        my $resp = $ua->request($req);

        if ($resp->code != RC_OK) {
                die "Request for $url failed: ", $resp->message, " (code: ",
$resp->code, ")\n";
        }

        my $html = $resp->content;
        if ($html =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/s) {
                my $ip = $1;
                print "Found IP: $ip\n";
                return $ip;
        } else {
                die "Cannot find ip address on $url\n";
        }
}

sub fetch_old_ip {
        my ($file) = @_;

        # If $file exists, we open it and read the first line.
        if (-e $file) {
                my $fh = new IO::File($file, 'r') or die "Cannot open $file: $!";
                my $line = $fh->getline;
                chomp $line;
                $fh->close;
                
                return $line;
        } else {
                # Return something that cannot be an IP.
                return "(nada)";
        }
}

sub update_ipfile {
        my ($file, $old_ip, $ip) = @_;
        if ($old_ip ne $ip) {
                print "Changing IP to $ip (was $old_ip)\n";
                my $fh = new IO::File($file, 'w') or die "Cannot open $file for writing: $!";
                $fh->print($ip);
                $fh->close;
        } else {
                print "Old IP and new IP are the same ($ip).\n";
        }
}

-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 20:28:08 EDT