Skip to content

Instantly share code, notes, and snippets.

@k4yt3x
Last active October 11, 2022 21:19
Show Gist options
  • Save k4yt3x/162a7419e58a60ab774b65318179601b to your computer and use it in GitHub Desktop.
Save k4yt3x/162a7419e58a60ab774b65318179601b to your computer and use it in GitHub Desktop.
ipa: A shell alias that will simplify your "ip address" output

ipa

Description

ipa is a shell function that will display the distilled informaiton from the ip a command.

Screenshots

image

single interface

Usage

ipa  # display addresses for all interfaces
ipa [interface]  # display address of a given interface
ip4a  # display only IPv4 information
ip6a  # display only IPv6 information

Code

Add this into your shell configuration file (e.g., ~/.bashrc or ~/.zshrc).

# ipa prints distilled output of the "ip a" command
# version 2.1.0
alias ip4a="ipa -4"
alias ip6a="ipa -6"
function ipa() {
    python3 - $@ << EOF
import contextlib, json, os, subprocess, sys
e = ['ip', '-j', 'a']
if len(sys.argv) >= 2 and sys.argv[1] in ['-4', '-6']:
    e.insert(2, sys.argv[1])
elif len(sys.argv) >= 2:
    e.extend(['s', sys.argv[1]])
color = True if os.getenv('COLORTERM') in ['truecolor', '24bit'] else False
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'], '\033[36m' if color else '', i['ifname'], '\033[0m' if color else ''))
        print('    State: {}{}{}'.format({'UP': '\033[32m', 'DOWN': '\033[31m'}.get(i['operstate'], '') if color else '',
                                         i['operstate'], '\033[0m' if color else '')) if i.get('operstate', 'UNKNOWN') != 'UNKNOWN' else None
        print('    MAC: {}{}{}'.format('\033[33m' if color else '', i['address'], '\033[0m' if color else '')) if i.get('address') else None
        print('    MAC (Permanent): {}{}{}'.format('\033[33m' if color else '', i['permaddr'], '\033[0m' if color else '')) if i.get('permaddr') else None
        for a in i['addr_info']:
            print('    {}: {}{}/{}{}'.format(
                {'inet': 'IPv4', 'inet6': 'IPv6'}.get(a['family'], a['family']),
                {'inet': '\033[35m', 'inet6': '\033[34m'}.get(a['family'], '') if color else '',
                a['local'], a['prefixlen'], '\033[0m' if color else ''))
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"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment