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`

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...