|
#!/bin/bash
##---------------------------------------------------------------------------##
# Script: jiptables-L
# Author: Brian <genius@groupbcl.ca> :)
# Date: Christmas Day 2019
#
# Creates a pretty-printed output of the "iptables -L" command for iptables
# tables (throws in -v and --line-numbers for good measure.) The user can
# include other switches as desired. Output is to stdout.
##---------------------------------------------------------------------------##
# BUUS: This script is part of Brian's Useful Utilities Set
if [ "$1" == '-h' -o "$1" == '--help' ]
then
echo "Usage: jiptables-L [-n|--numeric] [-x|--exact] [table ...]"
echo " iptables is always called with -L, --verbose, and --line-numbers"
echo " table 'all' displays all tables (raw mangle filter nat security)"
echo " If not given, table defaults to 'filter'"
exit 0
fi
while [ "$1" ]
do
if [ "${1:0:1}" == '-' ]
then
[ "$1" == '-n' -o "$1" == '--numeric' ] && IPT_NUMERIC='-n'
[ "$1" == '-x' -o "$1" == '--exact' ] && IPT_EXACT='-x'
else
if [ "$1" == 'all' ]
then
TABLE_LIST='raw mangle filter nat security'
else
TABLE_LIST="$TABLE_LIST $1"
fi
fi
shift
done
[ "$TABLE_LIST" ] || TABLE_LIST='filter'
# Process the requestesd tables
for TABLE in $TABLE_LIST
do
# The alphabet causes the 'column' command to create dummy columns for the
# informational lines; the 'a'-'z' characters are later removed
echo "a b c d e f g h i j k l m n o p q r s t u v w x y z -----+$TABLE+table+-----"
# Get a verbose listing of the table and number the rules
iptables -t $TABLE -L -v --line-numbers $IPT_NUMERIC $IPT_EXACT
done |
# Add a new column named 'additional' on the 'Chain' informational lines
# Changes spaces to '+' on the 'Chain' line to prevent 'column' from columnising the text
sed 's/destination[[:space:]]*$/destination additional/; /^Chain/ s/ /+/g' |
# (See above for why we add the alphabet to the 'Chain' lines)
sed 's/^\(Chain\)/a b c d e f g h i j k l m n o p q r s t u v w x y z \1/' |
# Create a table from the output
column -t |
# In the informational lines ('Chain' and '----- x table'), change '+' back to spaces
sed '/^a .* z/ s/+/ /g' |
# Remove the (columnized) alphabet characters from the informational lines
sed 's/^a .* y *z *\(.*\)/\n\1/' |
# Determine where on the line the 'addtional' column starts
# Decolumnize (that is, remove extra spaces from) the 'additional' colum
awk '
/additional[[:space:]]*$/ && ! p { p = index($0, "additional") }
/^[1-9]/ && length > p { $0 = substr($0, 1, p-1) gensub(/ +/, " ", "G", substr($0, p)) }
{ print }'
# vim: tabstop=4
|