Linux script to parse telnet message and exit

I am connecting to a telnet listener. The Telnet server sends "1234" every second. I want to read the message "1234" and close the telnet session. Here is my code below, 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?

+6
source share
5 answers

You can use the internal TCP mechanism:

 #!/bin/bash exec 3<>/dev/tcp/127.0.0.1/80 # echo -en "eventualy send something to the server\n" >&3 RESPONSE="`cat <&3`" echo "Response is: $RESPONSE" 

Or you can use nc (netcat), but please do not use telnet!

+5
source

Redirect output to file and read from file

 telnet [ip-address] > /tmp/tempfile.txt 
+1
source

The simplest and easiest method is given below.

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

n - timeout in seconds before automatic exit. It can be fractional, like 0.5. Please note that some required output may not return at the specified timeout. Therefore, we may need to increase accordingly.

server — The IP address of the target server or host name.

port — The destination service port number.

You can also redirect the output to a file as follows:

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

you need to use expect, I'm afraid this is the case when you need to check the manual from there, since expect it to actually be its own language to analyze application output and impact on the results

0
source

Already answered, but here is another point of view that uses curl, useful for quick checks (i.e. the service is active or not). I had to struggle a bit with the script and wait solutions.

Just a stub for a possible POP3 check:

 echo "quit" | curl telnet://localhost:110 > /tmp/telnet_session.txt if grep "POP3 ready" /tmp/telnet_session.txt; then echo "POP3 OK" else echo "POP3 KO" fi 
0
source

Source: https://habr.com/ru/post/913313/


All Articles