2014-10-11 110 views
0

我已經實現expect腳本來自動化某些服務器上的ssh連接。Expect腳本和ssh

當ssh嘗試第一次打開與服務器的連接時出現問題,我得到了一個是/否的問題。

我必須手動執行連接1,然後它不會再出現,腳本工作良好。

1st:我需要你的幫助才能在期望腳本中找到跳過這個問題的步驟,這有可能嗎?

+0

-oStrictHostKeyChecking = no -oCheckHostIP =沒有加入到ssh就可以解決問題,謝謝:) – user212051 2014-10-11 10:40:35

回答

0
  1. 這裏有一個例子:

    #!/usr/bin/env expect 
    
    spawn ssh [email protected] 
    
    while {1} { 
        expect "(yes/no)?" { 
         send "yes\r" 
        } "password: " { 
         send "xxxxxxxx\r" 
         interact 
         exit 0 
        } 
    } 
    
  2. 而如果你的root目的就是自動的ssh,請有sshpass的嘗試,但sshpass通常不是默認安裝:

    `sshpass -p xxxxxxxx ssh [email protected]` 
    
  3. 這裏還提供了設置公鑰/私鑰對的過程,這使您無需密碼即可訪問目標服務器:

    Generate a key pair, id_dsa.pub id_dsa with following command: 
        ssh-keygen -t dsa -b 1024 
    Move id_dsa to client:/home/xxxx/.ssh/ 
    Move id_dsa.pub to server:/home/xxxx/.ssh/, and "cat id_dsa.pub >> authorized_keys" 
    chmod 600 /home/xxxx/.ssh/* (both the id_dsa and remote authorized_keys) 
    
0
set timeout 30 
spawn ssh -o "StrictHostKeyChecking no" "[email protected]$ip" 
expect { 
    "RSA key fingerprint" {send "yes\r"; exp_continue} 
    -re "<regex for whatever the yes/no question is>" {send "yes\r"; exp_continue} 
    "assword:" {send "$password\r"} 
} 
sleep 5 
interact 

意志把一切都在一個循環,直到超時時間(30秒)。然後它會移動到腳本中的下一行。如果你需要的話,我還增加了對RSA的期望。