Easy way to get local IP address

I needed to get a machine's local IP address when not root (so no ifconfig).

Eventually, I arrived at this convoluted solution that grabs the unique local IP info from netstat...

netstat -n -t | awk '{print $4}' | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" |\
grep -v "127.0.0.1" | sort -u

...however I suspect there is an easier, more robust way of doing this.

Any ideas?

Here's an idea....tell us which os.

Linux, CentOS4.1.

Isn't netstat common to both Unix & Linux?

I don't have access to a CentOS4.1 distro of Linux (never even heard of it), but are you sure that "ifconfig -a" requires root? It certainly works for me on RedHat. I had to use /sbin/ifconfig -a since /sbin is not on my PATH.

There are many different versions of netstat and with HP-UX or Solaris, "netstat -in" would have solved your problem. With RedHat, the ip address is not displayed. (Also, while HP-UX has a ifconfig, there is no "ifconfig -a".) The first unix system I used had no netstat command. I don't know of any modern unix without a netstat command, but they do not conform to that link you posted. "netstat -D"? Let me try that on HP-UX....

$ netstat -D
netstat: illegal option -- D
Usage:  netstat [-an] [-f address-family] [system core]
        netstat [-Mnrsv] [-f address-family] [-p protocol] [system core]
        netstat [-gin] [-I interface] [interval] [system core]
        -a      show state of all sockets, including passive sockets
        -f      show statistics only for specified address family
        -g      show multicast information for network interfaces
        -i      show statistics for network interfaces
        -I      show statistics only for specified network interface
        -M      show multicast routing tables
        -Ms     show multicast routing statistics
        -n      display network addresses numerically
        -p      show statistics only for specified protocol
        -r      show routing tables
        -rv     show additional information for the routing table
        -s      show statistics for all protocols

        interval        display interface statistics continuously
        system          source of kernel symbols
        core            source of kernel data
$

if you have 'arp' you can try something like that:

thisHost=$(/usr/sbin/arp $(hostname) | nawk -F'[()]' '{print $2}')

FYI CentOS is Redhat Enterprise re-compiled from source RPMs with all the trademark stuff removed. It's good to play around with RHES for free before you need all the production-grade support etc.

Not sure why you are typing netstat -D. What would that normally do? netstat -n would work, that displays IP addresses instead of names. To get the same effect as netstat -t I guess you would use netstat -p <protocol>, on Redhat Linux -t restricts the protocol to TCP.

You're right, ifconfig -a does work as non-root however on Redhat the /sbin directory where it lives is not in a normal user's search path. Anyway, thanks for pointing that out to me as it is probably a safer command to work with.

Yes, arp is available on Redhat but is also in /sbin.
/sbin/arp -i eth0 -a
Seems to produce meaningful output.

Thanks for you help :slight_smile: