2

I need to receive full output from .recv function in paramiko. I tried to increase the nbyte number, but in my own opinion, I think it's not the proper way to do that. the main purpose of the script to automate list of commands at the same session. my main problem here is that I cant receive the full output from the shell.

Environment:

  • python v2.7.14
  • paramiko v2.4.1

My Script code: ()

#!/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")
ME_sociaty
  • 97
  • 1
  • 10
  • You can call `recv` multiple times until you get an empty output which means "the channel stream has closed". ref: http://docs.paramiko.org/en/2.4/api/channel.html#paramiko.channel.Channel.recv – Sraw Mar 28 '18 at 08:15
  • And as a side note, what you are doing (automating `ssh`, and on top of that automating a host key acceptance) is a terrible hack and a security flaw! – Martin Prikryl Mar 28 '18 at 08:23
  • @Sraw thank you for your reply. i will follow your suggestion "calling the recv many time" and i will let you know. – ME_sociaty Mar 28 '18 at 08:27
  • @MartinPrikryl Thank you for your help here. I have a middle server in my environment that's why I'm opening two sessions here. the first session will log in to the middle server the second one will be accessing the switches behind the controller or the middle server. – ME_sociaty Mar 28 '18 at 08:31
  • I know what you are doing. But your implementation is insecure and unreliable. – Martin Prikryl Mar 28 '18 at 08:33
  • so how to increase the reliability and security in my above code, please @MartinPrikryl – ME_sociaty Mar 28 '18 at 08:36
  • @Sraw I tried to use "recv" many times but still, I'm receiving incomplete output. – ME_sociaty Mar 28 '18 at 08:39
  • That's way beyond scope of this question. And a question about your real task, rather than about solving problems with your hack. – Martin Prikryl Mar 28 '18 at 08:41
  • Indeed you're right. I will try to solve my problem first and then I will make sure to improve my code reliability. @MartinPrikryl – ME_sociaty Mar 28 '18 at 08:45

1 Answers1

-1

Finally i found the solution.I added a While loop to the end of my code so it will continue receiving the output until the break statement.

While True:
    output = connection.recv(1024)
    print output
    if "condition" in output:
        break
ME_sociaty
  • 97
  • 1
  • 10
  • That's a code from the question I've linked in my comment to your question. What only proves that your question **is duplicate**. So please, **accept that your question is duplicate**, instead of duplicating an answer. – Martin Prikryl Mar 28 '18 at 12:28
  • @MartinPrikryl don't take it personally. i admit that my question is duplicate. hence, Thank you again and again martin for your assistance. – ME_sociaty Apr 12 '18 at 10:37
  • I do not take it personally. But we can to keep Stack Overflow tidy, without duplicate contents. That's why I ask you to accept that your question is duplicate. By *accept* I mean a *real action*. You should see a corresponding button somewhere around your question. – Martin Prikryl Apr 12 '18 at 12:06