iptables – How to List Line Numbers in iptables Script

bashfirewalliptableslinux

I am trying to use a script for showing iptables line numbers from iptables-save command.

The script was taken from this site: https://blog.oddbit.com/post/2018-02-08-listing-iptables-rules-with-li/

And is as follows with number-rules name:

#!/bin/awk -f

state == 0 && /^-A/ {state=1; chain=$2; counter=1; printf "\n"}
state == 1 && $2 != chain {chain=$2; counter=1; printf "\n"}
!/^-A/ {state=0}
state == 1 {printf "[%03d] %s\n", counter++, $0}
state == 0 {print}

When I execute it throws below error:

root@ergesttstsrv:~# iptables -S | number-rules
-bash: number-rules: command not found

I checked awk with

root@ergesttstsrv:~# which awk
/usr/bin/awk

And changed the first line of the script
from #!/bin/awk -f to /usr/bin/awk -f , but still the same error.

Is there a better way of doing this without the iptables -nv -L --line-numbers or what is the error on the script?
Note I'm quite new with bash.

Best Answer

You need to make sure number-rules is executable and then need to use ./ to execute it from the current working directory, as that's not in your path - so iptables -S | ./number-rules