Bash 脚本连接到开放端口、发送请求、读取响应
我有一个服务在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
netcat 应该几乎能够做你想做的事。
netcat should pretty much be able to do what you want.
如果您能让 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.
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.
我相信你可以自动执行此操作,我记得看到 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.