Skip to content

Instantly share code, notes, and snippets.

@haxxinen
Created February 15, 2020 16:03
Show Gist options
  • Save haxxinen/36feceab589e560b81596e660d116ae4 to your computer and use it in GitHub Desktop.
Save haxxinen/36feceab589e560b81596e660d116ae4 to your computer and use it in GitHub Desktop.
1. Show interfaces by name
$ ip addr | awk '/: /{sub(":","--",$2); print $2}'
lo--
ens33--
docker0--
2. Show interfaces by name and corresponding IP addresses
$ ip addr | awk '/: /{sub(":","--",$2); print $2}/inet/{ print $2}'
lo--
127.0.0.1/8
::1/128
ens33--
172.16.201.160/24
fe80::20c:29ff:fe3d:4d1f/64
docker0--
172.17.0.1/16
3. Print values on the same line
$ ip addr | awk '/: /{sub(":",": ",$2); x=$2}/inet/{ print x$2}'
lo: 127.0.0.1/8
lo: ::1/128
ens33: 172.16.201.160/24
ens33: fe80::20c:29ff:fe3d:4d1f/64
docker0: 172.17.0.1/16
4. Final command - Linux
$ ip addr | awk '/[0-9]: +/{sub(":",": ",$2);x=$2}/inet/{print x$2}' | sed 's/\/.*//'
lo: 127.0.0.1
lo: ::1
ens33: 172.16.201.160
ens33: fe80::20c:29ff:fe3d:4d1f
docker0: 172.17.0.1
4. Final command - macOS
$ ifconfig | awk '/[0-9]: +/{sub(":",": ",$1);x=$1}/inet/{print x$2}' | sed 's/\%.*//'
lo0: 127.0.0.1
lo0: ::1
lo0: fe80::1
en1: 192.168.1.76
utun0: fe80::2e62:ec4e:231:28c6
vmnet1: 172.16.195.1
vmnet8: 172.16.201.1
vboxnet0: 192.168.56.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment