Bash 脚本连接到开放端口、发送请求、读取响应

发布于 2024-08-13 14:40:07 字数 147 浏览 5 评论 0原文

我有一个服务在 localhost:port 上运行。在 bash 脚本中,我想连接到此端口,发送请求,然后读回响应 - 本质上是自动化 telnet 会话。这样做的最佳方法是什么?我正在查看 /dev/tcp、netcat 或 telnet/expect。

谢谢

I have a service running on localhost:port. In a bash script I want to connect to this port, send a request, and read back the response - essentially automating a telnet session. What's the best way of doing this? Am looking at /dev/tcp, netcat or telnet/expect.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

各自安好 2024-08-20 14:40:07

netcat 应该几乎能够做你想做的事。

netcat should pretty much be able to do what you want.

渡你暖光 2024-08-20 14:40:07

如果您能让 Expect 做您想做的事情,那就太好了:只需阅读手册页即可轻松上手并使用它。

Expect is great if you can get it to do what you'd like: it's easy to pickup and work with just by reading the man page.

悲凉≈ 2024-08-20 14:40:07

Expect 就是为了完成这项精确的任务而构建的。它不仅可以处理客户端/服务器对话,还可以处理超时、断开连接等情况。它有多种实现方式(Perl 和 Tcl 是我想到的两个),因此您可以选择一种适合您当前的情况。标准和环境。

Expect was built to do this precise task. It will not only handle the client/server dialog, but also the scenarios of timeouts, disconnects etc. It's available in multiple implementations (Perl and Tcl are two off the top of my head) so you can choose one to fit in with your current standards and environment.

孤蝉 2024-08-20 14:40:07

我相信你可以自动执行此操作,我记得看到 telnet 命令(使用 Ctrl+M 标记来模拟回车)并将这些命令放入文本文件中并发出

telnet somehost

输出被发送到标准输出(您的终端)然后就可以被捕获。

希望这有帮助,
此致,
汤姆.

PS:这是在命令行上使用此命令找到的内容的链接 这里

I believe you can automate this, I recall seeing the telnet commands (using Ctrl+M markers for simulating a carriage return) and putting these commands into a text file and issuing

telnet somehost

And the output was sent out to the standard output (your terminal) which can then be captured.

Hope this helps,
Best regards,
Tom.

P.S: Here's a link to something found using this on the command line here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文