http://www.perlmonks.org?node_id=62055

dusk has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a small script to automatically update the IP that a dynamic DNS points to (for myip.org).

The most efficient way that I can think of is to execute /sbin/ifconfig ppp0, and get the inet address, and store it in a scalar for processing

for example:

my $account = "blah"; my $password = "blah"; my $ip = exec "/sbin/ifconfig ppp0"; get ("http://www.myip.org/cgi-bin/Update.py?id=$account&pwd=$password& +hostname=all&ip=$ip");

However, in order for this to work, I need the IP in the URL to `get'.

Any assistance will be appreciated :)

Replies are listed 'Best First'.
Re: catching output
by AgentM (Curate) on Mar 04, 2001 at 02:22 UTC
    Use backticks to capture STDOUT from the program.
    my $captured=`/sbin/ifconfig ppp01`;
    Several other modules are also well suited to the purpose, for example, the IPC::Open series. By the way, you should familiarize yourself with the differences bewteen fork, exec, and system. The exec above completely nukes your program and runs ifconfig instead at that point it is called.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: catching output
by mr.nick (Chaplain) on Mar 04, 2001 at 03:16 UTC
    Something like this should fit your bill:
    my $account="blah"; my $password="blah2"; my $ip=`/sbin/ifconfig ppp0`; if ($ip=~/inet addr:(\d+\.\d+\.\d+\.\d+)/) { get ("http://www.myip.org/cgi-bin/Update.py?id=$account&pwd=$p +assword&hostname=all&ip=$1"); }
Re: catching output
by vladdrak (Monk) on Mar 04, 2001 at 14:04 UTC
    I don't know about you guys, but my ifconfig output isn't all that clean.. how about:
    #!/usr/bin/perl -w use strict; use LWP::Simple qw(get); my $acct="123"; my $pass="123"; my ($ip,$url); my $ifconfig=`/sbin/ifconfig eth0`; if ($ifconfig=~/inet addr\:((?:\d{1,3}\.){3}\d{1,3})/) { $ip=$1; print "ip: $ip\n" } else { die "Couldn't grab ip from: $ifconfig\n"; } print "Updating.."; if ($got=get("http://www.myip.org/cgi-bin/Update.py?id=$acct&pwd=$pass +&hostname=all&ip=$ip")) { if $got=~/SomeTextFromThePageThatWouldConfirmSuccess/) { print "success.\n"; } else { die "failed: $got\n"; } } else { die "failed: $!\n"; }