微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

bash – 捕获期望ssh输出变量

他是bash脚本的新手,并想知道如何将ssh命令的输出捕获到bash变量中?我环顾四周,似乎无法正确行事.我已经尝试过puts $expect_out(缓冲区),但是当echo它表示变量不存在时

我知道响应应该只是一行,如果我想将其保存到变量响应然后回显它我该怎么做?

一般概念可以如下所示.

>产生ssh会话
>正确登录
>使用send发送每个命令
>等待期望的输出

例:

spawn ssh $user@$domain
expect "password" { send "$pwd\r"}
expect "#"; # This '#' is nothing but the terminal prompt
send "$cmd\r"
expect "#"
puts $expect_out(buffer);  #Will print the output of the 'cmd' output Now.

执行命令后要等待的单词可能因系统而异.它可以是#或$或>要么 :;所以,确保你给出了正确的.或者,您可以为提示提供通用模式

set prompt "#|>|:|\\\$"; # We escaped the `$` symbol with backslash to match literal '$'

在发送命令后使用expect时,可以将其用作

expect -re $prompt; #Using regex to match the pattern`

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