gpt4 book ai didi

linux - 如何在 Shell 脚本中的变量中捕获 telnet 命令的输出

转载 作者:行者123 更新时间:2023-12-04 16:16:28 25 4
gpt4 key购买 nike

我需要使用 shell 脚本在远程服务器上运行 telnet 命令,并且必须捕获输出。当我执行下面的命令时,它没有完成,而是被挂起。有人可以建议如何在执行后使用 shell 脚本终止或超时 telnet 命令。

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

我也尝试将其写入文件。但这在远程服务器上执行时也会挂起。

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

最佳答案

试试这个:

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

echo $'\e'telnet 发送一个转义字符以终止它。

关于linux - 如何在 Shell 脚本中的变量中捕获 telnet 命令的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64296372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com