How does grep listen on port 80, which can also filter on another port, contains 80 such as 8080 8088 ...?

I want to use bash for grep if any process is listening on port 80, but now I come across a different port number that annoys me. How can I filter this in grep?

netstat -ant|grep LISTEN|grep 80|wc -l

It also displays other entries, such as 8080, 8088, etc.

+3
source share
6 answers

I look at the output of my command netstatand see the following:

tcp4       0      0  10.0.1.10.56941        64.34.119.101.80       ESTABLISHED
tcp4       0      0  10.0.1.10.56936        64.34.119.101.80       ESTABLISHED
tcp4       0      0  10.0.1.10.56932        64.34.119.101.80       ESTABLISHED
tcp4       0      0  10.0.1.10.56929        64.34.119.101.80       ESTABLISHED
tcp4       0      0  10.0.1.10.56922        64.34.119.101.80       ESTABLISHED
tcp4       0      0  10.0.1.10.56914        64.34.119.101.80       ESTABLISHED
tcp4       0      0  *.*                    *.*                    CLOSED     
tcp46      0      0  *.80                   *.*                    LISTEN     
tcp4       0      0  127.0.0.1.49153        *.*                    LISTEN     
tcp4       0      0  127.0.0.1.49152        *.*                    LISTEN     
tcp4       0      0  *.631                  *.*                    LISTEN     
tcp6       0      0  *.631                  *.*                    LISTEN     

I believe the port is the last number in the five-point point output. It means that

grep "\.80 " 

80. \. , . ( ). , 80, , 8080. , IP- .80 .

, awk grep. awk :

$ netstat -ant | awk '$6 == "LISTEN" && $4 ~ /\.80$/' | wc -l

awk . # 6 ($ 6 awk) - , ESTABLISHED, CLOSED, LISTEN. $4 - IP- .

, , LISTEN , # 4 \.80$. $ , \. . awk , , .

Awk - . . BEGIN, , END, . . $0 . , NF, , NR - . , .. awk script, , wc -l.:

$ netstat -ant | awk '
      BEGIN {total = 0}
      END {print "There are " total " lines I found"}
      {
          if ($6 == "LISTEN" && $4 ~ /\.80$/) {
              total = total + 1
          }
      }'

OP :

tcp       0      0  0.0.0.0:8080        0.0.0.0:*       LISTEN

:

$ netstat -ant | awk '$6 == "LISTEN" && $4 ~ /:80$/' | wc -l

\. : ...

$ netstat -ant | awk '$6 == "LISTEN" && $4 ~ /[\.:]80$/' | wc -l

[\.:], , . , ...

$ netstat -ant | awk '$6 == "LISTEN" && $4 ~ /[^0-9]80$/' | wc -l

[^0-9] . . , , , , - , netstat, .

+5

grepping netstat - , , - fuser, , :

$ fuser -n tcp 4005
4005/tcp:            19339

, - , , , -q :

if fuser -q -n tcp 4005 ; then
    echo "port is in use"
else
    echo "port not in use"
fi

, , :

fuser -n tcp ,,80

... :

fuser -n tcp ,1.2.3.4,80

- , , .

+4

grep ":80 " grep 80

+3

, (, :80). :

netstat -ant | grep ':80\b.*LISTEN'

, grep . , grep -c, wc:

netstat -ant | grep -c ':80\b.*LISTEN'
+2

, , : netstat -ntpl | grep ':PORT '

:

$ netstat -ntpl | grep ':80 '
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12960/nginx -g daem
tcp6       0      0 :::80                   :::*                    LISTEN      12960/nginx -g daem
+1
source

If you want it to be more reliable, you can do

netstat -ant |egrep  "^tcp +[[:digit:]]+ +[[:digit:]]+ +[[:digit:]\.]+\:80 +.*LISTEN *$"
0
source

All Articles