3

I'm trying to learn as much as possible about Linux, I'm currently stuck at trying to grab specific parts of my ifconfig text display so it looks exactly like this:

 eth0:
       inet 192.168.1.000  netmask 255.255.255.0  broadcast 192.168.1.255
 wlan0:
       inet 192.168.1.xxx  netmask 255.255.255.0  broadcast 192.168.1.255

I've come so close, so far i have tried about a million possible combinations but I'm close to exhausted, this is some of what i have tried.

  ifconfig | head -19 | sed 'wlan0|\eth0' | awk '{print $2}' 
  ifconfig | head -19 | egrep 'wlan0|\eth0' | awk '{print $1}' | sed '/^net/p'
  ifconfig | head -19 | egrep 'wlan0|\eth0|' | awk '{print $1}'
  ifconfig | cut -d: -f1 | awk '{print $2}' | cut -d: -f1

This is as close as i have come

 ifconfig  |  head -n 2  |   cut -d: -f1; ifconfig | tail -8 | head -1

 eth0
    inet 192.168.1.000  netmask 255.255.255.0  broadcast 192.168.1.255
    inet 192.168.1.xxx  netmask 255.255.255.0  broadcast 192.168.1.255

I'm missing wlan0 in between 000 and xxx, Thank you for your time and effort.

My Display

 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.1.000  netmask 255.255.255.0  broadcast 192.168.1.255
    inet6 xxxxxxxxxxxxxxxxxxxxxxx  prefixlen 64  scopeid 0x20<link>
    ether xxxxxxxxxxx  txqueuelen 1000  (Ethernet)
    RX packets 711634  bytes 635444016 (606.0 MiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 523020  bytes 98052518 (93.5 MiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
    inet 127.0.0.1  netmask 255.0.0.0
    inet6 ::1  prefixlen 128  scopeid 0x10<host>
    loop  txqueuelen 1000  (Local Loopback)
    RX packets 4266  bytes 735580 (718.3 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 4266  bytes 735580 (718.3 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.1.xxx  netmask 255.255.255.0  broadcast 192.168.1.255
    inet6 xxxxxxxxxxxxxxxx  prefixlen 64  scopeid 0x20<link>
    ether xxxxxxxxxxx  txqueuelen 1000  (Ethernet)
    RX packets 2429  bytes 371836 (363.1 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 227  bytes 79847 (77.9 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

I just found a way with

 ifconfig  |  head -n 2  |   cut -d: -f1; ifconfig | tail -9 | cut -d: -f1 | head -2

But I'm open to more ways if you have them

muru
  • 197,895
  • 55
  • 485
  • 740

3 Answers3

2

A proper way:

(better use ip(8) in 2018)

Using grep -P ('s regex)

for dev in wlan0 eth0; do
    ip address show dev $dev |
        grep -oP 'inet\s+\K\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
done

Using

for dev in wlan0 eth0; do
    ip address show dev $dev |
        awk -F'[ /]' '/inet /{print $6}'
done

If you insist to use :

for dev in wlan0 eth0; do
    ifconfig $dev | awk '/inet /{print $2}'
done

Last: if you need the interface name in the output :

for dev in wlan0 eth0; do
    ifconfig $dev | awk -vdev=$dev '/inet /{print dev, $2}'
done
1

Here is a sample of this answer of mine:

#!/bin/bash

# NAME: getIP

# Set the names of the target interfaces as array or assign the user's input
[[ -z ${@} ]] && IFACES=$(/sbin/ifconfig | sed -r '/^ .*/d; s/ .*//' | tr '\r\n' ' ') || IFACES=($@)

# Set IPv4 address match pattern
IPv4='[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'

# Get the IP address
for IFACE in ${IFACES[@]}; do
        /sbin/ifconfig "$IFACE" 2>/dev/null | grep -Po "${IPv4}" | tr '\r\n' ' ' | \
                awk -v iface="${IFACE}:" 'BEGIN{ print iface } { printf "\tinet %-16s netmask %-16s broadcast %s\n",$1, $3, $2}'
done

Note: The script is based on the output of ifconfig in Ubuntu 16.04. Usage:

$ ./getIP          # Automatic mode
enp2s0:
    inet 192.168.100.110  netmask 255.255.255.0    broadcast 192.168.100.255
lo:
    inet 127.0.0.1        netmask                  broadcast 255.0.0.0
vmnet1:
    inet 192.168.201.1    netmask 255.255.255.0    broadcast 192.168.201.255
vmnet8:
    inet 192.168.15.1     netmask 255.255.255.0    broadcast 192.168.15.255

$ ./getIP enp2s0    # User's input mode
enp2s0:
    inet 192.168.100.110  netmask 255.255.255.0    broadcast 192.168.100.255
pa4080
  • 29,831
  • Did you read my question, thank you for the example but i was looking for a specific output with headers of eth0 and wlan0 with the use of sed, cut, awk, grep and head or tail –  Mar 26 '18 at 21:22
0

If you're interested in just extracting an interface address- which it appears is the main thrust of your question- you could always try the hostname command and screen-scrape its' output by piping it to awk or grep, ie:

hostname -I | awk '{print $1}'

If the host is multi-homed, when using awk you can toggle the address output by changing $1 to $2, $3, etc... Note it also works with IPv6 addresses.

man hostname:

-I, --all-ip-addresses Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of the output.

Anyhoo, just another (very simple) way to grab an IP from a host.

F1Linux
  • 1,066