2015-10-07 92 views
0

我試過一個期望腳本,該腳本應該將ssh公鑰複製到遠程服務器並將其複製到位,以便您可以將ssh複製到該文件中wihout下次必須輸入密碼。ssh expect腳本無法識別連接字符串

我想允許連接的第一次期望有關未識別的主機密鑰投訴主機:

#!/usr/bin/expect -f 
set PUB "~/.ssh/id_rsa.pub" 
spawn scp $PUB [email protected]: 
expect { 
"(Are you sure you want to continue connecting yes/no)?" { send "yes\r"; exp_continue } 
    password:            {send secret!\r; exp_continue} 
    } 
expect "password: " 
send "secret!\r" 

這是當我真正運行腳本會發生什麼:

#./bin/ssh-login.ssh 
spawn scp ~/.ssh/id_rsa.pub [email protected]: 
spawn ssh [email protected] /bin/mkdir .ssh && /bin/chmod 700 .ssh && /bin/cat id_rsa.pub >> .ssh/authorized_keys && /bin/chmod 600 .ssh/authorized_keys 
The authenticity of host '100.114.116.106 (100.114.116.106)' can't be established. 
RSA key fingerprint is 3b:04:73:25:24:f3:aa:99:75:a7:98:62:5d:dd:1b:38. 
Are you sure you want to continue connecting (yes/no)? secret! 
Please type 'yes' or 'no': 

基本上問題是,我試圖設置預計「你確定要繼續連接是/否」字符串的行沒有被腳本識別。

有關我如何改善這些行以匹配這些行的建議?

感謝

回答

1

你在你的腳本中有一個錯字 - 關於「你確定......」行左括號應該是「是/否」之前,而不是在開始。固定的版本:

#!/usr/bin/expect -f 
set PUB "~/.ssh/id_rsa.pub" 
spawn scp $PUB [email protected]: 
expect { 
"Are you sure you want to continue connecting (yes/no)?" { send "yes\r"; exp_continue } 
    password:            {send secret!\r; exp_continue} 
    } 
expect "password: " 
send "secret!\r" 

...工作正常,我

+0

是啊!就是這樣。感謝您的輸入! :) – bluethundr

+1

不客氣,我從來沒有聽說過這些'期望'的事情,所以有趣的發現他們;-) – IBam