2014-02-20 99 views
0

我對腳本編程不熟悉,你能幫我寫一個期望腳本ssh進入設​​備並提示用戶輸入密碼。我們使用pin + rsa令牌代碼作爲密碼,所以我無法存儲密碼。期望腳本的用戶輸入

感謝您的幫助

#!/usr/bin/expect -f 

spawn ssh device1 

回答

0

我已經使用這個代碼之前達到你想達到的目標。你可以把它作爲參考點來編寫你自己的代碼版本。

#!/usr/bin/env expect -f 
set passw [lindex $argv 0] 

#timeout is a predefined variable in expect which by default is set to 10 sec 
set timeout 60 
spawn ssh [email protected] 
while {1} { 
    expect { 

    eof       {break} 
    "The authenticity of host" {send "yes\r"} 
    "password:"     {send "$password\r"} 
    "*\]"      {send "exit\r"} 
    } 
} 
wait 
#spawn_id is another default variable in expect. 
#It is good practice to close spawn_id handle created by spawn command 
close $spawn_id 

來源:Expect Wiki

1

您將需要使用交互命令自己輸入密碼。 下面的骨架應該讓你在路上:

set prompt "$" 

spawn ssh [email protected] 

set timeout 10 
expect { 
    timeout { 
     puts "Unable to connect" 
     exit 1 
    } 

    #Authenticty Check 
    "*(yes/no)?" { 
     send "yes\r" 
     exp_continue 
    } 

    "assword: " { 
     #Hand control back to user 
     interact -o "\r" exp_continue 
    } 

    "$prompt" { 
     puts "Cool by the pool" 
    } 
}