linux - 在 Linux 上使用 Expect 模块将屏幕输出捕获到文件

标签 linux shell expect

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

相关文章:

c - 在 C.Fork() 中制作自定义 shell

linux - 如何创建 shell 脚本

linux - 在终端中使用带有 curl 的 "if"语句?

linux - 在 *nix cd 命令中将目录更改为最后一个匹配项

linux - 用/etc/environment 中的输出替换命令

bash - Unix:递归解压缩相应文件夹中的 .zip 文件

shell - 在 Expect 脚本中比较字符串的 If 条件

shell - xdotool : how to wait so file opens and then execute ( xdotool windowactivate --sync $a )

linux - 使用 Expect 的 SFTP

linux - "$# -ne 4"在 bash 中是什么意思?