使用expect通过ssh远程执行脚本

问题描述:

我是使用expect的新手。我尝试下面的代码,它失败:使用expect通过ssh远程执行脚本

expect -c 'spawn ssh [email protected] < script.ecma3 
expect Password: ; 
send "******\r"; 
send "exit\r";'  

任何人都可以澄清

+1

[在bash脚本中预期]的可能重复(http://stackoverflow.com/questions/42353148/expect-within-bash-script) – mihir6692

这可以帮助你

#!/usr/bin/expect 
set timeout -1 

spawn -noecho bash -c "ssh -t [email protected] '<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)