Print num-bytes allowed.
My best attempt as been
sudo iptables -L -n -v -x | grep -i accept
This will give me a large list of rules, including the number of bytes in the second column. Unfortunately, getting just that 2nd column is pretty difficult.
One option would be to pipe it into a file, and use cut on the file but the goal is to do it in as few operations as possible. Any help would be appreciated.
Advertisement
Answer
This bash command adds up the second column:
c=0; while read a nbyte rest; do ((c+=nbyte)); done < <(sudo iptables -L -n -v -x | grep -i accept); echo $c
Or, written over multiple lines:
c=0 while read a nbyte rest do ((c+=nbyte)) done < <(sudo iptables -L -n -v -x | grep -i accept) echo $c
How it works
c=0
This initializes the count to zero.
while read a nbyte rest; do
This reads a line of input and assigns the first column to
a
, the second tonbyte
, and all remaining columns torest
.((c+=nbyte))
This adds the value in
nbyte
toc
. Ifnbyte
contains a string, not a number, bash helpfully treats the string as if it were zero.done < <(sudo iptables -L -n -v -x | grep -i accept)
This completes the
while
loop and also supplies input to it via process substitution.echo $c
This displays the result.