-1

I would like to grep ACCEPT, DROP, REJECT as is shown below.

-P INPUT ACCEPT
-P OUTPUT DROP
-P FORWARD REJECT

I executed

iptables -S | grep -E '\-P INPUT|\-P OUTPUT|\-P FORWARD' | awk '{print $2}'

My output looks like this

INPUT
OUTPUT
FORWARD

Anybody know how to grep only ACCEPT, DROP, REJECT? I just want grep policy in built-in chains. Below built-in chains I got user's chains which cannot be connected to result.

Thank you, M

  • 1
    Change your awk $2 to $3? – bsd Jan 30 '16 at 11:11
0

You were close. If you need the whole line:

iptables -S | grep -- "-P" | grep -E "ACCEPT|DROP|REJECT"

Output

-P INPUT ACCEPT
-P OUTPUT DROP
-P FORWARD REJECT

If you need only the matching words:

iptables -S | grep -- "-P" | grep -oE "ACCEPT|DROP|REJECT"

Output

ACCEPT
DROP
REJECT
1

There is no reason to invoke grep if awk is involved anyway:

iptables -S | 
  awk '/^-P INPUT/ {INPUT=$3}; /^-P FORWARD/ {FORWARD=$3}; /^-P OUTPUT/ {OUTPUT=$3}; 
  END {print INPUT; print FORWARD; print OUTPUT;}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.