Linux

期望腳本在 linux 機器上不起作用

  • January 30, 2021

當我必須對下面的一台機器執行 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 ~ # 

我正在通過期望腳本嘗試同樣的事情,它會拋出如下錯誤。

./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).

以下是我的期望腳本:

#!/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"

任何機構都可以建議,我該如何解決這個問題。

在 TCL 中,

$$ $$pair 是對執行命令替換的呼叫,就像 $( ) 在 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"

通常你想要更多的東西,讓提示是可選的,例如

#!/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"}
   {# }
}

引用自:https://unix.stackexchange.com/questions/631595