【问题标题】:How to capture the output of telnet command in a variable in Shell script如何在 Shell 脚本的变量中捕获 telnet 命令的输出
【发布时间】:2021-01-25 11:58:25
【问题描述】:

我需要使用 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"

【问题讨论】:

    标签: linux shell telnet


    【解决方案1】:

    试试这个:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 2011-06-28
      • 1970-01-01
      • 2018-11-06
      • 2020-08-30
      相关资源
      最近更新 更多