【问题标题】:Paramiko script with error有错误的 Paramiko 脚本
【发布时间】:2016-07-08 20:41:14
【问题描述】:

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

    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

有人可以帮助我吗?因为我不明白错误在哪里。

提前致谢

【问题讨论】:

    标签: python ssh raspberry-pi paramiko


    【解决方案1】:

    原来你需要升级 setuptools 然后重新安装包。似乎旧版本在安装 python 加密包时会破坏一些东西。

    试试下面...

    pip install -U setuptools
    pin install -U paramiko
    

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

    【讨论】:

    • 感谢您的帮助,我已经重新安装了模块,现在没有错误,但程序仍然无法运行。当我使用命令“rebo​​ot”运行程序时没有任何反应,我认为 sendCommand() 中存在一些问题
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 2020-08-01
      • 1970-01-01
      相关资源
      最近更新 更多