The ifconfig command displays details of every active network interface available on the system. However, we can restrict it to a specific interface using the following command:
$ ifconfig iface_name
Consider this example:
$ ifconfig wlan0 wlan0 Link encap:EthernetHWaddr 00:1c:bf:87:25:d2 inet addr:192.168.0.82 Bcast:192.168.3.255 Mask:255.255.252.0 inet6 addr: fe80::3a2c:4aff:6e6e:17a9/64 Scope:Link UP BROADCAST RUNNINT MULTICAST MTU:1500 Metric:1 RX Packets...
To control a device, we need the IP address, broadcast address, hardware address, and subnet mask:
- HWaddr 00:1c:bf:87:25:d2: This is the hardware address (MAC address)
- inet addr:192.168.0.82: This is the IP address
- Bcast:192.168.3.255: This is the broadcast address
- Mask:255.255.252.0: This is the subnet mask
To extract the IP address from the ifconfig output, use this command:
$ ifconfig wlan0 | egrep -o "inetaddr:[^ ]*" | grep -o "[0-9.]*" 192.168.0.82
The egrep -o "inetaddr:[^ ]*" command returns inet addr:192.168.0.82. The pattern starts with inetaddr: and ends with any non-space character sequence (specified by [^ ]*). The next command, grep -o "[0-9.]*" reduces its input to only numbers and periods, and prints out an IP4 address.