Bash 在expect中包含条件语句

Bash 在expect中包含条件语句,bash,ssh,expect,Bash,Ssh,Expect,我正在尝试编写一个bash脚本,它使用expect将文件scp到远程系统。到目前为止,我使用的expect块如下所示 expect -c " set timeout -1 spawn scp $file user@host:$file expect "\Are you sure you want to continue connection (yes/no)\" send -- \"$password\r\" expect eof " 问题是,这

我正在尝试编写一个bash脚本,它使用expect将文件scp到远程系统。到目前为止,我使用的expect块如下所示

expect -c "
    set timeout -1
    spawn scp $file user@host:$file
    expect "\Are you sure you want to continue connection (yes/no)\"
    send -- \"$password\r\"
    expect eof
    "
问题是,这会处理主机不是已知主机的情况,并询问是否要继续连接。我想添加一个选项,用于主机已知且只需要密码的情况

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

使用bash和expect实现这一点的最佳方法是什么

非常感谢

主机不是已知主机,它会询问我是否要继续连接

使用:
scp-o“StrictHostKeyChecking no”

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

如果您的脚本被认为是交互式的,那么为什么要使用
expect
scp
可以自行询问和重新询问密码。

如下:

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

exp\u continue
命令循环回包含expect的命令,以便其他模式有一个要匹配的更改。

如果提示您
…(yes/no)
,是否要响应
yes
?祝你好运