0

I am writing a bash script and need to store some primitive network information in separate variables. For example, mac address and IP address in separate variables, for example

IPADDRESS=$(some_command)
MACADDRESS=$(some_other_command)
echo "$IPADDRESS has mac address $MACADDRESS"

This is a simplified example, since I AM NOT going to be reading this directly, but rather passing to a proprietary program as /bin/proprietary_program $IPADDRESS $MACADDRESS.

How do I get just the MAC address as a string and the IP address as a string in Linux? I prefer to use the ip command where possible.

mrflash818
  • 305
  • 3
  • 11
  • 2
    What if you had more than one IP and/or MAC address? – Jeff Schaller Nov 30 '20 at 16:28
  • 2
    Relating https://unix.stackexchange.com/q/8518/117549 – Jeff Schaller Nov 30 '20 at 16:30
  • Thanks for the link. The systems this runs on should only have the one interface, but in the event that there's more than one I will ask the user to enter the interface name they want to use – Anon Ymous Nov 30 '20 at 16:41
  • Which IP address? IPv4? IPv6? Both? Local network? External network? Presumably you don't want the `127.0.0.1` IP, right? Can you post the output of `ip 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:02
  • I had wanted to capture the output from `ip addr` for link/ether, although I seem to have managed this by some weird combination of grep and cut via `MACADDR=$(ip -4 -o link list $IFACE | grep -o -E 'link/ether.*' | cut -d ' ' -f 2)`; I can do similar for ip address and add a `cut -d '/' -f 1` to cut off the /16 after the IP. – Anon Ymous Dec 02 '20 at 02:33

2 Answers2

0

Since I didn't see any examples using nmcli (NetworkManager), I'll add them here:

nmcli -f IP4.ADDRESS device show your_nic (returns all IPv4 addresses)
nmcli -f IP6.ADDRESS device show your_nic (returns all IPv6 addresses)
nmcli -f GENERAL.HWADDR device show your_nic (returns MAC address)
ajgringo619
  • 3,113
  • 1
  • 11
  • 37
0

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.

Stewart
  • 12,628
  • 1
  • 37
  • 80