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

From: Sick Twist (thesicktwist@hotmail.com)
Date: Sat Apr 01 2006 - 18:25:56 EST


>From: "Dylan Hardison" <dylanwh@gmail.com>
>Reply-To: slug@nks.net
>To: slug@nks.net
>Subject: Re: [SLUG] script to parse and compare an IP address
>Date: Sat, 1 Apr 2006 17:07:37 -0500
>
>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";
> }
>}
>

Dylan, it works perfectly with my Linksys RT31P2. Thanks a heap!

Would you like to specify a license for it? ;)

-Jonathon

-----------------------------------------------------------------------
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:11 EDT