在Windows上安装Python的Cryptography

时间:2017-11-21 13:00:01

标签: python windows ssh

我在Windows上创建了一个脚本来连接远程SSH服务器。我已经成功安装了cryptographypynacl,最后成功安装了paramiko(花了我一整天的时间来弄清楚如何在Windows上成功安装它们。)

现在我运行脚本,它弹出一个错误,说DLL加载失败了。该错误似乎与libsodium有关但我无法弄清楚究竟要尝试加载哪个DLL以及从何处加载。为了更安全,我还安装了pysodium

这是脚本:

  

automate.py

import SSH

connection = ssh("10.10.65.100", "gerrit2", "gerrit@123")
print("Calling OpenShell")
connection.openShell()
print("Calling sendShell")
connection.sendShell("ls -l")
print("Calling process")
connection.process()
print("Calling closeConnection")
connection.closeConnection()
  

SSH.py

import threading, paramiko

class ssh:
    shell = None
    client = None
    transport = None

    def __init__(self, address, username, password):
        print("Connecting to server on ip", str(address) + ".")
        self.client = paramiko.client.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
        self.client.connect(address, username=username, password=password, look_for_keys=False)
        self.transport = paramiko.Transport((address, 22))
        self.transport.connect(username=username, password=password)

        thread = threading.Thread(target=self.process)
        thread.daemon = True
        thread.start()

    def closeConnection(self):
        if(self.client != None):
            self.client.close()
            self.transport.close()

    def openShell(self):
        self.shell = self.client.invoke_shell()

    def sendShell(self, command):
        if(self.shell):
            self.shell.send(command + "\n")
        else:
            print("Shell not opened.")

    def process(self):
        global connection
        while True:
            # Print data when available
            if self.shell != None and self.shell.recv_ready():
                alldata = self.shell.recv(1024)
                while self.shell.recv_ready():
                    alldata += self.shell.recv(1024)
                strdata = str(alldata, "utf8")
                strdata.replace('\r', '')
                print(strdata, end = "")
                if(strdata.endswith("$ ")):
                    print("\n$ ", end = "")

这是错误:

> python automate.py

Traceback (most recent call last):
  File "automate.py", line 1, in <module>
    import SSH
  File "D:\Automate\SSH_Paramiko\SSH.py", line 1, in <module>
    import threading, paramiko
  File "D:\Users\prashant-gu\AppData\Local\Programs\Python\Python37\lib\site-packages\paramiko-2.4.0-py3.7.egg\paramiko\__init__.py", line 22, in <module>
  File "D:\Users\prashant-gu\AppData\Local\Programs\Python\Python37\lib\site-packages\paramiko-2.4.0-py3.7.egg\paramiko\transport.py", line 57, in <module>
  File "D:\Users\prashant-gu\AppData\Local\Programs\Python\Python37\lib\site-packages\paramiko-2.4.0-py3.7.egg\paramiko\ed25519key.py", line 22, in <module>
  File "D:\Users\prashant-gu\AppData\Local\Programs\Python\Python37\lib\site-packages\nacl\signing.py", line 19, in <module>
    import nacl.bindings
  File "D:\Users\prashant-gu\AppData\Local\Programs\Python\Python37\lib\site-packages\nacl\bindings\__init__.py", line 17, in <module>
    from nacl.bindings.crypto_box import (
  File "D:\Users\prashant-gu\AppData\Local\Programs\Python\Python37\lib\site-packages\nacl\bindings\crypto_box.py", line 18, in <module>
    from nacl._sodium import ffi, lib
ImportError: DLL load failed: The specified module could not be found.

1 个答案:

答案 0 :(得分:0)

经过大量的谷歌搜索后,我终于偶然发现了this。正如在对话中所提到的,我卸载了之前的pynacl安装,从https://github.com/lmctv/pynacl/archive/v1.2.a0.reorder.zip下载了压缩源,从https://github.com/jedisct1/libsodium/releases/download/1.0.15/libsodium-1.0.15.tar.gz下载了libsodium,将LIB环境变量设置为D:\Users\prashant-gu\Downloads\libsodium-1.0.15\bin\x64\Release\v140\dynamic,最后安装pynacl使用

形成这个下载的源代码

pip install .

现在工作正常。

在安装paramiko期间,我也碰巧从https://ci.cryptography.io/job/cryptography-support-jobs/job/openssl-release-1.1/下载了OpenSSL,并将INCLUDE环境变量设置为D:\Users\prashant-gu\Downloads\openssl-1.1.0g-2015-x86_64\openssl-win64-2015\include,以便成功安装cryptography包。碰巧是paramiko的依赖。