Skip to content

Instantly share code, notes, and snippets.

@jps3
Last active December 2, 2018 17:43
Show Gist options
  • Save jps3/c1c90aa3979f65bb6e1555b768fd23cd to your computer and use it in GitHub Desktop.
Save jps3/c1c90aa3979f65bb6e1555b768fd23cd to your computer and use it in GitHub Desktop.
Colorize output from `iptables --line-numbers -nL` (`--list`) command
#!/bin/sed -Ef
# Adoption of nega0's gist as a standalone sed script
# https://gist.github.com/nega0/1d232622a1fa3dad176869bbfe747602
#
# Usage:
#
# $ sudo iptables --line-numbers -nL | ./iptables-colorize.sed
#
# You must, of course, make this sed script executable.
#
# It can be convenient to create a function or alias like:
#
# function iptables-list-color () {
# sudo iptables --line-numbers -nL | \
# /path/to/iptables-colorize.sed
# }
#
## Underline chain "section titles"
s/^Chain.*$/\x1b[4m&\x1b[0m/
## Makes the column headers yellow
s/^num.*/\x1b[33m&\x1b[0m/
## Highlights REJECT and DROP as red everwhere except chain "section titles"
/([^y] )((REJECT|DROP))/s//\1\x1b[31m\3\x1b[0m/
## Highlights ACCEPT as green
/([^y] )(ACCEPT)/s//\1\x1b[32m\2\x1b[0m/
## Highlights port numbers as yellow
/([ds]pt[s]?:)([[:digit:]]+(:[[:digit:]]+)?)/s//\1\x1b[33;1m\2\x1b[0m/
## Highlights IP address and CIDR blocks as cyan
/([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}(\/([[:digit:]]){1,3}){0,1}/s//\x1b[36;1m&\x1b[0m/g
## Highlights LOGDROP as yellow everywhere except chain "section titles"
/([^n] )(LOGDROP)/s//\1\x1b[33;1m\2\x1b[0m/
## Highlights LOG everywhere as cyan
s/ LOG /\x1b[36;1m&\x1b[0m/
## Highlights DOCKER as blue everywhere except chain "section titles"
/([^n] )(DOCKER)/s//\1\x1b[34;1m\2\x1b[0m/
@jps3
Copy link
Author

jps3 commented Dec 2, 2018

This was created and tested with Debian 9 (stretch), GNU sed 4.4, and GNU bash 4.4.12, although I can't imagine these will make much of a difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment