简体   繁体   English

如何grep监听端口80,也可以过滤其他端口包含80如8080 8088 ...?

[英]how to grep listening on port 80 that can also filter other port contain 80 such as 8080 8088 …?

I want to use bash to grep if any process is listening on port 80,but now I stumble upon other port number which annoys me. 如果任何进程正在侦听端口80,我想使用bash grep,但现在我偶然发现了其他令我烦恼的端口号。 How can I filter this in grep? 我怎样才能在grep中过滤这个?

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

It also outputs other records such as 8080 8088 and so on. 它还输出其他记录,如8080 8088等。

I'm looking at the output of my netstat command, and see this: 我正在查看我的netstat命令的输出,看到这个:

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 take it that the port is the last number in the five part dotted output. 我认为端口是五部分虚线输出中的最后一个数字。 That means that 这意味着

grep "\.80 " 

will pick up only port 80. The \\. 将只接收80端口。 \\. says to pick up the period. 说拿起这个时期。 (Normally the period means any character in regular expressions). (通常句点表示正则表达式中的任何字符 )。 And, by putting a space after the 80 , you'll guarantee that you're not going to pick up port 8080. In fact, you're guaranteed that you're not going to pick up IP addresses that have .80 in them. 并且,通过在80之后放置一个空格,您将保证您不会选择端口8080.事实上,您可以保证您不会选择其中包含.80 IP地址。

In fact, I'd recommend to use awk instead of grep . 事实上,我建议使用awk而不是grep With awk , you can specify fields and do a bit more processing: 使用awk ,您可以指定字段并进行更多处理:

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

With awk each column automatically becomes a separate field. 使用awk每列自动成为一个单独的字段。 Field #6 ($6 in awk) is the one that says ESTABLISHED , CLOSED , LISTEN in it. 字段#6(awk中的$ 6)是其中包含ESTABLISHEDCLOSEDLISTEN字段。 Field $4 is the first column IP address one. 字段$4是第一列IP地址。

In the above, I'm looking for lines that have the word LISTEN in the sixth field, and where field #4 matches the regular expression \\.80$ . 在上面,我正在寻找在第六个字段中有单词LISTEN的行,并且字段#4匹配正则表达式\\.80$ The $ is an anchor to the end of the string, and the \\. $是字符串末尾的锚点和\\. is picking up a decimal point and not representing any character. 正在拾取一个小数点并且不代表任何字符。 The awk command automatically prints out each line that matches, so I don't have to specify that. awk命令自动打印出匹配的每一行,所以我不必指定它。

Awk is really a programming language. Awk实际上是一种编程语言。 It assumes a read loop for each line in the file. 它假定文件中每行的读循环。 You can have a BEGIN clause that gets executed before the file is read and an END clause that executes after the file has been read. 您可以拥有一个在读取文件之前执行的BEGIN子句和一个在读取文件后执行的END子句。 The various fields are numbered and represented with a dollar sign. 各个字段都有编号,并以美元符号表示。 The special $0 variable represents the whole line. 特殊的$0变量代表整行。 Special variables like like NF gives you the number of fields in a line and NR gives you the number of lines read in. You also have a whole slew of functions to help parse text, etc. Here's a full blown version of the awk script that basically lays out everything for you, and does its own line counting, so you don't have to pipe to wc -l .: 像NF这样的特殊变量可以为你提供一行中的字段数,NR可以为你提供读入的行数。你还有一大堆函数可以帮助解析文本等等。这是awk脚本的完整版本。基本上为你布置了一切,并自己进行行计数,所以你不必管道到wc -l

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

Appendage 附加物

OP gets the following output: OP获得以下输出:

tcp       0      0  0.0.0.0:8080        0.0.0.0:*       LISTEN

In this case, try either: 在这种情况下,请尝试:

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

Where the \\. 在哪里\\. is replaced by a : or... 被...替换为:或...

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

Which uses [\\.:] which will get it whether it's a colon or a period. 使用[\\.:]将获得它无论是冒号还是句号。 Heck, might as well go all the way... 哎呀,不妨一路走下去......

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

The [^0-9] means not a numeric character. [^0-9]表示不是数字字符。 You can't go wrong with that. 你不能错。 This way, whether it's a period, a colon, a semi-colon, a comma, or whatever separator your version of netstat uses, it will work. 这样,无论是句点,冒号,分号,逗号,还是netstat版本使用的任何分隔符,都可以。

Instead of grepping the output of netstat -- asking for more information than you need and then discarding the bulk of it -- simply ask fuser which process has the port you care about open: 而不是grepping netstat的输出 - 要求提供超出你需要的信息然后丢弃它的大部分 - 只需要询问fuser哪个进程有你关心的端口打开:

$ fuser -n tcp 4005
4005/tcp:            19339

If you only care to know if any process has the port in question open, you can do this even more quickly and efficiently, without needing to process output at all, by using the -q argument to fuser and operating on its exit status: 如果您只关心是否有任何进程打开了相关端口,您可以更快速有效地执行此操作,而无需根据fuser使用-q参数并对其退出状态进行操作来处理输出:

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

If you want to know how many connections there are to a remote port, fuser can do that too: 如果您想知道远程端口有多少连接,热熔器也可以这样做:

fuser -n tcp ,,80

...or connections to a remote port on a specific host: ...或与特定主机上的远程端口的连接:

fuser -n tcp ,1.2.3.4,80

In short -- using the right tool for the job to query for the parameters you want directly prevents you from needing to do text-based filtering in the first place. 简而言之 - 使用正确的工具来查询您想要的参数直接阻止您首先需要进行基于文本的过滤。

使用grep ":80 "而不是grep 80

You can take advantage of the fact that ports are preceded by a colon (like :80 ). 您可以利用端口前面有冒号(如:80 )这一事实。 The fasting thing then would be something like: 那么禁食就像是:

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

That way you're not invoking grep twice. 这样你就不会两次调用grep了。 Also, grep has the ability to count matches with -c , so you can use that instead of wc : 此外,grep能够使用-c计算匹配,因此您可以使用它来代替wc

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

The command you search for is: netstat -ntpl | grep ':PORT ' 您搜索的命令是: netstat -ntpl | grep ':PORT ' netstat -ntpl | grep ':PORT '

Example: 例:

$ 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

如果你想让它更强大,你可以做到

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM