Expect 脚本无法在 Linux 机器上运行

Expect 脚本无法在 Linux 机器上运行

当我必须对其中一台机器执行 ssh 时,下面是命令,如果我输入“yes”,它就可以正常工作并能够按如下所示登录。

ssh [email protected]
The authenticity of host '192.168.1.177 (192.168.1.177)' can't be established.
ED25519 key fingerprint is SHA256:KqI6oKKY1JOH+OJZzCYObPdkMVNNwhkaMGTYgx/fDxE.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.1.177' (ED25519) to the list of known hosts.
localhost ~ # 

我正在通过期望脚本尝试同样的事情,它会抛出如下错误。

 ./expectscriptssh.sh 192.168.1.177
spawn ssh [email protected]
invalid command name "fingerprint"
    while executing
"fingerprint"
    invoked from within
"expect "Are you sure you want to continue connecting (yes/no/[fingerprint])? ""
    (file "./expectscriptssh.sh" line 4).

以下是我的期望脚本:

#!/usr/bin/expect -f
set VAR [lindex $argv 1]
spawn ssh root@$argv 
expect "Are you sure you want to continue connecting (yes/no/[fingerprint])? "
send "yes\r"

任何人都可以建议,我该如何解决这个问题。

答案1

在 TCL 中,[ ] 对是执行命令替换的调用,就像 POSIX shell 中的 $( ) 一样。

如果您有autoexpect可用的资源,那么编写 Expect 脚本的最简单方法是使用 autoexpect 来监视您执行某些操作,然后编辑生成的脚本以删除不需要的内容。

您可以将“...”更改为 {...} 以避免对字符串进行求值。

#!/usr/bin/expect -f
set VAR [lindex $argv 1]
spawn ssh root@$argv 
expect {Are you sure you want to continue connecting (yes/no/[fingerprint])? }
send "yes\r"

通常你想要更多的东西,允许提示是可选的,例如

#!/usr/bin/expect -f
set VAR [lindex $argv 1]
spawn ssh root@$argv 
expect {
    {Are you sure you want to continue connecting (yes/no/[fingerprint])? } {
        exp_send "yes\r"
        exp_continue
    }
    {Password:} {send "secret\r"}
    {# }
}

相关内容