Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Getting net addy on localhost?

by r.joseph (Hermit)
on Nov 27, 2001 at 09:36 UTC ( #127730=perlquestion: print w/ replies, xml ) Need Help??
r.joseph has asked for the wisdom of the Perl Monks concerning the following question:

Quick question - I want to get the correct IP address (and if possible, hostname) for my computer (ie: localhost)...this:
($a, $b, $c, $d) = unpack('C4', ((gethostbyname('localhost'))[4])[0]); print "$a.$b.$c.$d\n";
Always returns 127.0.0.1 - how do I get my real IP (which is currently 206.62.155.15)? Thanks!

r. j o s e p h
"Violence is a last resort of the incompetent" - Salvor Hardin, Foundation by Issac Asimov

Comment on Getting net addy on localhost?
Download Code
Re: Getting net addy on localhost?
by rob_au (Abbot) on Nov 27, 2001 at 09:40 UTC
    The reason that this code is returning 127.0.0.1 is that you are requesting the resolution of the name localhost, which should always be your loopback adapter address - If you want it to resolve your external IP address, you should pass the external hostname you have been assigned. If this is dynamic in nature then you may want to look at other solutions such as Net::Pcap which allow network interfaces to be iterated.

    Update #1 - I did a bit of digging on a hunch and if you have a known external address that you can connect to, you could make a socket connection to it and then call perlfunc:getsockname on the socket handle (or sockname if you use IO::Socket) to return the packed address of this end of the socket. eg.

    use Socket; $mysockaddr = getsockname(SOCK); ($port, $myaddr) = sockaddr_in($mysockaddr); printf "Connected from %s [%s]\n", scalar gethostbyaddr($myaddr, AF_INET), inet_ntoa($myaddr);

    Update #2 - And the prize goes to converter for the link to IO::Interface in this post

     

    Ooohhh, Rob no beer function well without!

Re: Getting net addy on localhost?
by IlyaM (Parson) on Nov 27, 2001 at 09:41 UTC
    localhost should always resolve to 127.0.0.1. If it doesn't you have misconfigured computer. If you are on UNIX-like system you can parse output of ifconfig to get your real IP.
Re: Getting net addy on localhost?
by kwoff (Friar) on Nov 27, 2001 at 09:49 UTC
    You can use Net::hostent gethost() to get $h->name, which would be the outside name (not localhost), then use gethost() again on that outside name and the address is in inet_ntoa($h->addr). Look at `perldoc Net::hostent`.
      Nifty idea, but the problem with this approach is that you still need to know the world-resolvable name of the machine. You cannot call Net::hostent::gethost() without a host name to resolve. eg.

      perl -MNet::hostent -e '$host = gethost(); print $host->name, "\n"' Not enough arguments for Net::hostent::gethost at -e line 1, near "()" Execution of -e aborted due to compilation errors.

       
      Ooohhh, Rob no beer function well without!
        And let's not get into the fact that you might have multiple IP addresses.

        You never said what platform you are on either. If UN*Xy the easiest pure-perl is to parse /etc/sysconfig/network-scripts/ifcfg-*. You might steal the code from http://www.webmin.com/webmin/

        --
        perl -p -e "s/(?:\w);([st])/'\$1/mg"

Re: Getting net addy on localhost?
by converter (Priest) on Nov 27, 2001 at 10:39 UTC
Re: Getting net addy on localhost?
by dws (Chancellor) on Nov 27, 2001 at 10:43 UTC
    I want to get the correct IP address (and if possible, hostname) for my computer ... how do I get my real IP (which is currently 206.62.155.15)?

    Since you say "currently", I'm going to assume that your address is assigned dynamically by your ISP/DSL/Cable provider.

    The way I attack this problem is by periodically pinging a hidden CGI on my external web site. The CGI records the REMOTE_ADDR that pinged it, and responds with that REMOTE_ADDR. Then, from work, I can hit another hidden page on my site that tells me how recently my home machine reported in, and what IP address it used. (My DSL provider will drop the link after a few minutes of inactivity.) If the connection is still up, I can then reach it from the outside world.

(tye)Re: Getting net addy on localhost?
by tye (Cardinal) on Nov 27, 2001 at 11:12 UTC

    This question gets asked quite a bit. You'd think there would be a better answer by now, eh?

    Well, the reason that I don't think there is a really great answer is that, eventually, after lots of wrangling, 95% of the time the reason that this information was requested in the first place was due to a misunderstanding of how TCP/IP works, or something similar.

    Asking for "your own" IP address is pretty much guaranteed to give you a "solution" that doesn't work in quite a few situations.

    So you might want to tell us why you want this because most of the time there is a better solution that doesn't involve solving this particular problem. (:

            - tye (but my friends call me "Tye")
Re: Getting net addy on localhost?
by Dr. Mu (Hermit) on Nov 27, 2001 at 11:54 UTC
    I've used Net::Domain for this very purpose, and so far it's worked okay for me on single-interface boxes. And that's really the only time such a "Who am I?" question makes any sense, because IPs are not attached to computers but to interfaces. A computer acting as a gateway between two networks, for example, will have two interfaces and, hence, two associated IP addresses. Which one is the "computer's IP address"? Well, the question just doesn't make sense. And I don't know what Net::Domain would do in such a case, because I haven't tried it.
Re: Getting net addy on localhost?
by blakem (Monsignor) on Nov 27, 2001 at 13:16 UTC
    You've gotten lots of perl solutions, so let me offer a non-perl one. If you have GNU hostname on your machine1, use the -i option to get the machines IP address.
    $ hostname -i 63.150.14.148
    1 any flavor of linux or other machine with the standard set of GNU tools installed.

    -Blake

Re: Getting net addy on localhost?
by r.joseph (Hermit) on Nov 30, 2001 at 10:01 UTC
    Hey everyone, thanks for all the help! In response to some of your questions about my specifics:

    Slackware Linunx
    IP Obtained from DHCP server
    Reason for needing IP: I am running a streaming Icecast server from my computer (ssshhhhh, don't tell the net admins :), and I want to, everytime I restart, have my computer automagicaly contact a script I write on my webserver, which then updates my page, telling everyone where to connect for my music.

    Again, thanks for all the help!

    r. j o s e p h
    "Violence is a last resort of the incompetent" - Salvor Hardin, Foundation by Issac Asimov

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-08-20 13:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (113 votes), past polls