Last active
August 29, 2015 14:05
-
-
Save melissaboiko/bdfdb75f0f1d1d8dcbbf to your computer and use it in GitHub Desktop.
colorize output of Linux ip(8). suitable for replacement via alias (hopefully).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# colorize output of Linux ip(8). suitable to replacement via alias (hopefully). | |
# expects GNU sed. | |
# | |
# expanded from: | |
# http://unix.stackexchange.com/questions/148/colorizing-your-terminal-and-shell-environment | |
IP_CMD=/bin/ip | |
NORMAL=`echo -e '\033[0m'` | |
RED=`echo -e '\033[0;31m'` | |
LRED=`echo -e '\033[1;31m'` | |
GREEN=`echo -e '\033[0;32m'` | |
LGREEN=`echo -e '\033[1;32m'` | |
YELLOW=`echo -e '\033[0;33m'` | |
LYELLOW=`echo -e '\033[1;33m'` | |
BLUE=`echo -e '\033[0;34m'` | |
LBLUE=`echo -e '\033[1;34m'` | |
MAGENTA=`echo -e '\033[0;35m'` | |
LMAGENTA=`echo -e '\033[1;35m'` | |
CYAN=`echo -e '\033[0;36m'` | |
LCYAN=`echo -e '\033[1;36m'` | |
DEV='\<(lo|(eth|wlan)[0-9:]+)\>' | |
MAC='([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}' | |
IP4='([0-9]{1,3}\.){3}[0-9]{1,3}' | |
# http://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses | |
IP6='(([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]))' | |
# whee | |
MASK='\/[0-9]+\>' | |
GOOD='\<(UP|REACHABLE)\>' | |
BAD='\<DOWN|STALE\>' | |
KEYWORD='\<(state|scope|label|lookup|from|mtu) ([A-Za-z0-9]*)\>' | |
DEV_C=$LMAGENTA | |
MAC_C=$MAGENTA | |
IP4_C=$LBLUE | |
IP6_C=$LCYAN | |
MASK_C=$YELLOW | |
GOOD_C=$LGREEN | |
BAD_C=$LRED | |
VALUE_C=$GREEN | |
DEFAULT_C=$LGREEN | |
"$IP_CMD" "$@" | sed -r \ | |
-e "s/$DEV/${DEV_C}&${NORMAL}/g"\ | |
-e "s/$MAC/${MAC_C}&${NORMAL}/g"\ | |
-e "s/$IP4/${IP4_C}&${NORMAL}/g"\ | |
-e "s/$IP6/${IP6_C}&${NORMAL}/g"\ | |
-e "s/$MASK/${MASK_C}&${NORMAL}/g"\ | |
-e "s/$GOOD/${GOOD_C}&${NORMAL}/g"\ | |
-e "s/$BAD/${BAD_C}&${NORMAL}/g"\ | |
-e "s/$KEYWORD/\1 ${VALUE_C}\2${NORMAL}/g"\ | |
-e "s/link\/([a-z0-9]*)\>/link\/${VALUE_C}\1${NORMAL}/g"\ | |
-e "s/\<default\>/${DEFAULT_C}&${NORMAL}/g"\ |
Author
melissaboiko
commented
Aug 22, 2014
This cool. I was searching for exactly this and it came up as the third result on Google. ip's output is so compact and hard to read. Thanks for saving me a lot of work doing it myself... especially those crazy regexes.
One note... in bash, this will do the same thing without calling echo:
RED=$'\033[0;31m'
From the bash manpage: "Words of the form $'string' are treated specially. The word expands to string, with back‐slash-escaped characters replaced as specified by the ANSI C standard." It goes on to define all the escapes it recognizes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment