使用期望通过SSH远程执行脚本



我是使用expect的新手。我尝试以下代码,但失败:

expect -c 'spawn ssh user@host < script.ecma3  
expect Password: ;
send "******r";
send "exitr";'    

任何人都可以澄清

这可能会帮助您

#!/usr/bin/expect
set timeout -1
spawn -noecho bash -c "ssh -t user@host '<here-comes-your-command>'"
expect {
  -re ".*assword.*" {
    exp_send "$env(PASS_WORD)n"
    exp_continue
  }
}

注意: -

1)在运行之前将脚本复制到远程主机。传递整个脚本不是好事。

2)要访问预期中的环境变量,使用$env(variable_name)

在上面的示例中,对于$PASS_WORD,我使用了$env(PASS_WORD)

最新更新