我需要从使用 Expect 脚本执行的许多命令中捕获特定命令的屏幕输出。该脚本在不捕获该命令的输出的情况下运行得很好,但是它没有达到目的。
#! /usr/bin/expect -f
set systemTime [clock seconds]
set now [clock format $systemTime -format "%m_%d_%y"]
exec touch ASR$now.txt
set Password "prizer"
set password "banger"
set bye "exit"
set quitconsole "q"
set exitcons "logout"
spawn ssh admin@192.168.95.96
expect "password:"
send "$password\r"
sleep 5
expect "admin>"
send "32\r"
sleep 5
send "\r"
expect "ASR1001>"
send "enable\r"
expect "Password:"
send "$Password\r"
expect "ASR1001#"
send "terminal length 0\r"
expect "ASR1001#"
send "show run \r"
set output [open "ASR$now.txt" "w"] **<<<<<<<<<<<<<<< Fails here**
send "$bye\r"
sleep 5
send "ctrl-]"
sleep 5
send "$quitconsole\r"
expect "admin>"
send "$exitcons\r"
expect eof
我还想知道如何使用条件语句执行期望脚本,如下所示 仅当某个命令的输出为负或 时才执行命令列表。这类似于执行 grep 并使用 if、else 或 while 语句进一步执行脚本。
谢谢 阿维纳什
最佳答案
我们可以使用 script 命令捕获 Unix/Linux 终端的屏幕输出
命令的用法解释清楚here
关于linux - 在 Linux 上使用 Expect 模块将屏幕输出捕获到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21800950/