他是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`