如何保存telnet脚本的响应?

时间:2017-02-03 16:27:28

标签: linux bash

我的脚本可以连接,登录并运行命令。但是我不知道如何将命令的响应保存到文件中,而不保存整个会话。

#!/bin/sh
Var=1
while [ $Var -lt 20 ]
do
HOST='IPa.ddr.ess.'$Var
USER='MyUser'
PASSWD='MyPassword'
CMD='MyCommand'

(
    echo open "$HOST"
    sleep 1
    echo "$USER"
    sleep 1
    echo "$PASSWD"
    sleep 1
    echo "$CMD"
    #I want to save the output from my $cmd to an varaible $Output
    #Then I want to write "$HOST - $Output" to a file named "output.txt"
    sleep 2
    echo "exit"
    ) | telnet
Var=$((Var + 1))
done

我会感谢任何帮助,或指向正确方向的指示

1 个答案:

答案 0 :(得分:0)

好吧,这看起来比我最初的想法更具挑战性。我喜欢它: - )

#!/bin/sh
Var=1
while [ $Var -lt 20 ]
do
HOST='IPa.ddr.ess.'$Var
USER='MyUser'
PASSWD='MyPassword'
CMD='MyCommand'
MARKER='XXXX1234:AUIE'

(echo "$HOST - " ; (
    echo unset echo
    echo open "$HOST"
    sleep 1
    echo "$USER"
    sleep 1
    echo "$PASSWD"
    sleep 1
    echo echo "$MARKER"
    echo "$CMD"
    #I want to save the output from my $cmd to an varaible $Output
    #Then I want to write "$HOST - $Output" to a file named "output.txt"
    sleep 2
    echo "exit"
    ) | telnet | sed -e "1,/$MARKER/d" ) >> output.txt
Var=$((Var + 1))
done

这是做什么的:

  • 它禁用了telnet中的回显
  • 登录会话后,会打印标记
  • 将标记保存到output.txt
  • 后的任何内容
  • 我将另一个shell打印出来,打印出“$ HOST - ”部分