Expect 脚本在 linux 机器上不起作用

Rag*_*ags 1 linux scripting expect

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

ssh root@192.168.1.177
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 ~ # 
Run Code Online (Sandbox Code Playgroud)

我正在通过expect脚本尝试同样的事情,它会引发如下错误。

 ./expectscriptssh.sh 192.168.1.177
spawn ssh root@192.168.1.177
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).
Run Code Online (Sandbox Code Playgroud)

以下是我的期望脚本:

#!/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"
Run Code Online (Sandbox Code Playgroud)

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

ica*_*rus 5

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

如果autoexpect可用,那么编写期望脚本的最简单方法是使用 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"
Run Code Online (Sandbox Code Playgroud)

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

#!/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"}
    {# }
}
Run Code Online (Sandbox Code Playgroud)