linux - 抑制期望输出

标签 linux bash shell unix expect

我有以下登录路由器并执行命令的脚本。我怎样才能只完成输出而不显示任何其他内容。

#!/usr/bin/expect -f
set timeout 3000
log_user 0
spawn ssh -o StrictHostKeyChecking=no ananair@172.20.60.187
expect {
"*assword:" {
send "anismi@123\n"
expect "*#"
log_user 1
send "show bgp vrf IKA summary | include 10.155.192.50\n"
expect "*#"
}
}

这是上述脚本的输出。

[root@cap-nwmon-01 anand]# ./migrate.sh
show bgp vrf IKA summary | include 10.155.192.50
Sun Feb 25 07:17:25.150 EET
10.155.192.50     0 65256 3107179 4237248  1624156    0    0    5d17h        322

这是我想要从这个脚本中获得的唯一输出。

Sun Feb 25 07:17:25.150 EET
10.155.192.50     0 65256 3107179 4237248  1624156    0    0    5d17h        322

最佳答案

您看到额外的行是因为您在 send "show..." 之前输入了命令 log_user 1,这导致了 "show.. ” 将被记录到标准输出。如果您稍后将此命令移动一行,在 send 之后,并通过添加 expect "\n" 等待回显完成,那么您将看到更少:

expect "#"
send "show bgp vrf IKA summary | include 10.155.192.50\n"
expect "\n"
log_user 1
expect "#"

请注意,模式开头的 * glob 字符没有任何作用。

或者,不要更改 log_user,您应该考虑使用变量 $expect_out(buffer) 来保存与模式匹配的内容,包括在内。

关于linux - 抑制期望输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48970503/

相关文章:

bash - 为什么 bash 按字典顺序而不是数字来评估数字的比较?

linux - 如何将状态代码从远程运行的 shell 脚本返回到本地 shell 脚本

shell - | 的选项(管道)命令

shell - tr [:upper:] [:lower:] with Cyrillic text

linux - 在 Yii2 中创建/迁移 rBac

c - 通过 Linux 套接字发送文件描述符

linux - 文件行在执行时被更改

C静态变量和linux fork

linux - 将目录更改为 bash 中父/调用脚本的路径

bash - 外壳 sha1($salt.$password) 错误