Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Public IP Address

by Anonymous Monk
on Nov 10, 2006 at 23:53 UTC ( [id://583436]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All, I am wanting a script that will return my public IP address. The script will be running on a Windows machine that is behind a firewall. The best solution I have found uses the following code
#!/usr/bin/perl use LWP::Simple; $content = get("http://checkip.dyndns.org");

Does anyone have any better methods?
I can not use gethostbyname(Sys::Hostname::ghname())as it will only return my private network address.

The output from $content is as follows
<html><head><title>Current IP Check</title></head><body>Current IP Add +ress: 123.132.12.23</body></html>

Any ideas on how to sort so that $content is left with only the ip address?
Thank you for your time

Replies are listed 'Best First'.
Re: Public IP Address
by Joost (Canon) on Nov 11, 2006 at 00:01 UTC
Re: Public IP Address
by NetWallah (Canon) on Nov 11, 2006 at 05:08 UTC
    You may want to add additional external address providers, in case dyndns is unavailable.

    Here are a few alternatives:

    http://www.ipchicken.com/
    http://www.whatismyip.com

    Here is some parsing code that will generically work on all 3 : (It could be made more paranoid, or cleverer, but this is easy to read):

    my ($IP_addr)=$content=~m/(\d+\.\d+\.\d+\.\d+)/;
    Better still, use Regexp::Common: (untested)
    use Regexp::Common qw /net/; my ($IP_addr)= $content=~ /$RE{net}{IPv4}/ ;

         "A closed mouth gathers no feet." --Unknown

Re: Public IP Address
by b10m (Vicar) on Nov 14, 2006 at 08:43 UTC

    Our good brother Juerd has the easiest solution posted on his site:

    use LWP::Simple; my $ip = get "http://tnx.nl/ip";
    ... also check out the awesome CPAN/perldoc redirect service on his site!
    --
    b10m

    All code is usually tested, but rarely trusted.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://583436]
Approved by BaldPenguin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 03:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found