2013-12-21 50 views
0

我想写一个bash脚本,使用期望scp文件到远程系统。该预期块我到目前为止是这样包括期望的条件语句

expect -c " 
    set timeout -1 
    spawn scp $file [email protected]:$file 
    expect "\Are you sure you want to continue connection (yes/no)\" 
    send -- \"$password\r\" 
    expect eof 
    " 

的问题是,这种处理在该主机不是已知的主体的情况下,并问我是否要继续连接。我想为主机已知的情况添加一个选项,它只需要密码。

另一个问题是我想处理用户输入的密码不正确的事件。在这种情况下,我想让用户重新输入密码。

什么是使用bash实现这一目标的最佳方法?

非常感谢提前!

+0

如果提示'...(是/否)',你不想回应'是'吗?祝你好运。 – shellter

回答

0

主机不是已知的主机并问我是否要继续连接

用途:scp -o "StrictHostKeyChecking no" <...>

事件中,用户输入的密码不正确

如果您的脚本被认为是互动的,为什么您使用expectscp可以自行询问并重新询问密码。

0

像这样:

expect -c " 
    set timeout -1 
    spawn scp $file [email protected]:$file 
    expect 
     {Are you sure you want to continue connection (yes/no)} { 
      send \"yes\r\" 
      exp_continue 
     } 
     {Password: } { 
      send -- \"$password\r\" 
     } 
    } 
    expect eof 
" 

exp_continue命令返回到含有期望命令,使其他的模式必须匹配的变化。