0

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

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

2 Comments

As far as I can tell with expect though, is that it actually expects a particular response, e.g "Password". In this case, I don't know what I'll be getting back - I just want to read whatever comes back.
I think you should be able to wildcard this
0

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.

1 Comment

As mentioned by antik & Brian Agnew, it seems expect is the much easier solution. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.