My ISP changes my internet address every now and then (I have cable modem, eth0 gets an IP from the cable modem via DHCP). I want to detect when this happens, so I wrote a little program to persist the data and check if it has changed. This program is meant to be run by cron, and will probably email any output to me.

Is there an easier way to detect what internet address has been assigned to eth0? I tried to install Net::Interface (supposed to replace ifconfig), but it wouldn't compile.

Here's what I'm using now:

#!/usr/bin/perl -w use English; use strict; use vars qw( $INET_ADDR_FILE ); $INET_ADDR_FILE = "/home/adamm/.current_inet_addr"; die "only currently implemented for linux." unless ($OSNAME eq 'linux' +); ### MAIN my ($old_inet_addr, $found_old_addr_file) = read_saved_inet_addr(); my $new_inet_addr = read_new_inet_addr(); if (!$found_old_addr_file) { write_new_inet_addr($new_inet_addr); print "Current IP address ($new_inet_addr) stored to disk.\n"; } elsif ($old_inet_addr ne $new_inet_addr) { write_new_inet_addr($new_inet_addr); print <<OutputEnd; Change in IP address detected. New IP stored to disk. Old IP: $old_inet_addr New IP: $new_inet_addr OutputEnd } ### SUBS sub read_saved_inet_addr { my ($fh, $oldfile_found, $old_addr); open($fh, "< $INET_ADDR_FILE") ? $oldfile_found = 1 : warn "couldn't open $INET_ADDR_FILE. Hopefully this is the first + run.\n"; if ($oldfile_found) { $found_old_addr_file = 1; local $INPUT_RECORD_SEPARATOR = undef; $old_addr = <$fh>; $old_addr =~ s/\r|\n//; close($fh) or die "couldn't close $INET_ADDR_FILE"; } return ($old_addr, $oldfile_found); } sub read_new_inet_addr { local $INPUT_RECORD_SEPARATOR = undef; my $ifconfig_output = `/sbin/ifconfig eth0`; my ($new_inet_addr) = ($ifconfig_output =~ m/inet\saddr:(.+?)\s/); return $new_inet_addr; } sub write_new_inet_addr { my $new_addr = shift; my $fh; open($fh, "> $INET_ADDR_FILE") or die "couldn't create $INET_ADDR_FI +LE"; print $fh $new_addr,"\n"; close($fh) or die "couldn't close $INET_ADDR_FILE"; }


---
"A Jedi uses the Force for knowledge and defense, never for attack."

In reply to Detecting when my ISP changes my IP by meonkeys

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.