Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Easy way to get local IP address

Shell Programming and Scripting


Tags
linux

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-26-2005
simonb simonb is offline
Registered User
 
Join Date: Nov 2005
Last Activity: 4 February 2011, 4:33 AM EST
Location: London
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Sponsored Links
    #2  
Old Unix and Linux 11-26-2005
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 26 February 2016, 12:31 PM EST
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 469 Times in 270 Posts
Here's an idea....tell us which os.
Sponsored Links
    #3  
Old Unix and Linux 11-26-2005
simonb simonb is offline
Registered User
 
Join Date: Nov 2005
Last Activity: 4 February 2011, 4:33 AM EST
Location: London
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
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 Unix and Linux 11-26-2005
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 26 February 2016, 12:31 PM EST
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 469 Times in 270 Posts
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
$

Sponsored Links
    #5  
Old Unix and Linux 11-26-2005
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 23 June 2017, 8:10 AM EDT
Location: Foxborough, MA
Posts: 8,164
Thanks: 220
Thanked 773 Times in 723 Posts
if you have 'arp' you can try something like that:

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

Sponsored Links
    #6  
Old Unix and Linux 11-27-2005
simonb simonb is offline
Registered User
 
Join Date: Nov 2005
Last Activity: 4 February 2011, 4:33 AM EST
Location: London
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
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 :-)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
configuring site-local IPv6 address kirtikjr HP-UX 0 09-07-2011 07:18 AM
Get Local IP address using Sed, Awk k_manimuthu Shell Programming and Scripting 7 05-31-2011 03:17 PM
Local system IP address in UNIX siri_886 Shell Programming and Scripting 4 07-14-2009 01:44 PM
Need help on installing an EASY to use and easy to install command line text editor EugeneG UNIX for Dummies Questions & Answers 4 07-16-2007 11:53 AM
How do I get the IP address of my local unix box??? marty 600 UNIX for Dummies Questions & Answers 9 04-12-2005 09:45 PM



All times are GMT -4. The time now is 09:44 AM.