2012-03-23 19 views
3

首先,我試圖讓織物工作,但它一直要求我輸入密碼。paramiko,不是跟ssh-agent說話。在織物中的相同行爲

所以我試圖減少這個問題。也許從Python創建一個SSH連接將是一個很好的POC。我發現fabric使用parmiko進行SSH處理。嗯。好的,讓我們試着讓一個例子工作。

這是我寫的。

from ssh import * 
import os 

print "SSH-AGENT VARS" 

print "SSH_AGENT_PID: %s " % os.environ['SSH_AGENT_PID'] 
print "SSH_AUTH_SOCK: %s " % os.environ['SSH_AUTH_SOCK'] 

a = Agent() 
keys=a.get_keys() 
print keys.count("192.168.1.10") 


client = SSHClient() 
client.load_system_host_keys() 
client.connect('192.168.1.10') 

以下錯誤信息所導致的:

% ./ssh_test.py 
SSH-AGENT VARS 
SSH_AGENT_PID: 26557 
SSH_AUTH_SOCK: /tmp/ssh-pZHBElj26556/agent.26556 
0 
Traceback (most recent call last): 
    File "./ssh_test.py", line 18, in <module> 
    client.connect('192.168.1.10') 
    File "/usr/local/lib/python2.7/dist-packages/ssh/client.py", line 332, in connect 
    self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys) 
    File "/usr/local/lib/python2.7/dist-packages/ssh/client.py", line 493, in _auth 
    raise saved_exception 
ssh.PasswordRequiredException: Private key file is encrypted 

的ssh-agent在我的會話中運行的,我可以ssh到那個盒子,沒有問題,它並沒有提示我輸入密碼或任何東西。

我猜paramiko無法連接到正在運行的ssh-agent的一些奇怪的原因。

有沒有其他人有過這樣的問題?我正在使用Ubuntu 11.10

我似乎記得嘗試使用Fabric並且遇到類似的問題,可能它已被打破了一段時間?

我連接,只使用主機名作爲參數。這是根據文件。

http://www.lag.net/paramiko/docs/paramiko.SSHClient-class.html

connect(self, hostname, port=22, username=None, password=None, pkey=None, key_filename=None, timeout=None, allow_agent=True, look_for_keys=True, compress=False) 
+1

我使用的面料在Ubuntu 11.10也沒有任何問題。這聽起來像是光纖網不能訪問與您的控制檯ssh相同的密鑰文件,或者您使用了錯誤的登錄名。你記得在你的fabfile中設置'env.user ='my_user''嗎? – 2012-03-26 14:44:09

+0

試過了,同樣的問題。我猜猜問題在於paramiko而不是織物。讓paramiko先工作將是一個好的一步。這就是爲什麼我創建了上面的小測試腳本。 – 2012-03-26 15:43:44

+1

您是否嘗試過創建分支新的RSA密鑰,確保它沒有密碼,在服務器上安裝並使用它? – 2012-03-26 15:46:38

回答

4

好的,所以我發現的第一件事就是Paramiko已經過時了,並且沒有維護。

它現在被稱爲ssh軟件包,至少在Ubuntu的,並有不同的維護者(bitprophet)

下面是一個示例會話,使用它:

$ ./ssh_demo.py 
Hostname: 192.168.1.10 
*** Host key OK. 
Username [bryan]: root 
Trying ssh-agent key eee5638f390e1698898984b10adfa9317 ... success! 
*** Here we go! 

Linux top.secret.com 2.9.37-1-amd64 #1 SMP Thu Nov 3 03:41:26 UTC 2011 x86_64 
┌┌([email protected])-(10:44am-:-03/27)┌-¨-¨¨˙ 

那並沒有回答爲什麼布料未認證針對的ssh-agent正確思想的問題。所以問題仍然存在。

更新:

由於摩根的提示,我已經得到了遠一點這個問題。正如他所建議的,我通過將以下內容添加到我的fabfile的頂部來啓用ssh日誌記錄:我還監視了服務器日誌。在這樣做的時候,我發現我指定的用戶被忽略,而我的本地用戶名被使用。

在服務器上:

tail -f /var/log/auth.log 
Mar 28 11:12:36 xxxxxxxxxxx sshd[17652]: Invalid user bryan from xxx.xxx.xxx.xxx 

