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
netcat should pretty much be able to do what you want.
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 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.
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 somehostAnd 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.
精彩评论