Easy way to get local IP address


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Easy way to get local IP address
# 1  
Old 11-26-2005
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?
# 2  
Old 11-26-2005
Here's an idea....tell us which os.
# 3  
Old 11-26-2005
Quote:
Originally Posted by Perderabo
Here's an idea....tell us which os.
Linux, CentOS4.1.

Isn't netstat common to both Unix & Linux?
http://www.computerhope.com/unix/unetstat.htm
# 4  
Old 11-26-2005
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....
Code:
$ 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
$

# 5  
Old 11-26-2005
if you have 'arp' you can try something like that:
Code:
thisHost=$(/usr/sbin/arp $(hostname) | nawk -F'[()]' '{print $2}')

# 6  
Old 11-27-2005
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 :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Can't get local IP address in getaddrinfo

Hi all, I am working with SIP protocol and am using SIPp to generate SIP traffic. But the call is not going through since I get the error; 2015-02-24 14:09:39:330 1424804979.330517: Can't get local IP address in getaddrinfo, local_host='NODE-01', local_ip=''. My ifconfig output is; ... (2 Replies)
Discussion started by: Junaid Subhani
2 Replies

2. IP Networking

netstat local and foreign address relationship.

Hi All, Can you please help me in understanding the relationship between local and foreign address in the output of netstat -an. Output 1 ---------- 162.103.162.37.50224 162.103.162.35.9511 49640 0 49640 0 ESTABLISHED 162.103.162.37.50263 162.103.162.35.9512 49640 0... (1 Reply)
Discussion started by: Girish19
1 Replies

3. IP Networking

Ping domain indicates the external ip address and not the local ip

when i execute from local machine ping domainname i get the external ip address but i am on local dns and i expect the local ip address.. using nslookup : no problem so i cannot find why... thanks (2 Replies)
Discussion started by: activedms
2 Replies

4. Debian

How to change local IP address?

I have a new Ethernet device that has a default IP address on a different subnet and need to change it. I have a Debian 6.0 host connected to the device with a crossover cable and have changed the host /etc/hosts and /etc/networks files to the same subnet as the new device and rebooted. Ping... (2 Replies)
Discussion started by: snorkack59
2 Replies

5. IP Networking

How to know local IP address in X-Terminal?

Im using a X-Terminal in my windows pc to connect to a Linux server. Is there a way to know my local IP address in my x-terminal console? Here are few commands which didnt help me: ss_cc@MGTS5026-13sh1:~> finger Login Name Tty Idle Login Time Where loadhlr ... (6 Replies)
Discussion started by: Arun_Linux
6 Replies

6. BSD

Link Local IPv6 Address

Hi, Am using FreeBSD7.4/i386 During IPv6 configuration, I added the following in rc.conf as Restarted IPv6 network using /etc/rc.d/network_ipv6 restart.. My problem is I need to set link local IPv6 address auto-configured.. Is my proceeding right?? I feel something missing to make... (0 Replies)
Discussion started by: Priya Amaresh
0 Replies

7. HP-UX

configuring site-local IPv6 address

How do I configure site-local IPv6 address in HP-UX box? I can get link local IPv6 address automatically when I put IPv6 up. aps39-88-root# ifconfig lan0 inet6 up (0 Replies)
Discussion started by: kirtikjr
0 Replies

8. Shell Programming and Scripting

Get Local IP address using Sed, Awk

Hi All, how to get solaris box local ip addresss in variable, using sed or awk utlities. Thanks, Mani Muthu (7 Replies)
Discussion started by: k_manimuthu
7 Replies

9. Shell Programming and Scripting

Local system IP address in UNIX

We have requirement to get the local system IP address of whoever logined the DataBase(sqlplus) or any process in application server. Actually I connected to application server thru putty and then conencted to Database using sqlplus command. we have tried below commands : who -u|grep... (4 Replies)
Discussion started by: siri_886
4 Replies

10. UNIX for Dummies Questions & Answers

How do I get the IP address of my local unix box???

Hiya all, How do I get the IP address of my local unix box??? IPconfig - don't exist nslookup - only for other boxes.... I have my Fedora Server now on the office network. Can ping others, can get onto internet via web proxy.... Also: How do I create a Drive Share, so our windows... (9 Replies)
Discussion started by: marty 600
9 Replies
Login or Register to Ask a Question