如何在paramiko .recv函数中接收大输出?

时间:2018-03-28 08:09:29

标签: python automation paramiko

我需要从paramiko的.recv函数接收完整输出。我试图增加nbyte数量,但在我看来,我认为这不是正确的方法。脚本的主要目的是在同一会话中自动执行命令列表。 我的主要问题是我无法从shell获得完整的输出。

环境:

  • python v2.7.14
  • paramiko v2.4.1

我的脚本代码:()

#!/usr/bin/env python
import sys,os,time
import paramiko

user = raw_input("Enter your username please:  ")
upassword = getpass.getpass()
ip = "172.x.x.x"
username = "ikxxxx"
password = "xxxgdxx"
ofile = open("ipsadd.csv", "r")


def sshConnection(ip):
   ssh = paramiko.SSHClient()
   ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
   ssh.connect(ip, username=user, password=upassword)
   connection = ssh.invoke_shell()


for line in ofile:
    hostname = line.split(",")[0].strip()
    print "\n\n" 
    print "Configuration running on: " + hostname
    host = line.split(",")[1].strip()
    print "IP: " + host
    print "*" * 40

    connection.send("ssh " + username + "@" + host + "\n")
    time.sleep(1)
    connection.send("\n")
    time.sleep(2)
    connection.send("yes" + "\n")
    time.sleep(2)
    connection.send(password + "\n")
    time.sleep(1)
    connection.send("screen-length 0 temporary" + "\n")
    time.sleep(2)
    connection.send("clear alarm  al no-trap" + "\n")
    time.sleep(2)
    connection.send("Y\n")
    connection.send("display device\n")
    connection.send("display temperature\n")
    connection.send("display device pic-status\n")
    connection.send("display power\n")
    connection.send("display health\n")
    connection.send("display cpu-usage\n")
    connection.send("display memory-usage\n")
    connection.send("display alarm all\n")
    connection.send("display switchover state\n")
    connection.send("display startup\n")
    connection.send("display version\n")
    connection.send("display interface brief\n")
    connection.send("display isis peer\n")
    connection.send("display isis peer verbose\n")
    connection.send("display bgp vpnv4 all peer\n")
    connection.send("display bgp vpn4 all peer verbose\n")
    connection.send("display bgp peer\n")
    connection.send("display current-configuration\n")
    time.sleep(1)
    connection.send("q\n")
    output = connection.recv(999999)
    print output
    break


  sshConnection("179.x.x.x")

1 个答案:

答案 0 :(得分:-1)

最后我找到了解决方案。我在代码末尾添加了一个While循环,因此它将继续接收输出,直到break语句。

easy_install my.egg