繁体   English   中英

Paramiko脚本有错误

[英]Paramiko script with error

我在互联网上找到了以下脚本:

    import paramiko
    from paramiko import client


class ssh:
client = None

def __init__(self, address, username, password):
    # Let the user know we're connecting to the server
    print("Connecting to server.")
    # Create a new SSH client
    self.client = client.SSHClient()
    # The following line is required if you want the script to be able to access a server that's not yet in the known_hosts file
    self.client.set_missing_host_key_policy(client.AutoAddPolicy())
    # Make the connection
    self.client.connect(address, username=username, password=password, look_for_keys=False)

def sendCommand(self, command):
    # Check if connection is made previously
    if (self.client):
        stdin, stdout, stderr = self.client.exec_command(command)
        while not stdout.channel.exit_status_ready():
            # Print stdout data when available
            if stdout.channel.recv_ready():
                # Retrieve the first 1024 bytes
                alldata = stdout.channel.recv(1024)
                while stdout.channel.recv_ready():
                    # Retrieve the next 1024 bytes
                    alldata += stdout.channel.recv(1024)

                # Print as string with utf8 encoding
                print(str(alldata, "utf8"))
    else:
        print("Connection not opened.")


 paramiko.util.log_to_file('paramiko.log') # <----- added line
 connessione = ssh("10.76.80.11","pi","raspberry")

 connessione.sendCommand("arp -a")

我想用这个脚本向我的树莓发送一个突击队,我试着在没有线路的情况下运行程序:

    paramiko.util.log_to_file('paramiko.log')

但是当我尝试运行代码时,我遇到了这个运行时错误:

     /usr/bin/python /Users/Marco/PycharmProjects/ssh_control/ssh.py
     Connecting to server.
     No handlers could be found for logger "paramiko.transport"
     Traceback (most recent call last):
     File "/Users/Marco/PycharmProjects/ssh_control/ssh.py", line 43, in <module>
     connessione = ssh("10.76.80.11","pi","raspberry")
     File "/Users/Marco/PycharmProjects/ssh_control/ssh.py", line 16, in __init__
     self.client.connect(address, username=username, password=password, look_for_keys=False)
     File "/Users/Marco/Library/Python/2.7/lib/python/site-packages/paramiko/client.py", line 338, in connect
      t.start_client()
     File "/Users/Marco/Library/Python/2.7/lib/python/site-    packages/paramiko/transport.py", line 493, in start_client
     raise e
     AttributeError: 'EntryPoint' object has no attribute 'resolve'

      Process finished with exit code 1

所以我在互联网上搜索,我发现问题可以通过paramiko.log行修复。

但现在我又出现了另一个错误:

    /usr/bin/python /Users/Marco/PycharmProjects/ssh_control/ssh.py
    Connecting to server.
    Traceback (most recent call last):
     File "/Users/Marco/PycharmProjects/ssh_control/ssh.py", line 38, in <module>
     connessione = ssh("10.76.80.11","pi","raspberry")
    File "/Users/Marco/PycharmProjects/ssh_control/ssh.py", line 16, in     __init__
    self.client.connect(address, username=username, password=password,   look_for_keys=False)
    File "/Users/Marco/Library/Python/2.7/lib/python/site- packages/paramiko/client.py", line 338, in connect
    t.start_client()
    File "/Users/Marco/Library/Python/2.7/lib/python/site-packages/paramiko/transport.py", line 493, in start_client
raise e
    AttributeError: 'EntryPoint' object has no attribute 'resolve'

    Process finished with exit code 1

有人可以帮我吗? 因为我无法理解错误在哪里。

提前致谢

事实证明,您需要升级setuptools然后重新安装该软件包。 似乎旧版本在安装python加密包时破坏了一些东西。

试试以下......

pip install -U setuptools
pin install -U paramiko

https://github.com/pyca/cryptography/issues/2853

我用这个解决了我的问题:

   # Make the connection
    self.client.connect(address, port = 22, username=username, password=password, look_for_keys=False)

因为之前的端口没有具体说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM