Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

Want to get the IP address using Shell script. Without knowing the eth0 or eth1 or eth2 How to get the particular IP address.

I am not interest to get localhost address, want to get private IP address.

share|improve this question

marked as duplicate by jasonwryan, slm, Anthon, rahmu, Timo Mar 12 at 12:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Isn't ifconfig -a what you're looking for? –  rusty Mar 12 at 7:43
    
@jasonwryan : clearly i said, i am using Private ip address (within office or college we can set private address). I am not using public address. –  rajcoumar Mar 12 at 7:46
4  
I've just posted two links that together answer every permutation of your muddled question. Perhaps you might consider searching before whining... –  jasonwryan Mar 12 at 7:52
    
@rajcoumar I fail to see why using a private IP prohibits you from using the output of ifconfig -a. In fact the answer you marked as accepted does just that (just uses ip instead of ifconfig). –  Patrick Mar 12 at 12:47
    
hello slm Anthon rahmu jasonwryan : i got popular question award for this question... still you all guys giving negative points... such a irritate fellows –  rajcoumar Jul 10 at 4:07

3 Answers 3

up vote 2 down vote accepted

You can do :

ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'

which gives you the first private IP address listed in ip addr.

For example, with ip addr, I get:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:16:76:de:c1:f1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.85/24 brd 192.168.0.255 scope global dynamic em1
       valid_lft 42505sec preferred_lft 42505sec
    inet6 fe80::216:76ff:fede:c1f1/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 52:54:00:da:92:d0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 500
    link/ether 52:54:00:da:92:d0 brd ff:ff:ff:ff:ff:ff

With the commandline before, I get 192.168.0.85 which is the IP address of em1.

To put it in a variable inside a shell script, you can do var=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'). Now, until the end of the script, $var will have the value of the IP address.

share|improve this answer
    
And indeed, it's very similar to unix.stackexchange.com/questions/8518/…. I did not look for duplicates before posting... –  lgeorget Mar 12 at 7:54
    
ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/' using this command i got private address. Now i want assign the output to a variable –  rajcoumar Mar 12 at 8:07
    
@rajcoumar I updated the answer. Note that this question had already been asked and answered on this site. See unix.stackexchange.com/questions/4569/… for details. –  lgeorget Mar 12 at 8:12
    
Note also that my answer is not of the most elegant. There may be a better way to do this than parsing the output of ip addr. –  lgeorget Mar 12 at 8:13

If you want list all ip address, regardless its name, try this:

ifconfig | perl -nle 's/dr:(\S+)/print $1/e'

or:

ifconfig | awk '/inet addr/{print substr($2,6)}'
share|improve this answer
    
thanks, this is my output 192.168.56.102 10.192.1.218 10.0.2.15 127.0.0.1, Now i want to take 10.192.1.218 and assign into new varible in shell script. how we can do ? –  rajcoumar Mar 12 at 7:48
    
that would just display the IP addresses.. it would be better if the corresponding interface names like eth0, eth1,... are displayed too. :) –  rusty Mar 12 at 7:50

Flagrant copy paste from stackoverflow since we can't dupe across sites. I know it's not bash or sh, but who doesn't have python installed at this point anyway?

You should use netifaces. It is designed to be cross-platform on Mac OS X, Linux, and Windows.

>>> import netifaces as ni
>>> ni.interfaces()
['lo', 'eth0', 'eth1', 'vboxnet0', 'dummy1']
>>> ni.ifaddresses('eth0')
{17: [{'broadcast': 'ff:ff:ff:ff:ff:ff', 'addr': '00:02:55:7b:b2:f6'}], 2: [{'broadcast': '24.19.161.7', 'netmask': '255.255.255.248', 'addr': '24.19.161.6'}], 10: [{'netmask': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::202:55ff:fe7b:b2f6%eth0'}]}
>>> 
>>> ni.ifaddresses.__doc__
'Obtain information about the specified network interface.\n\nReturns a dict whose keys are equal to the address family constants,\ne.g. netifaces.AF_INET, and whose values are a list of addresses in\nthat family that are attached to the network interface.'
>>> # for the IPv4 address of eth0
>>> ni.ifaddresses('eth0')[2][0]['addr']
'24.19.161.6'

The numbers used to index protocols are from /usr/include/linux/socket.h (in Linux)...

#define AF_INET         2       /* Internet IP Protocol         */
#define AF_INET6        10      /* IP version 6                 */
#define AF_PACKET       17      /* Packet family                */

End copy paste

If all you want is the IP of the up and outbound interface, this works.

Yet ANOTHER option if you just want to enumerate over up interfaces, since nobody seems to be able to understand exactly what you want:

import netifaces as ni
ints = ni.interfaces()
for i in ints:
  if 'eth' in i:
    try:
      ni.ifaddresses(i)[2][0]['addr']
      print("interface: " + i)
      print("address: " + ni.ifaddresses(i)[2][0]['addr'])
    except:
      pass
  elif 'wlan' in i:
    try:
      ni.ifaddresses(i)[2][0]['addr']
      print("interface: " + i)
      print("address: " + ni.ifaddresses(i)[2][0]['addr'])
    except:
      pass
share|improve this answer
    
Note that the OP stated in his question that it doesn't know in advance which interface is UP. He just wants the IP addr of the interface in state "up" without knowing which one it is. –  lgeorget Mar 12 at 8:29
    
Then it's just getting the local address from an open socket to Google. The way I read it, he wanted to enumerate over all interfaces and map IPs to interfaces as opposed to just opening a socket and looking for the local IP. –  hbdgaf Mar 12 at 8:31
    
Added a simple script to address the "just give me the outbound IP" problem. –  hbdgaf Mar 12 at 8:36
    
He doesn't want his external address but his private address. The way I understand it, he could be connected to a private network by any of his interfaces and just wants to know his private IP address without knowing which interface is used. –  lgeorget Mar 12 at 8:42
    
The private address would still be given by the second script, it's just the outbound private address. –  hbdgaf Mar 12 at 8:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.