paramiko的无密码ssh无法授权

时间:2015-08-06 19:57:42

标签: authentication vmware vmware-player

我无法使用paramiko SSHClient进行身份验证。尝试从一个虚拟机转到网络上的另一个盒子。一般的想法是我创建一个公钥/私钥对,使用给定的密码ssh到客户端,获取客户端公钥并将其添加到我的known_hosts。将我的公钥放在客户authorized_keys中。关闭该连接,然后尝试在没有密码的情况下重新连接。它在重新连接时失败了。我正在使用paramiko 1.15.2和python 2.7.10。

本教程中的代码如下:http://www.minvolai.com/blog/2009/09/How-to-ssh-in-python-using-Paramiko/how-to-ssh-in-python-using-paramiko/

$upload_data = $this->upload->data();
$data['images'] = $this->Edit_profile->insert_new_post($upload_data['file_name']);

这是我得到的调试日志:

import paramiko, StringIO, os    
pkey = paramiko.rsakey.RSAKey.generate(1024)    
pub_key = "ssh-rsa %s" % (pkey.get_base64())    
file_obj = StringIO.StringIO()    
pkey.write_private_key(file_obj)    
priv_key = file_obj.getvalue()    
server, username, password = ('host', 'username', 'password')   

ssh = paramiko.SSHClient()    
parmiko.util.log_to_file(log_filename)    
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())    
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))    
ssh.connect(server, username=username, password=password)   

sftp = ssh.open_sftp()    
sftp.get(remote_path, local_path)    
sftp.put(local_path, remote_path)    
sftp.close()    
ssh.close()

key = StringIO.StringIO(priv_key)    
privkey = paramiko.rsakey.RSAKey(key)    
ssh.connect(server, username=username,pkey=privkey )
编辑:让我感到困惑的是,这可以在网络上的两台实际机器之间进行。我可以直接进入apache @ virtualmachine和终端中的apache。我已经验证在ftp.put()期间添加了密钥。虽然我找不到任何关于paramiko的问题,但是从VM出来问题。

EDIT2:使用“look_for_keys = False”给出相同的输出,但只使用给定的键。 注意:它正在使用不同的密钥,因为我今天重新生成了一个与昨天不同的密钥。

DEBUG:paramiko.transport:starting thread (client mode): 0x728ac950L    
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_5.3)    
DEBUG:paramiko.transport:kex algos:[u'diffie-hellman-group-exchange-sha256', u'diffie-hellman-group-exchange-sha1', u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1'] server key:[u'ssh-rsa', u'ssh-dss'] client encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'rijndael-cbc@lysator.liu.se'] server encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'rijndael-cbc@lysator.liu.se'] client mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] server mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] client compress:[u'none', u'zlib@openssh.com'] server compress:[u'none', u'zlib@openssh.com'] client lang:[u''] server lang:[u''] kex follows?False    
DEBUG:paramiko.transport:Ciphers agreed: local=aes128-ctr, remote=aes128-ctr    
DEBUG:paramiko.transport:using kex diffie-hellman-group14-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    
DEBUG:paramiko.transport:Switch to new keys ...    
DEBUG:paramiko.transport:Trying SSH key 36f4e43a968404ef8e7f277e1429f0fd    
DEBUG:paramiko.transport:userauth is OK    
INFO:paramiko.transport:Authentication (publickey) failed.    
DEBUG:paramiko.transport:Trying discovered key 54b98c4b8ba454594e9df58bc8f9b5e7 in /home/apache/.ssh/id_rsa
DEBUG:paramiko.transport:userauth is OK    
INFO:paramiko.transport:Authentication (publickey) failed.    
DEBUG:paramiko.transport:Trying discovered key d2a34d82ebe4439672bd2c16540c5bb4 in /home/apache/.ssh/id_dsa    
DEBUG:paramiko.transport:userauth is OK    
INFO:paramiko.transport:Authentication (publickey) failed.    
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/apache/miniconda/lib/python2.7/site-packages/paramiko-1.15.2-py2.7.egg/paramiko/client.py", line 307, in connect
  File "/home/apache/miniconda/lib/python2.7/site-packages/paramiko-1.15.2-py2.7.egg/paramiko/client.py", line 519, in _auth
paramiko.ssh_exception.AuthenticationException: Authentication failed.
>>> DEBUG:paramiko.transport:EOF in transport thread

更新:我接到了从VM到托管机器的机器的连接呼叫。不知道在哪里认为这会缩小问题的范围:/

2 个答案:

答案 0 :(得分:0)

它从您提供的输出中看起来好像paramiko正在尝试位于同一个密钥文件中的多个不同的密钥(在获取授权登录的正确密钥之前)。通常会有最大的尝试次数,这就是身份验证可能失败的原因。如果您在.ssh/文件夹中有很多密钥,并且使用ssh在没有-o IdentitiesOnly=yes选项的情况下登录,则会发生这种情况(即使您使用-i path/to/key指定也会发生这种情况特定的密钥文件)。不幸的是,我不知道如何转换为paramiko,但我认为库允许您更精确地指定密钥。但是,我认为您要为look_for_keysFalse设置为ssh.connect,并将不同的密钥分隔为不同的文件(每个服务器一个文件?)。

答案 1 :(得分:0)

.ssh / authorized_keys上的TIL权限位...只能由所有者写入。