【问题标题】:Paramiko TypeError: from_buffer() cannot return the address of the raw string within a str or unicode or bytearray objectParamiko TypeError: from_buffer() 无法返回 str 或 unicode 或 bytearray 对象中的原始字符串的地址
【发布时间】:2020-11-13 14:19:01
【问题描述】:

我正在尝试使用 Python 和 Paramiko 连接到 SFTP 服务器,但出现此错误(使用 pysftp 时出现相同的错误):

starting thread (client mode): 0x17ccde50L
Local version/idstring: SSH-2.0-paramiko_2.7.2
Remote version/idstring: SSH-2.0-OpenSSH_7.2
Connected (version 2.0, client OpenSSH_7.2)
kex algos:[u'curve25519-sha256@libssh.org', u'ecdh-sha2-nistp256', u'ecdh-sha2-nistp384', u'ecdh-sha2-nistp521', u'diffie-hellman-group-exchange-sha256', u'diffie-hellman-group14-sha1'] server key:[u'ssh-rsa', u'rsa-sha2-512', u'rsa-sha2-256', u'ssh-dss', u'ecdsa-sha2-nistp256', u'ssh-ed25519'] client encrypt:[u'chacha20-poly1305@openssh.com', u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'aes128-gcm@openssh.com', u'aes256-gcm@openssh.com'] server encrypt:[u'chacha20-poly1305@openssh.com', u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'aes128-gcm@openssh.com', u'aes256-gcm@openssh.com'] client mac:[u'umac-64-etm@openssh.com', u'umac-128-etm@openssh.com', u'hmac-sha2-256-etm@openssh.com', u'hmac-sha2-512-etm@openssh.com', u'hmac-sha1-etm@openssh.com', u'umac-64@openssh.com', u'umac-128@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-sha1'] server mac:[u'umac-64-etm@openssh.com', u'umac-128-etm@openssh.com', u'hmac-sha2-256-etm@openssh.com', u'hmac-sha2-512-etm@openssh.com', u'hmac-sha1-etm@openssh.com', u'umac-64@openssh.com', u'umac-128@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-sha1'] 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
Kex agreed: curve25519-sha256@libssh.org
HostKey agreed: ssh-ed25519
Cipher agreed: aes128-ctr
MAC agreed: hmac-sha2-256
Compression agreed: none
kex engine KexCurve25519 specified hash_algo <built-in function openssl_sha256>
Unknown exception: from_buffer() cannot return the address of the raw string within a str or unicode or bytearray object
 Traceback (most recent call last):
   File "/usr/lib/python2.7/site-packages/paramiko/transport.py", line 2075, in run
     self.kex_engine.parse_next(ptype, m)
   File "/usr/lib/python2.7/site-packages/paramiko/kex_curve25519.py", line 64, in parse_next
     return self._parse_kexecdh_reply(m)
   File "/usr/lib/python2.7/site-packages/paramiko/kex_curve25519.py", line 129, in _parse_kexecdh_reply
     self.transport._activate_outbound()
   File "/usr/lib/python2.7/site-packages/paramiko/transport.py", line 2553, in _activate_outbound
     self.local_cipher, key_out, IV_out, self._ENCRYPT
   File "/usr/lib/python2.7/site-packages/paramiko/transport.py", line 1934, in _get_cipher
     return cipher.encryptor()
   File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/primitives/ciphers/base.py", line 126, in encryptor
     self.algorithm, self.mode
   File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 487, in create_symmetric_encryption_ctx
     return _CipherContext(self, cipher, mode, _CipherContext._ENCRYPT)
   File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/openssl/ciphers.py", line 69, in __init__
     iv_nonce = self._backend._ffi.from_buffer(mode.nonce)
 TypeError: from_buffer() cannot return the address of the raw string within a str or unicode or bytearray object

我能够使用以下方法成功连接到 SFTP 服务器:

sftp -oPort=22 xxxxx@10.132.x.x:/home

所以我知道服务器存在并且可以访问。

我的 Python 代码是这样的:

paramiko.util.log_to_file("filename.log")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
ssh.connect(ftp_host, username=ftp_username, password=ftp_password, timeout=None)

