bash 如何通过 TCP 匹配模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2070526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:33:06  来源:igfitidea点击:

How can I match a pattern over TCP?

bashtcpmonitoringexpect

提问by james

I'm trying to write a monitoring script. It should connect to some port on my server, read the output, and if the output is the expected value, print 1 otherwise 0.

我正在尝试编写一个监控脚本。它应该连接到我服务器上的某个端口,读取输出,如果输出是预期值,则打印 1,否则打印 0。

I'm working on a solution involving cat < /dev/tcp/hostname/port, but the solution eludes me. Maybe something involving expect? I'd prefer a bash script solution. Help appreciated!

我正在研究一个涉及 的解决方案cat < /dev/tcp/hostname/port,但该解决方案让我望而却步。也许涉及期望的东西?我更喜欢 bash 脚本解决方案。帮助表示赞赏!

回答by JB.

All other solutions use netcat for network support, but you can do away with it if your bash is recent enough and compiled with --enable-net-redirections(which is, as far as I can tell, all but Debian and Ubuntu; feel free to comment if I should adjust).

所有其他解决方案都使用 netcat 来提供网络支持,但如果您的 bash 足够新并使用--enable-net-redirections它编译(据我所知,除 Debian 和 Ubuntu 之外的所有解决方案;如果我应该调整,请随时发表评论) )。

Then grepcan do the actual testing. Its return code is a shell one (0 for success, nonzero for failure), so you'd need to invert that, but bashhandles it just fine.

然后grep就可以进行实际测试了。它的返回码是一个 shell(0 表示成功,非零表示失败),所以你需要反转它,但bash处理它就好了。

In a nutshell:

简而言之:

< /dev/tcp/host/port grep -q 'expected value'
echo $(( 1 - $? ))

回答by catwalk

you can use netcat:

你可以使用netcat

echo "GET /"|netcat google.com 80

and then pipe the output to your processing script

然后将输出通过管道传输到您的处理脚本

回答by Emil Vikstr?m

#!/bin/bash
EXPECTED="hello"
SERVER="example.com"
PORT="123"

netcat $SERVER $PORT | grep -F -q "$EXPECTED"

if [ $? ]; then
  echo 1
else
  echo 0
fi

回答by ezpz

I would suggest you use a popen approach. That way you can read with whatever command you like (i.e. nc) and handle the output iteratively. This can all be spawned by a bash script if necessary.

我建议您使用 popen 方法。这样你就可以用你喜欢的任何命令(即nc)读取并迭代处理输出。如有必要,这一切都可以由 bash 脚本生成。

For example:

例如:

#!/usr/bin/env ruby

IO.popen("nc host 4560") do |stdout|
   while line = stdout.gets
      if line =~ /pattern/
         puts "1"
      else
         puts "0"
      end
   end
end

Obviously you would need to substitute patternwith whatever you were looking for. This has the advantage of comparing each line instead of the entire output of the command. I'm not sure which is preferable in your case. I assume from you mentioning expectthat you want a line-by-line approach.

显然,你需要pattern用你想要的任何东西来代替。这具有比较每一行而不是命令的整个输出的优点。我不确定在你的情况下哪个更可取。我假设你提到expect你想要一种逐行的方法。

回答by ghostdog74

there are little examples here. you can take reference to it. However, you should note that it also depends on what services runs on your port and what protocol it speaks.

有一点的例子在这里。你可以参考一下。但是,您应该注意,它还取决于您的端口上运行的服务以及它所使用的协议。

if you are connecting to web server, common port is 80. But if you running your own application running as a server, make sure your script knows how to "talk" to each other.

如果您连接到 Web 服务器,公共端口是 80。但是如果您运行自己的应用程序作为服务器运行,请确保您的脚本知道如何相互“交谈”。

another resource you can look at here. Its gawk though, so if you have gawk, then you may want to try it. (you can "port" to bash too)

您可以在此处查看另一个资源。不过,它的傻瓜,所以如果你有傻瓜,那么你可能想尝试一下。(你也可以“移植”到 bash)

回答by kenorb

Here is the script in Bash shell which involves HTTP request:

这是 Bash shell 中涉及 HTTP 请求的脚本:

exec {stream}<>/dev/tcp/example.com/80
printf "GET / HTTP/1.1\nHost: example.com\nConnection: close\n\n" >&${stream}
grep Example <&${stream}
[ $? -eq 0 ] && echo Pattern match || echo Pattern not match

See also: More on Using Bash's Built-in /dev/tcp File (TCP/IP).

另请参阅:有关使用 Bash 的内置 /dev/tcp 文件 (TCP/IP) 的更多信息