ipa is a shell function that will display the distilled informaiton from the ip a
command.
ipa # display addresses for all interfaces
ipa [INTERFACE] # display address of a given interface
Add this into your shell configuration file (e.g., ~/.bashrc
or ~/.zshrc
).
# ipa prints distilled output of the "ip a" command
# version 1.1.0
function ipa() {
python3 - $@ << EOF
import contextlib, json, subprocess, sys
e = ['ip', '-j', 'a']
e.extend(['s', sys.argv[1]]) if len(sys.argv) >= 2 else None
s = subprocess.Popen(e, stdout=subprocess.PIPE)
j = s.communicate()[0]
sys.exit(s.returncode) if s.returncode != 0 else None
for i in json.loads(j):
with contextlib.suppress(Exception):
print('{}: {}'.format(i['ifindex'], i['ifname']))
print(' MAC: {}'.format(i['address'])) if i.get('address') else None
print(' MAC (Permanent): {}'.format(i['permaddr'])) if i.get('permaddr') else None
for a in i['addr_info']:
family = 'IPv4' if a['family'] == 'inet' else a['family']
family = 'IPv6' if a['family'] == 'inet6' else family
print(' {}: {}/{}'.format(family, a['local'], a['prefixlen']))
EOF
}
Below is the legacy implementation implemented with egrep. It is not recommended.
alias ipa='ip a | egrep --color=never -o "^[0-9]+: ([a-z0-9])+[0-9]*|.*link/(ether|loopback) ([a-zA-Z0-9]{2}:){5}[a-zA-Z0-9]{2}|.*inet ([0-9]{1,3}.){3}([0-9]{1,3}/[0-9]{1,2})|.*inet6 (([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/[0-9]{1,3}" | sed "s/link\/ether/Ethernet:/g" | sed "s/inet6/IPv6:/g" | sed "s/inet/IPv4:/g"'