I want to print specific port is listening or not on RHEL 8

I want to print specific port is listening or not on RHEL 8.

If i run this command -->netstat -an | grep "0.0.0.0:80" | grep -i listen
o/p

tcp 0 0 0.0.0.0:8009 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN

I would like to print only 80 port line.
I have tried with lsof -i:80 which is not working.

Please help me out.

Welcome!

grep -w
(word)
Beginning and end of the search expression must be at word boundaries.

So grep -w might not actually help in this particular case.
If I understood correctly, the OP needs something more like these:

netstat -an | grep '0.0.0.0:80 ' | grep -i listen #there's a space after 80
#or
netstat -an | grep '0.0.0.0:80\s' | grep -i listen
#or
netstat -an | grep '0.0.0.0:80[[:blank:]]' | grep -i listen
#in more general case (all interfaces, and also IPv6)
netstat -an | grep ':80 ' | grep -i listen
netstat -an | grep ':80\s' | grep -i listen
netstat -an | grep ':80[[:blank:]]' | grep -i listen

It does help:

netstat -an | grep -w '0.0.0.0:80' | grep -i listen

The . matches any character; the following are more precise:

netstat -an | grep -w '0\.0\.0\.0:80' | grep -i listen
netstat -an | grep -Fw '0.0.0.0:80' | grep -i listen

Try the following:

netstat -an | awk '($4 == "0.0.0.0:80"){print}'

N.B. the {print} is not strictly required as that's the default action. But I find that listing it out helps people learn awk.

And just print defaults to print $0
$0 is the whole input record. ($1, $2, ... are the 1st, 2nd, ... fields.)

1 Like