2016-10-13 59 views
0

我是一個系統測試員,並且我有一組特定的DUT可以在預發行版本的固件或候選版本上運行。在預發行版中,我可以通過使用特定用戶帳戶登錄來訪問DUT的Linux核心操作系統。發佈候選人不允許這樣做。Expect腳本:需要一種方法來驗證SSH連接不需要密碼

我寫的腳本的全部基礎是能夠遠程執行駐留在DUT上的腳本。我想檢查一下是否可以訪問需要密碼的Linux核心操作系統。如果我有權訪問,則繼續退出腳本。

我已經嘗試了很多事情,並且在針對發佈候選版進行測試時都會失敗,因爲預期會輸入密碼。這是最新的嘗試:

set status [catch {exec ssh [email protected]$host ls} result] 
    if { [regexp "Password:" $result]} then { 
     # A password was asked for. Fail 
     puts "This is a Release Candidate Version\nAccess to the Linux OS is denied" 
     exit 
    } else { 
     # no password needed. Success 
     puts "This is a Pre-Release version" 
    } 

當針對預發佈版本執行此代碼時,此代碼有效。但是,當需要密碼時,它不會像SSH會話提示輸入密碼並等待輸入。

有沒有人有一個解決方案,將突破所需的密碼方案?

謝謝

+0

我真的很想切換到使用密鑰與SSH,因爲那樣你就不需要有密碼參與。但它確實需要更多的設置。 –

回答

2

如果你有一個情況下到遠程系統連接可能詢問密碼,但並不總是去,你最好做從內部連接期望。這是因爲可以讓expect命令一次等待幾個不同的事情。

set timeout 20; # 20 seconds; if things don't respond within that, we've got problems 

# 'echo OK' because it is quick and produces known output 
spawn ssh [email protected]$host echo OK 

# Wait for the possibilities we know about; note the extra cases! 
expect { 
    "Password:" { 
     # Password was asked for 
     puts "This is a Release Candidate Version\nAccess to the Linux OS is denied" 
     close 
     exit 
    } 
    "OK" { 
     # Password not asked for 
     puts "This is a Pre-Release version" 
    } 
    timeout { 
     puts "There was a network problem? Cannot continue test" 
     close 
     exit 1 
    } 
    eof { 
     puts "Inferior ssh exited early? Cannot continue test" 
     close 
     exit 1 
    } 
} 
close 

# ... now you've checked the connection ... 
+0

這工作正如我所希望的。謝謝。我做的唯一更改是向SSH添加「-q」選項來抑制問候消息。 –