meonkeys has asked for the wisdom of the Perl Monks concerning the following question:
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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Detecting when my ISP changes my IP
by BrowserUk (Patriarch) on Sep 22, 2002 at 05:51 UTC | |
|
Re: Detecting when my ISP changes my IP
by Kanji (Parson) on Sep 22, 2002 at 05:34 UTC | |
|
Re: Detecting when my ISP changes my IP
by gmpassos (Priest) on Sep 22, 2002 at 06:03 UTC | |
|
Re: Detecting when my ISP changes my IP
by zengargoyle (Deacon) on Sep 22, 2002 at 07:05 UTC | |
by Aristotle (Chancellor) on Sep 22, 2002 at 11:58 UTC | |
|
Re: Detecting when my ISP changes my IP
by Dr. Mu (Hermit) on Sep 23, 2002 at 06:19 UTC |