简体   繁体   中英

How to capture the output of telnet command in a variable in Shell script

I need to run the te.net command on a remote server using shell script and have to capture the output. When i execute the below, it is not getting completed but instead getting hung. Can someone please advise how to terminate or timeout the te.net command using shell script once it is executed.

telnet_output=`telnet $server $port`
echo "Output is $telnet_output"

I tried writing it to a file as well. But this is also getting hung when executed in remote server.

opfile="telop.log"
telnet_output=`telnet $server $port | tee $opfile`
op=`cat $opfile`
echo "$op"

Try this:

telnet_output="$({ sleep 1; echo $'\e'; } | telnet $server $port 2>&1)"
printf "Output is\n%s\n" "$telnet_output"

echo $'\e' sends an escape character to te.net to terminate it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM