python 2.7 - How to store stdout.channel.recv(1024).decode("utf-8") value in variable -


i using paramiko in python , when running ssh.exec command, have use stdout.channel.recv(1024).decode("utf-8") formatted output. here sample code.

def runcmd(cmd):     stdin, stdout, stderr = ssh.exec_command (cmd)     #stdin, stdout, stderr = ssh.exec_command ('ls')     # wait command terminate     while not stdout.channel.exit_status_ready():         # print data if there data read in channel         if stdout.channel.recv_ready():             rl, wl, xl = select.select([stdout.channel], [], [], 0.0)             if len(rl) > 0:                 # print data stdout                 print (stdout.channel.recv(1024).decode("utf-8")) 

now whatever o/p print above command have store in variable have use output action. when using output = runcmd(cmd) in main class. getting o/p none. clue fix this.

your problem runcmd function printing , not returning anything. need end function return.

concretely:

def runcmd(cmd):     out = ""     stdin, stdout, stderr = ssh.exec_command (cmd)     #stdin, stdout, stderr = ssh.exec_command ('ls')     # wait command terminate     while not stdout.channel.exit_status_ready():         # print data if there data read in channel         if stdout.channel.recv_ready():             rl, wl, xl = select.select([stdout.channel], [], [], 0.0)             if len(rl) > 0:             # print data stdout                 out = stdout.channel.recv(1024).decode("utf-8")                 print out                 return out 

now function returns string can do:

output = runcmd(cmd) 

Comments