本地:

tail -f paramiko.log  
DEB [20120328-11:39:29.038] thr=1 ssh.transport: starting thread (client mode): 0x8dfc66cL 
INF [20120328-11:39:29.066] thr=1 ssh.transport: Connected (version 2.0, client OpenSSH_5.5p1) 
DEB [20120328-11:39:29.093] thr=1 ssh.transport: kex algos:['diffie-hellman-group-exchange-sha256', 'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', '[email protected]'] server encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', '[email protected]'] client mac:['hmac-md5', 'hmac-sha1', '[email protected]', 'hmac-ripemd160', '[email protected]', 'hmac-sha1-96', 'hmac-md5-96'] server mac:['hmac-md5', 'hmac-sha1', '[email protected]', 'hmac-ripemd160', '[email protected]', 'hmac-sha1-96', 'hmac-md5-96'] client compress:['none', '[email protected]'] server compress:['none', '[email protected]'] client lang:[''] server lang:[''] kex follows?False 
DEB [20120328-11:39:29.093] thr=1 ssh.transport: Ciphers agreed: local=aes128-ctr, remote=aes128-ctr 
DEB [20120328-11:39:29.093] thr=1 ssh.transport: using kex diffie-hellman-group1-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, remote none 
DEB [20120328-11:39:29.183] thr=1 ssh.transport: Switch to new keys ... 
DEB [20120328-11:39:29.224] thr=2 ssh.transport: Trying SSH agent key cda5638f390e166864444b1093b91017 
DEB [20120328-11:39:29.272] thr=1 ssh.transport: userauth is OK 
INF [20120328-11:39:53.310] thr=1 ssh.transport: Authentication (publickey) failed. 
DEB [20120328-11:41:29.076] thr=1 ssh.transport: EOF in transport thread 

嗯,這很奇怪,我跑的命令: FAB diskfree -H xxx.xxx.xxx.xxx -u根

但這是什麼?

$ cat ./fabfile.py 
from fabric.api import * 
import ssh 
ssh.util.log_to_file("paramiko.log", 10) 

env.user = 'bryan' 

def host_type(): 
run('uname -s') 

def diskfree(): 
run('df -h') 

env.user = 'bryan' 

莫非是問題的根源在哪裏? ssh錯誤信息是否會誤導我?

我刪除了該行,它工作,所以我想是的,是答案。

+0

你可能在你的.ssh/config中有一個配置文件,你忘記了或者想到了,但是我喜歡在謹慎的方面犯錯,並且保留在那裏的用戶中,或者在其中一個實例中將它們編碼爲主持人列表與列表解析。很高興這個提示讓你找出問題。 – Morgan 2012-03-28 14:27:26

0

我會嘗試以指定的密碼作爲關鍵字password參數connect()

如文檔SSHCLient.connect()所述,它使用任何PKey它可以在系統中找到,如果沒有提供特定的。類方法from_private_key()from_private_key_file()(我不確定它們中的哪一個被調用,可能都是)採用可選參數password。文檔說,

如果私鑰被加密且密碼不是無,則給定的密碼將用於解密密鑰(否則引發PasswordRequiredException)。

......這可能是你的情況發生了什麼。

+1

但ssh-agent正在運行,並在我的控制檯中進行了身份驗證。我可以在不提供密碼的情況下ssh到服務器。我的問題是如何與paramiko實現這一點。我不想將密碼放在代碼中,否則會影響練習的整個目的。 – 2012-03-27 08:52:02

4

所以從paramiko代碼和你的當你做a.get_keys()應該返回一個列表。我會看到它返回。而且它不會返回可以計算的東西,因爲它返回實際加密的密鑰位。但無論如何,當你轉移到ssh,並且工作,讓我們移動到Fabric。

您可以通過做打開它爲SSH lib中獲得更多的日誌記錄:

import ssh 
ssh.util.log_to_file("paramiko.log", 10) 

在你fabfile。這將記錄所有日誌,並顯示更多paramiko/ssh本身正在做的事情,這可以幫助您進一步調試問題。

+1

嘿,好的提示。我將把額外的信息放入原始發佈中。原來它忽略了指定的用戶。 – 2012-03-28 10:41:22

相關問題