2012-05-11 137 views
5

Stackoverflow的新功能,所以首先,你好。ssh使用python沒有RSA密鑰

我正在爲我的學校開發一個小項目,該項目應該是開源Unison程序的一個自定義gui(用python作爲教育挑戰,因爲我從來沒有用過python)。我們正試圖讓學生和員工通過儘可能少的輸入來啓動此程序(如果您願意,可以在白板上打字),從而在家中和學校同步文件夾。界面應該只是他們的學校用戶名和密碼,gui包裝應該只是將用戶名和密碼發送到Unison並同步它們。

問題是Unison反過來啓動SSh並提示輸入密碼,但是python subprocess.communicate(input)方法不會讓ssh獲取密碼。我意識到ssh只會接受來自終端的輸入,我無法弄清楚如何欺騙它。我讀過一些關於使用僞終端的東西,但我仍然難倒了。 RSA密鑰將是理想的解決方案,但生成它們然後將它們放置在遠程服務器上仍然需要我至少使用一次密碼登錄,這需要上述解決方案,或者終端不是白癡證明。如果我在貼吧。而且任何一般蟒蛇提示也將是如果你願意讚賞

def startSync(self): 
    ''' 
    ''' 
    userName = self.userNameIn.get() 
    userPass = self.userPassIn.get() 
    localDir = "/Users/localuser/syncFolder/" 
    remoteDir = " ssh://schoolServer/remotesyncFolder" #for testing purposes, I set this to my own home machine which logs into my own account if I don't provide [email protected] 
    unisonExecRemotePath = " -servercmd /Users/RemoteMe/unison" #unison is the unix executable responsible for launching unison on the remote system 
    silenceCmdPrompts = " -silent" #keeps unison from displaying directory differences and asking if its okay to sync 
    executionString = "./unison" + localDir + remoteDir + unisonExecRemotePath + silenceCmdPrompts 

    mainProcess = subprocess.Popen(executionString,shell = True, stdin = subprocess.PIPE) 
    mainProcess.communicate(userPass) 

執行字符串工作在那裏終端的罰款。

謝謝!

齊奏用戶手冊:http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html

編輯:我還要指出的是,雖然我目前OSX和Linux下的發展,我最終將不得不作出這個Windows兼容的,因爲我的大部分學校的學生運行Windows作爲他們的主要(或唯一)機器。

回答

3

調查pexpect

import pexpect 

child = pexpect.spawn('ssh myna[email protected]') 
child.expect('Password:') 
child.sendline(mypassword) 
child.interact() 
+0

我只是試圖測試這個ouside我的項目,正是你在那裏(顯然與實際主機雖然),沒有任何反應。稍微停頓一下,然後回到沒有消息的bash提示符。 我嘗試從我的上面的代碼中添加整個執行行,也許它只是關閉,如果沒有給它,但它做同樣的事情。 – Hiroshi

+0

確保您傳遞給child.expect()的參數與服務器爲您提供的密碼提示匹配。另外,您可能需要在child.sendline(mypassword)之後調用child.interact()。自從我使用pexpect以來,這已經有一段時間了,但它是工作的工具。 –

+0

工作!謝謝! :) – Hiroshi

2

如果您想發送一個密碼ssh你需要打開僞終端(一pty),並使用它對話,而不是僅僅使用stdin/stdout說。看看pexpect模塊,該模塊旨在完成此操作。

另一種解決方案將涉及某種帶外分發公鑰的機制:例如,設置一個簡單的Web應用程序,人們可以粘貼公鑰並讓其代表管理authorized_keys文件用戶。