Richard H
Richard H

Reputation: 39055

Bash script to connect to open port, send request, read response

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

Upvotes: 0

Views: 4155

Answers (4)

t0mm13b
t0mm13b

Reputation: 34592

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.

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272217

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.

Upvotes: 0

antik
antik

Reputation: 5330

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.

Upvotes: 0

Joey
Joey

Reputation: 354356

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

Upvotes: 1

Related Questions