SPUG: Get the local IP address?

Bill Campbell bill at celestial.com
Wed Jul 20 15:18:41 PDT 2005

On Wed, Jul 20, 2005, Fred Morris wrote:
>At 2:44 PM 7/20/05, Atom Powers wrote:
>>I'm trying to write a script what will give me the host name ( from
>>DNS ) for whatever machine it is run on. The stumbling block is
>>getting the local IP address.
>Hate to throw a spanner in the works, but machines can have multiple
>network cards; it is unlikely that they will all have the same address. It
>is also possible for a single card to be multi-homed.
>OS-dependent answers include looking for a file /etc/HOSTNAME. Also, what
>does hostname -i return? How does that compare to what plain old hostname
>returns (do they translate to one another)? (on Linux)
>That's another common problem: systems which are misconfigured. Very
>common, biggest support PITA for systems which are sensitive to this sort
>of issue that you can possibly imagine.
>Or, how about the systems which don't even know who they are until they ask
>a DNS server? Yes, they're out there (this is common on HP-UX)!
>Is there a Perlish solution to this rather complex problem? Inquiring minds
>want to know!

I handle this with a module I wrote that scans the output of the ifconfig
command with appropriate conversions based on the underlying OS.

This is fairly simple on Linux since each IP address is assigned it's own
device name (e.g. eth0, eth0:1, eth1, eth1:1, etc.).  FreeBSD is more
interesting since there is an interface for each physical NIC, and an
interface may have more than one IP address.  SCO OpenServer has multiple
IPs per NIC too, and makes it more interesting as one has to first parse
the output of the ``netstat -in'' command to get a list of interfaces, then
run ``ifconfig interface'' to get the specifics for that interface.

INTERNET:   bill at Celestial.COM  Bill Campbell; Celestial Software LLC
UUCP:               camco!bill  PO Box 820; 6641 E. Mercer Way
FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676
URL: http://www.celestial.com/

``Do not meddle in the affairs of cats, for they are subtle and will piss on
your computer.''  --Bruce Graham

More information about the spug-list mailing list