Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Finding your IP address with Perl

by $ENV{REMOTE_USER} (Novice)
on Dec 04, 2000 at 07:01 UTC ( [id://44734]=perlquestion: print w/replies, xml ) Need Help??

$ENV{REMOTE_USER} has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone have a good portable method of finding the IP address of the system you are on besides running ifconfig or ipconfig against a regex?

Sys::Hostname doesn't seem to be accurate on any of the boxes I've tested on. Since the machines will be in a DHCP environment where I have no control over DNS, it's rather tricky. I would think that if you have a socket open, and you are sending and recieving data, that there would be an easy method of determining the IP address you were using.

If I have to use a regex, I suppose that I can but I want to make this as bullet proof as possible.

Replies are listed 'Best First'.
Re: Finding your IP address with Perl
by chromatic (Archbishop) on Dec 04, 2000 at 07:41 UTC
    The Cookbook suggests Sys::Hostname then gethostbyname (or the POSIX module's uname()):
    use Sys::Hostname; my $hostname = hostname(); my $ip = gethostbyname($hostname);
    (Untested, but pretty much verbatim from the book.)

    The POSIX::uname() method looks like:

    use POSIX qw( uname ); my $hostname = (uname())[1]; # the rest as above
    On systems that don't implement any part of POSIX, you'll have ... other... results. You might also try reverse resolving any IP address you get with gethostbyaddr.
      Sys::Hostname (on linux) never seems to give me the DNS name I was assigned by the DNS server so the gethostbyname() call won't work. It does seem to work on win32 though.
        DNS name I was assigned by the DNS server

        err... assigned by the DHCP server..
Re: Finding your IP address with Perl (Sys::HostIP)
by ybiC (Prior) on Dec 04, 2000 at 07:47 UTC
    I've not used it myself yet, but CPAN module Sys::HostIP appears to be what you're looking for.   From the module source:
    NAME Sys::HostIP - Try really hard to get IP addr. SYNOPSIS use Sys::HostIP; $ipaddr= hostip; # get (text) dotted-decimal ip use IO::Socket; use Sys::Hostname; # Have to try non-cannonical name first, since there # is a better chance the computer knows itself than # of DNS knowing it. if($^O eq 'MSWin32') # check ipconfig.exe # (Which does all the work of checking the registry, # probably more efficiently than I could.) # check nbtstat.exe # (Which does all the work of checking WINS, # more easily than Win32::AdminMisc::GetHostAddress().) # check /etc/hosts entries # check /etc/lmhosts entries # (It will only be here if the file has been modified since th +e # last WINS refresh, which is unlikely, but might as well try. +) elsif($^O=~ /IX|ux/i) # check /etc/hosts entries # last resort: ping (which can be very slow)
    Ya just gotta love cpan.
        cheers,
        Don
        striving for Perl Adept
        (it's pronounced "why-bick")
      This module is actually doing what I was reluctant to do which was matching the IP address from `ipconfig` or the /etc/hosts file (depending on the OS) if it doesn't find the IP address based on what Sys::Hostname finds (which is incorrect).

      Oh well, it works.
        I'm glad it works, also curious - why the reluctance?

        BTW, your script sounds like a good candidate for Snippets.   Dunno 'bout other monks, but I'd benefit from seeing it.
            cheers,
            Don
            striving for Perl Adept
            (it's pronounced "why-bick")

      Thanks! This is exactly what I needed. It seems to work very well on the systems I've tested so far.
(crazyinsomniac) Re: Finding your IP address with Perl
by crazyinsomniac (Prior) on Dec 04, 2000 at 11:28 UTC
    Got this from How do I find out my hostname/domainname/IP address? and it worked.
    use Socket; use Sys::Hostname; my $hostname = hostname(); my $name ='yahoo.com'; my $ip = inet_ntoa(scalar(gethostbyname($name)) || 'localhost'); my $myip = inet_ntoa(scalar(gethostbyname($hostname))); print $ip, "\n", $myip; __END__ sample output: 216.115.108.245 4.52.23.253

    "cRaZy is co01, but sometimes cRaZy is cRaZy".
                                                          - crazyinsomniac

Re: Finding your IP address with Perl
by Maclir (Curate) on Dec 04, 2000 at 07:52 UTC
    I have to agree with Chromatic's reply. I would post the working code I have - but for some strange reason my terminal emulation program refuses to work. I use the cookbook solution on Win 98 pc's, in a DHCP environment.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-03-19 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found