首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何接收大输出的paramiko .recv函数?

如何接收大输出的paramiko .recv函数?
EN

Stack Overflow用户
提问于 2018-03-28 08:09:29
回答 1查看 5.8K关注 0票数 2

我需要从paramiko的.recv函数中得到全部输出。我试图增加字节数,但在我个人看来,这不是正确的方法。脚本的主要目的是在同一会话中自动化命令列表。这里我的主要问题是无法从shell接收全部输出。

环境:

  • python v2.7.14
  • paramiko v2.4.1

我的脚本代码:()

代码语言:javascript
复制
#!/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")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-28 12:23:25

最后,我找到了解决方案,我在代码的末尾添加了一个While循环,这样它就会继续接收输出,直到发生中断语句。

代码语言:javascript
复制
While True:
    output = connection.recv(1024)
    print output
    if "condition" in output:
        break
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49529247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档