You could parse the output of ip address
: Mine looks like this (I've added extra IP addresses to show it can be done):
$ ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: enp10s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000
link/ether f0:79:59:dc:c4:bf brd ff:ff:ff:ff:ff:ff
3: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether f0:79:59:dc:c3:75 brd ff:ff:ff:ff:ff:ff
altname enp0s25
inet 192.168.1.100/24 brd 192.168.1.255 scope global noprefixroute eno1
valid_lft forever preferred_lft forever
inet 129.168.1.101/24 scope global eno1
valid_lft forever preferred_lft forever
inet6 fe80::f279:59ff:fedc:c375/64 scope link noprefixroute
valid_lft forever preferred_lft forever
If we grep a regex for the address we could end up with a list of IPv4 addresses.
$ IPV4=$(ip address | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2}' )
$ echo $IPV4
127.0.0.1/8 192.168.1.100/24 129.168.1.101/24
$ for i in $IPV4; do echo $i; done
127.0.0.1/8
192.168.1.100/24
129.168.1.101/24
You'd just need to write a regex for IPv6 (if desired) and MAC to round it out.
127.0.0.1
IP, right? Can you post the output ofip addr
and tell us what you want to capture from it? That is, assuming you are using a Linux variant. What OS does this need to work on? – terdon Nov 30 '20 at 18:02ip addr
for link/ether, although I seem to have managed this by some weird combination of grep and cut viaMACADDR=$(ip -4 -o link list $IFACE | grep -o -E 'link/ether.*' | cut -d ' ' -f 2)
; I can do similar for ip address and add acut -d '/' -f 1
to cut off the /16 after the IP. – Anon Ymous Dec 02 '20 at 02:33