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

This is probably a simple question but its the simple ones that get you. I'm looking to write a small program that gets the ip address of the host machine that the program is ran on. So far I have the following:
$hostname = `hostname`; print "hostname is $hostname\n"; $source = `ypcat hosts | grep '$hostname$'`; $source =~ /\d+\.\d+\.\d+\.\d/; print "source is $source\n";
This does not work though. The backticks method of assigning program outputs to variables works for hostname but not for source. What am I doing wrong?

Replies are listed 'Best First'.
Re: ip address get problem
by b10m (Vicar) on Mar 22, 2004 at 15:03 UTC

    Getting an IP address is even a Perl FAQ1 :)

    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 || 'localho +st'));

    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.)

    1 perldoc -q "IP address"

    --
    b10m

    All code is usually tested, but rarely trusted.
      Thanks for that but that code does not seem to work for me. The active ip address on my host machine is on the hme0 interface. The code you supplied got me the ip address on another interface. This is why I was trying to go through the hosts file to get my ip address. This has the primary ip address on the host. The trouble is that my code to extract from this file is not working. Would you know what is wrong with this code? I can't figure what is happening:
      $hostname = `hostname`; print "hostname is $hostname\n";
      This works to get the hostname. It is the following bit of code that does not work to translate that hostname into an ip address:
      $source = `ypcat hosts | grep '$hostname$'`; $source =~ /\d+.\d+.\d+.\d/; print "source is $addr\n";
        The active ip address on my host machine is on the hme0 interface. The code you supplied got me the ip address on another interface.
        This is exactly why questions like "finding the IP address of my machine" are meaningless. It also means any solution to "finding a IP address of my machine" that use `hostname` are not very useful.

        Machines don't have IP addresses. Interfaces have IP addresses. An interface can have 0, 1 or more than one IP address, and an IP address can be shared by more than one interface, which may be on the same machine, or on different machines. Machines can have 0, 1 or more interfaces.

        Machines have hostnames, typically one. There is NO relationship between a machines hostname, and the IP addresses of their interfaces, unless the local administrator happens to set up things that way. My laptop has a hostname, and it always has the same hostname. But the IP address of its non-loopback interface depends on which network I plug the machine in.

        Abigail

Re: ip address get problem
by Happy-the-monk (Canon) on Mar 22, 2004 at 15:01 UTC
Re: ip address get problem
by markd (Acolyte) on Mar 22, 2004 at 15:29 UTC
    oops. Don't mind a word of my last mail. It did work. I just can't type thats all. Thank you for your help.