【问题标题】:paramiko in router huawei [duplicate]华为路由器中的paramiko [重复]
【发布时间】:2020-08-20 23:21:13
【问题描述】:

朋友们, 有谁知道为什么这段代码在 cisco 的路由器上运行完美,但在华为路由器上,它保持不变并且没有完成?

import paramiko

ssh_RT = paramiko.SSHClient()
ssh_RT.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh_RT.connect(
    sIP,
    port=22,
    username=uuu,
    password=sss,
    timeout=5,
    look_for_keys=False,
    allow_agent=False
)

sComando = 'dis ip int br'
# sComando = 'sh ip int br'

stdin, stdout, stderr = ssh_RT.exec_command(sComando)
sOut = stdout.read().decode()
print (sOut)
ssh_RT.close()

【问题讨论】:

  • 有可能在第二个路由器上,连接被防火墙断开了 - “连接”可能超时,但很难确定。
  • 我不这么认为,因为路由器上的直接命令是可能的。通过netmiko(另一个库)可以咨询......但netmiko非常慢......
  • 标准输入、标准输出、标准错误 = ssh_RT.exec_command(sComando)
  • 我怀疑可能是字符编码。

标签: python paramiko huawei-mobile-services


【解决方案1】:

成功了。像这样:

def receive_data(pLenght, pTimeout):
    for x in range(pTimeout):    
        time.sleep(1)
        r = channel.recv(2024)
        z = r.decode('utf-8','ignore')
        if len(z) > pLenght:
            break
    return z


channel = ssh_RT.invoke_shell()
channel.send('dis ip int br\n')
out = receive_data(50,5)

【讨论】:

  • 由于某些原因,exec_command 不适用于华为路由器。
  • 我怀疑。这就是为什么我问你是否可以在命令行上执行ssh uuu@sss dis ip int br。这相当于exec_command
  • 真的……你是对的。您所代表的 ssh 命令(在命令行上)正在崩溃...
  • @MartinPrikryl 关于使用invoke_shell 而不是exec_command 的最后一个问题:我在另一篇文章中看到您不推荐使用invoke_shell。警报是因为invoke_shell 需要在代码中进行更多控制才能关闭通道吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-22
  • 2023-02-16
  • 1970-01-01
相关资源
最近更新 更多