Linux script to parse telnet message and exit

user1336117 picture user1336117 · Apr 16, 2012 · Viewed 17.9k times · Source

I am connecting to a telnet listener. Telnet server sends "1234" for every second. I want to read the message "1234" and close the telnet session. Here below is my code but it does not work.

#!/bin/bash
telnet 192.168.10.24 1234
read $RESPONSE
echo "Response is"$RESPONSE
echo "quit"

How can i automatically read the telnet message?

Answer

Seff picture Seff · Jun 13, 2014

The simplest and easiest method is given below.

sleep <n> | telnet <server> <port>

n - The wait time in seconds before auto exit. It could be fractional like 0.5. Note that some required output may not be returned in the specified wait time. So we may need to increase accordingly.

server - The target server IP or hostname.

port - Target service port number.

You can also redirect the output to file like this,

sleep 1 | telnet <server> <port> > output.log