If like me you are unfortunate enough to live in an area not covered by *any* form of broadband access and must use a modem this script may help if you need to know your latest dynamically assigned IP. I've configured a NAT/Firewall server and need to let several people know the latest IP so they can log in. The $to address is assigned to a mailist but YMMV. Any comments welcome.
#!/usr/bin/perl -w use strict; my $from = "foo\@bar.com"; my $to = "bar\@foo.com"; my $subject = "Latest IP address"; my @ifc = `/sbin/ifconfig ppp0`; chomp ($_ = $ifc[1]); /inet\ addr:(\d+\.\d+\.\d+\.\d+)\s+.*$/; open (MAIL, "|/usr/lib/sendmail -oi -t -odq") || die "Can't fork for s +endmail: $!\n"; print MAIL "From: $from\nTo: $to\nSubject: $subject\n\n"; print MAIL $1; close (MAIL);

Replies are listed 'Best First'.
•Don't use $1 without testing! (was Re: Getting the latest IP after dial-up)
by merlyn (Sage) on Aug 25, 2002 at 16:22 UTC
    /inet\ addr:(\d+\.\d+\.\d+\.\d+)\s+.*$/; ... print MAIL $1;
    I'd fail this in a code review. You've not assured a match on the regex, so the $1 there could be the previous $1, since the dollar vars are not updated on an unsuccessful match.

    At the very minimum, put "or die" after the match.

    -- Randal L. Schwartz, Perl hacker

      Thanks merlyn, the comments are valid and welcome. I'm rewriting the code with a filehandle to check and collect the old IP and only send a mail if different... I have much to learn.
Re: Getting the latest IP after dial-up
by Mr. Muskrat (Canon) on Aug 25, 2002 at 17:00 UTC

    How do I find out my hostname/domainname/IP address?

    How do I send mail? Well, I would have linked to perlfaq9 again for that except that it mentions using Mail::Mailer (which I could not even find using ppm3) or Net::SMTP (which I have only had limited success with on Windows). So I'm going to use Mail::Sendmail. After you have installed it, you should edit Mail::Sendmail.pm and change the line to show your actual SMTP server:
        'smtp'    => [ qw( localhost ) ],

    Combine everything together and what do we get?

    #!/usr/bin/perl -w use strict; use Socket; use Sys::Hostname; use Mail::Sendmail; my $from = "foo\@bar.com"; my $to = "bar\@foo.com"; my $subject = "Latest IP address"; my $host = hostname(); my $addr = inet_ntoa(scalar gethostbyname($host || 'localhost')); my %mail = (To => $to, From => $from, Subject => $subject, Message => $addr ); sendmail(%mail) or die $Mail::Sendmail::error; print "OK.\n", $Mail::Sendmail::log;

    Answer: A cross-platform solution!

      An interesting approach... I'd just finished refining my old code but now have something new to look at and play with, thank you very much Mr. Muskrat much appreciated!

        barrd, that is why am here! To help: myself by learning and others by sharing what I have learned.

        When I saw that you were using a *nix specific way of doing it, I knew that someone else would come along one day and ask how to do it on Windows. I could have written a Windows specific version without much effort but why? I didn't want to make it specific to any one OS.