Score:0

How to show bytes value iptables

ua flag

I am trying to output the bytes value of iptables. I tried the following:

sudo iptables -nvL INPUT --line-numbers

I got the following output:

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      316 18844 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate NEW limit: avg 60/sec burst 20
2        0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate NEW

I also tried:

sudo iptables -nvL INPUT --line-numbers | grep ACCEPT

But I'm not getting ONLY the bytes value.

I have no ideo how I can extract the bytes value (18844 and 0) from this command.

I hope you can help me.

Score:1
pt flag

It looks as if the bytes value is the 3rd column, so you can write:

iptables -nvL INPUT | awk '/policy/ {next} /ACCEPT/ {print $3}'

Given your example output above, that produces:

18844

The first pattern in that awk script (/policy/ {next}) is to skip the first line, which otherwise would match on ACCEPT.

PythonProgrammer_12 avatar
ua flag
Your answer wasn't 100% correct for me, but I works when I changen the "$3" to "$2". I marked it as correct, because without your answer I would have no idea how I could sove it. Thanks you so much.
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.