I’m trying to connect to a remote host via pysftp
:
try: with pysftp.Connection(inventory[0], username='transit', private_key='~/.ssh/id_rsa.sftp', port=8055) as sftp: sftp.put('/home/me/test.file') except Exception, err: print sys.exc_info() print err
However, I get a weird exception raised that I can’t find much detail on.
(<class 'paramiko.ssh_exception.SSHException'>, SSHException('Bad host key from server',), <traceback object at 0x7fa76269c5a8>) Bad host key from server
I found a related question that suggested I run ssh-keygen -R [host]
to replace the key in my known_hosts
file — once I did that, I got a new error:
(<class 'paramiko.ssh_exception.SSHException'>, SSHException('No hostkey for host abc.com found.',), <traceback object at 0x7f6b8520cb48>) No hostkey for host abc.com found.
Now, if I try to ssh to this host, it prompts me again to trust the key to be put back into my known_hosts
file, which I accept and can happily ssh onto the box. After this step, if I attempt to run my script again, I get the original error Bad host key from server
.
Am I doing something wrong here?
ninja edit: I should mention that the following works fine from the terminal:
sftp -i .ssh/id_rsa.sftp -oPort=8055 user@host.com sftp> put /home/me/test.file
Paramiko Debug Output:
[2016-07-14 10:32:23,809] | paramiko.transport - DEBUG - starting thread (client mode): 0x7f1dab10L [2016-07-14 10:32:23,810] | paramiko.transport - DEBUG - Local version/idstring: SSH-2.0-paramiko_2.0.1 [2016-07-14 10:32:23,836] | paramiko.transport - DEBUG - Remote version/idstring: SSH-2.0-mod_sftp/0.9.9 [2016-07-14 10:32:23,836] | paramiko.transport - INFO - Connected (version 2.0, client mod_sftp/0.9.9) [2016-07-14 10:32:23,837] | paramiko.transport - DEBUG - kex algos:[u'ecdh-sha2-nistp256', u'ecdh-sha2-nistp384', u'ecdh-sha2-nistp521', u'diffie-hellman-group-exchange-sha256', u'diffie-hellman-group-exchange-sha1', u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1', u'rsa1024-sha1'] server key:[u'ssh-rsa', u'ssh-dss'] client encrypt:[u'aes256-ctr', u'aes192-ctr', u'aes128-ctr', u'aes256-cbc', u'aes192-cbc', u'aes128-cbc', u'blowfish-ctr', u'blowfish-cbc', u'cast128-cbc', u'arcfour256', u'arcfour128', u'3des-ctr', u'3des-cbc'] server encrypt:[u'aes256-ctr', u'aes192-ctr', u'aes128-ctr', u'aes256-cbc', u'aes192-cbc', u'aes128-cbc', u'blowfish-ctr', u'blowfish-cbc', u'cast128-cbc', u'arcfour256', u'arcfour128', u'3des-ctr', u'3des-cbc'] client mac:[u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-sha1', u'hmac-sha1-96', u'hmac-md5', u'hmac-md5-96', u'hmac-ripemd160', u'umac-64@openssh.com'] server mac:[u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-sha1', u'hmac-sha1-96', u'hmac-md5', u'hmac-md5-96', u'hmac-ripemd160', u'umac-64@openssh.com'] client compress:[u'none'] server compress:[u'none'] client lang:[u''] server lang:[u''] kex follows?False [2016-07-14 10:32:23,837] | paramiko.transport - DEBUG - Kex agreed: diffie-hellman-group1-sha1 [2016-07-14 10:32:23,838] | paramiko.transport - DEBUG - Cipher agreed: aes128-ctr [2016-07-14 10:32:23,838] | paramiko.transport - DEBUG - MAC agreed: hmac-sha2-256 [2016-07-14 10:32:23,838] | paramiko.transport - DEBUG - Compression agreed: none [2016-07-14 10:32:23,935] | paramiko.transport - DEBUG - kex engine KexGroup1 specified hash_algo <built-in function openssl_sha1> [2016-07-14 10:32:23,936] | paramiko.transport - DEBUG - Switch to new keys ... [2016-07-14 10:32:23,952] | paramiko.transport - DEBUG - Bad host key from server [2016-07-14 10:32:23,952] | paramiko.transport - DEBUG - Expected: ssh-rsa: 'x00x00x00x07ssh-rsax00x00x00x01#x00x00x01x01x00xb9x05xddUF?oxeetxadx06=x83xd6xa8xb1x06x03xb7xb5xa5Cx80x0c~xa8x83[w!x16yxe6@xdajXx90+cxd4x18Xxd7xffx05x06xe2x19x96x8c&Jx0cx1fxd2xefCxa5xc6xbbx1fxc9Nxd7rx12xd29x1fxe1xb6Fxe8xdc(w~x1fxeaxeaxfdUT?^Vxe8='xd60xdcxc44gYnnxfexe8^?x8bxa8"xdaxc7(x18xc3<xa9x1cxa1)A3_x00<$yxbexd4$xc2Sxe6x93xe3B^xefxe6xbfxb5x88xd6x98EePx9aZmxa7xc5&xffnxc1lbx1e[x8d!*xfaxa3xcexb6xe7xd5Ur<Exd2xe1x89.xed%oxefjuxde>=*bxaeBOxbf>xe4/,xd5@Rxc5x9exacxf5x80x04xa9s)3xb5xbcxc2xccxe7xa7xb8x04>uAxb4Oxa6xf3xdbx9c2xb5"%nx89tx8192xe7#nx82pxeexdfx16]xb3Ax93xebm|xb9xd7nXsmwx81xa9' [2016-07-14 10:32:23,953] | paramiko.transport - DEBUG - Got : ssh-rsa: "x00x00x00x07ssh-rsax00x00x00x01#x00x00x01x01x00xc6_xeaxc1yFxd5xddx146x8cx0eUxeah[3x16VPxde2xbe~xedx92xefxb81x14xd8xb7xa7x08xa3x02xa8nx9fx7f{x896x1bx89/,x92x86xc7Mxa5x7f0Cjxe6x93xb0x0eup%xa2px9d3ixacoxf7x14xfbxfex8f}axcbJ :xdfxdd1x10xe8;xd3%x98k')xf8x1cxc1Dx97\xccxe6xf5*6xc2xf1xb8xcf!xeedx1coxc1x03xb4vxc0,?xdaxc7xa3xd9xe8xafyxf5kxf7xe8xa1x9crxfax81xcdxeexd3xeaoxa7x072xcex8bxf9x95x03xa1xe2xaftxa2xbaxa1Oxbcxbf}}x9exc0tx9bCx88xbdx18'x00?-xa0x05x83Ix1ah#Ixfcxebc$xfcxa0xadxeePx80x88Bh_xeb`xb6xabx8bCx83x987xbax05xb14xeax90xac>x99&x99xb3xf9nCxdfxfdxba<xf8xafxcaxafxfax15+x1e,Lxa1xf0x1exa5`xbd>xf4xa63)xae7Fxd2xc9xf2xf3Axa4x10uxa9" (<class 'paramiko.ssh_exception.SSHException'>, SSHException('Bad host key from server',), <traceback object at 0x7fb87f143ef0>) Bad host key from server [2016-07-14 10:32:24,037] | paramiko.transport - DEBUG - EOF in transport thread
Advertisement
Answer
Workaround
This seems to be some sort of bug with the pysftp
wrapper… I’m not sure. Reverting to native paramiko
got me connected just fine, so I’m going with that for now. Current working code:
rsa_key = paramiko.RSAKey.from_private_key_file('~/.ssh/the_key', password='myPassword') transport = paramiko.Transport((inventory[0],8055)) transport.connect(username='theUser', pkey=rsa_key) sftp = paramiko.SFTPClient.from_transport(transport) print sftp.listdir()