You can try using Python. It is installed by default on most distributions. It is very easy to use.
$python
>>> import netifaces as ni
>>> ni.interfaces()
['lo', 'eth0','eth1','eth2']
>>> ni.ifaddresses('eth0')
{17: [{'addr': '02:93:07:be:da:d9', 'broadcast': 'ff:ff:ff:ff:ff:ff'}], 2: [{'addr': '10.0.0.76', 'netmask': '255.255.255.0', 'broadcast': '10.0.0.255'}], 10: [{'addr': 'fe80::93:7ff:febe:dad9%eth0', 'netmask': 'ffff:ffff:ffff:ffff::'}]}
In the above example, there are four interfaces lo, eth0, eth1, eth2. The ip address for eth0 is 10.0.0.76.
A Linux server can have more than one network interface. You can check out my post on my website here to get more info about this.
This Python script displays the mac address, ip address, netmask for each network interface.
Here's another way to get the IP address without using a python package:
import socket
import fcntl
import struct
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
get_ip_address('eth0') # '192.168.0.110'
You can refer to this post to get more about how to use Python to get ip address.