【问题标题】:Executing a script remotely through ssh using expect使用expect通过ssh远程执行脚本
【发布时间】:2017-08-02 02:47:24
【问题描述】:

我不熟悉使用expect。我尝试下面的代码,但它失败了:

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

谁能解释清楚

【问题讨论】:

标签: ssh stdin expect remote-execution


【解决方案1】:

这可能对你有帮助

#!/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) 访问 expect 中的环境变量,使用$env(variable_name)

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

【讨论】:

    猜你喜欢
    • 2020-01-06
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2014-01-20
    • 1970-01-01
    • 2014-12-07
    • 2017-05-30
    相关资源
    最近更新 更多