代码之家  ›  专栏  ›  技术社区  ›  7_R3X

在Windows上安装Python加密

  •  1
  • 7_R3X  · 技术社区  · 7 年前

    我在windows上创建了一个脚本,用于连接到远程SSH服务器。我已成功安装 cryptography , pynacl 最后 paramiko (我花了一整天的时间才想出如何在windows上成功安装它们)。

    现在我运行脚本,它弹出一个错误,说DLL加载失败。错误似乎与 libsodium 但我无法准确地确定要加载哪个DLL以及从哪里加载。为了安全起见,我还安装了 pysodium .

    以下是脚本:

    自动化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 回复  |  直到 7 年前
        1
  •  0
  •   7_R3X    7 年前

    在谷歌上搜索了很多之后,我终于发现了 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 设置 LIB 环境变量至 D:\Users\prashant-gu\Downloads\libsodium-1.0.15\bin\x64\Release\v140\dynamic ,并最终使用

    pip install .

    现在它工作得很好。

    在安装 paramiko ,我也碰巧从 https://ci.cryptography.io/job/cryptography-support-jobs/job/openssl-release-1.1/ ,并将INCLUDE环境变量设置为 D:\Users\prashant-gu\Downloads\openssl-1.1.0g-2015-x86_64\openssl-win64-2015\include 为了成功安装 cryptography 恰好是的依赖项的包 帕拉米科 .