Python Can';t将SSH与paramiko连接到Google计算引擎实例
我在SO中发现了很多类似的问题,但它们中的任何一个都帮助了我,即使这个问题看起来很简单 我正在尝试使用paramiko通过SSH(我想使用SFTP)连接到远程googlecomputeengine实例。下面是我的代码:Python Can';t将SSH与paramiko连接到Google计算引擎实例,python,ssh,google-compute-engine,sftp,paramiko,Python,Ssh,Google Compute Engine,Sftp,Paramiko,我在SO中发现了很多类似的问题,但它们中的任何一个都帮助了我,即使这个问题看起来很简单 我正在尝试使用paramiko通过SSH(我想使用SFTP)连接到远程googlecomputeengine实例。下面是我的代码: client = paramiko.SSHClient() client.load_system_host_keys() client.connect('External_IP_Get_In_GCE_panel', username
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect('External_IP_Get_In_GCE_panel', username='myuser')
stdin, stdout, stderr = client.exec_command('ls')
for line in stdout:
print('... ' + line.strip('\n'))
client.close()
有了这个代码,我就有了错误
PasswordRequiredException:私钥文件已加密
当我尝试client.connect('External\u IP\u Get\u In\u GCE\u panel',username='myuser',password=''')时,
错误是:
BadAuthenticationType:('Bad authentication type',[u'publickey'])(允许的类型=[u'publickey']))
我访问谷歌计算引擎的SSH密钥没有密码。我可以使用gcloud compute ssh实例名
,也可以通过Filezilla访问SFTP而不会出现问题
正如我所说的,我尝试了许多在SO中找到的替代方案,但其中任何一个都对我有所帮助。以下是代码的3个其他版本:
使用键
使用另一个库
使用ssh密钥文件
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(hostname="External_IP_Get_In_GCE_panel", username="myuser", key_filename=os.path.expanduser('~/.ssh/ssh_key_file'), password='') # also tried ssh_key_file.pub
在所有这些版本(以及其他一些版本)中,我尝试使用password=''
,password=None
,并且不发送密码参数。结果总是与上面的错误相同
有关于我做错了什么的提示吗?密钥是加密的,您需要一个密码(可能是非空密码),即
那里的服务器只允许公钥身份验证,因此将
密码提供给客户机.connect
没有任何意义。正如我所说,SSH密钥是在没有密码的情况下创建的,因此,我没有任何东西可以填充此参数,而带有空字符串的password='
会引发加密私钥的相同错误。@James它似乎有密码-可能您将“无密码”与“sshagent记住密码”混淆了@James或在调用RSAKey
时尝试使用空密码。我创建了密钥,但没有为密码短语键入任何内容,因此我非常确定此密钥没有密码。无论如何,我将尝试您的解决方案,并可能创建一个带有密码的新密钥,以避免此类问题。谢谢你的帮助。
key = paramiko.RSAKey(data=decodebytes(keydata))
cnopts = pysftp.CnOpts()
cnopts.hostkeys.add('External_IP_Get_In_GCE_panel', 'ssh-rsa', key)
with pysftp.Connection('External_IP_Get_In_GCE_panel', username='myuser', cnopts=cnopts) as sftp:
with sftp.cd('../directory'):
sftp.get('remote_file')'''
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(hostname="External_IP_Get_In_GCE_panel", username="myuser", key_filename=os.path.expanduser('~/.ssh/ssh_key_file'), password='') # also tried ssh_key_file.pub
key = paramiko.RSAKey(data=base64.b64decode(b"""AAAAB3Nza..."""), password='my key password')