Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

What is my IP address?

by Massyn (Hermit)
on Apr 25, 2003 at 09:26 UTC ( #253107=perlquestion: print w/replies, xml ) Need Help??

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


In a particular app, I'm using the `ipconfig` command to determine the IP address of a Windows machine. I'd like to use some perl module to do that, as I like the code to be portable to different OSs.

Can the wise ones point me to some code that will reveal the IP address(es) to me? Please note, I'm not doing a CGI, so using $ENV{REMOTE_ADDR} won't solve my problem.


#!/ The early worm gets caught by the bird.

Replies are listed 'Best First'.
Re: What is my IP address?
by broquaint (Abbot) on Apr 25, 2003 at 09:34 UTC
    Let's see what ol' perldoc has to say about the matter ...
    shell> perldoc -q 'ip address'
    Found in /usr/lib/perl5/5.6.1/pod/perlfaq9.pod
    How do I find out my hostname/domainname/IP address?

    The normal way to find your own hostname is to call the "`hostname`" program. While sometimes expedient, this has some problems, such as not knowing whether you've got the canonical name or not. It's one of those tradeoffs of convenience versus portability.

    The Sys::Hostname module (part of the standard perl dis- tribution) will give you the hostname after which you can find out the IP address (assuming you have working DNS) with a gethostbyname() call.

    use Socket; use Sys::Hostname; my $host = hostname(); my $addr = inet_ntoa(scalar gethostbyname($host || 'localhost'));
    Probably the simplest way to learn your DNS domain name is to grok it out of /etc/resolv.conf, at least under Unix. Of course, this assumes several things about your resolv.conf configuration, including that it exists.

    (We still need a good DNS domain name-learning method for non-Unix systems.)

    Well, there you have it :)


Re: What is my IP address?
by Abigail-II (Bishop) on Apr 25, 2003 at 09:54 UTC
    Well, I think it should be pointed out that machines do not have IP addresses. interfaces have IP addresses, and often more than one - and not necessarely unique. My home box for instance has 2 ethernet cards, but 4 interfaces, each with a different IP address (the interfaces are lo, eth0, eth1 and ppp0, with the IP addresses,, and Well, in fact there are 6 interfaces, but two of them don't have an IP address.

    If you want something portable, your best chance is probably to just return


Re: What is my IP address?
by mce (Curate) on Apr 25, 2003 at 09:42 UTC

    type ipconfig in the search box, and you'll quickly come up with IO::Interface.

    I hope this helps,
    Dr. Mark Ceulemans
    Senior Consultant
    BMC, Belgium

Re: What is my IP address?
by BrowserUk (Patriarch) on Apr 25, 2003 at 10:04 UTC

    If its strictly the local machine IP then you could use Tye's Win32API::Registry and grab it from HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\NdisWan4\Parameters\Tcpip\DchpIPAddress. That key may not be completely correct under some versions of windows, but it should be fairly easy for you to find yours with regedit.

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
Re: What is my IP address?
by Biker (Priest) on Apr 25, 2003 at 10:04 UTC

    You've already got some good answers above. I'd just like to add some thoughts to your approach.

    Since you say you want to remain portable, I make the assumption that you're not 100% sure of what will be your target computer(s) for your application. (With target computer I here mean the computer where your application is going to be executed.)

    Remember that in the target computer(s), you may have more than one network card, each with potentially more than one IP address. Even if you think you may assume only one network card in the target computer(s), a card may have more than one address.

    You will need to start with defining what you meen by "the IP address", primarily which IP address.
    Then you'll have to think of how to behave when the computer has multiple IP addresses.

    Everything went worng, just as foreseen.

Re: What is my IP address?
by zengargoyle (Deacon) on Apr 25, 2003 at 10:25 UTC

    just some more to think about...

    see checking my default gateway. if you can find a default gateway/netmask then the local IP on the same network would be a good choice.

    just don't forget to add an option or configuration setting to specify the correct IP if your searching doesn't pick the address the user would like to use.

    if you're checking for net connectivity you could just create a socket and bind it to a foreign address ( for instance) and get the local address from the socket. (i think that might work)

Re: What is my IP address?
by Massyn (Hermit) on Apr 25, 2003 at 11:48 UTC
    Thanks guys! This surely points me to the right solution!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2022-05-27 06:37 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (94 votes). Check out past polls.