How to get IP Address using shell script?

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.


To list all IP addresses, regardless of name, try this:

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

or:

ifconfig | awk '/inet addr/{print substr($2,6)}'

Specify the interface name (e.g. eth0) right after ifconfig if you only want the IP of a specific interface:

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

or:

ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'

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