Score:0

iptables list line numbers script iptables-save -bash: number-rules: command not found

cn flag

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.

it flag
You need to make sure `number-rules` is executable and then need to use `./` to execute it from the current working directory (as it's not in your path) - so `iptables -S | ./number-rules`
Ergest Basha avatar
cn flag
@parkamark that was the error. Thank you
Score:2
it flag

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

Ergest Basha avatar
cn flag
I made it executable and execute as you posted and it worked like a charm. Thanks
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.