还有一些依赖..

asn1crypto @ file:///home/folder/app/utils/asn1crypto-1.2.0-py2.py3-none-any.whl
bcrypt @ file:///home/folder/app/utils/bcrypt-3.1.6-cp27-cp27mu-manylinux1_x86_64.whl
cffi==1.5.2 
cryptography @ file:///home/folder/app/utils/cryptography-3.1-cp27-cp27mu-manylinux2010_x86_64.whl
netmiko==2.3.2
paramiko @ file:///home/folder/app/utils/vendor/paramiko-2.7.2-py2.py3-none-any.whl
ply==3.4
pyasn1==0.1.9
pycparser==2.19 
PyNaCl @ file:///home/folder/app/utils/PyNaCl-1.3.0-cp27-cp27mu-manylinux1_x86_64.whl
pyOpenSSL==16.0.0
six==1.9.0

我的问题是,这个错误到底是什么意思,解决它的最佳方法是什么?我需要将图像复制到 SFTP,但无法完全连接。

顺便说一句,我正在运行 Python 的服务器卡在 2.7 上,并且不允许我升级它。此外,它无法访问互联网,因此我无法使用 apt-get 之类的东西。我通过拖放压缩文件夹或 .whl 文件来安装东西。我只是找到正确的依赖组合。

【问题讨论】:

    标签: python ssh sftp paramiko pysftp


    【解决方案1】:

    本主题表明您可能有过时的依赖项:
    https://github.com/paramiko/paramiko/issues/1027

    The solution by @bieli 似乎帮助了许多面临问题的人:

    sudo pip uninstall cryptography -y && sudo apt-get purge python3-cryptography && sudo apt-get autoremove && sudo pip3 install --upgrade cryptography
    

    如果您无法升级依赖项,您可以尝试使用不同的 KEX。但总的来说,这可能是死路一条。


    强制警告:不要使用AutoAddPolicy - 这样做会失去对MITM attacks 的保护。如需正确的解决方案,请参阅Paramiko "Unknown Server"

    【讨论】:

      【解决方案2】:

      在下面的示例中,您可能会看到一些依赖项的绝对路径,因为我在没有 Internet 的远程服务器上运行 Python 脚本。因此,必须将 .whl 文件从我的 PC 复制到远程服务器。在这些依赖项中,“cffi”已升级到版本 1.11.2 并最终解决了问题。

      如果您发现自己遇到类似问题,请尝试找到最佳的依赖组合,如下所示:

      asn1crypto @ file:///home/badge_bridge/utils/asn1crypto-1.2.0-py2.py3-none-any.whl
      bcrypt @ file:///home/badge_bridge/utils/bcrypt-3.1.6-cp27-cp27mu-manylinux1_x86_64.whl
      cffi @ file:///home/badge_bridge/utils/vendor/cffi-1.11.2-cp27-cp27mu-manylinux1_x86_64.whl
      chrome-gnome-shell==0.0.0
      cryptography @ file:///home/badge_bridge/utils/vendor/cryptography-2.1-cp27-cp27mu-manylinux1_x86_64.whl
      cupshelpers==1.0
      ecdsa==0.6
      enum34 @ file:///home/badge_bridge/utils/vendor/enum34-1.1.6-py2-none-any.whl
      idna==2.0
      ipaddress==1.0.14
      isc==2.0
      netmiko==2.3.2
      paramiko @ file:///home/badge_bridge/utils/vendor/paramiko-2.3.3-py2.py3-none-any.whl
      ply==3.4
      pyasn1==0.1.9
      pycparser==2.19
      pycryptodome @ file:///home/badge_bridge/utils/vendor/pycryptodome-3.6.5-cp27-cp27mu-manylinux1_x86_64.whl
      pycups==1.9.66
      pycurl==7.19.0
      pygobject==3.20.1
      PyNaCl @ file:///home/badge_bridge/utils/PyNaCl-1.3.0-cp27-cp27mu-manylinux1_x86_64.whl
      pyOpenSSL==16.0.0
      pysftp==0.2.9
      pysmbc==1.0.13
      requests==2.11.1
      six==1.13.0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-27
        • 1970-01-01
        • 2012-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多