Paramiko: read from standard output of remotely executed command

后端 未结 3 1507
孤城傲影
孤城傲影 2020-12-03 18:28

so I was working with paramiko for some basic SSH testing and I\'m not getting any output into stdout. Heres my code.

import paramiko
client=paramiko.SSHCli         


        
相关标签:
3条回答
  • 2020-12-03 18:33
    # Program to print the output in console/interpreter/shell in runtime without using stdout.
    
     import paramiko
     import xlrd
     import time
    
     ssh = paramiko.SSHClient()
     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
     loc = ('/Users/harshgow/Documents/PYTHON_WORK/labcred.xlsx')
     wo = xlrd.open_workbook(loc)
     sheet = wo.sheet_by_index(0)
     Host = sheet.cell_value(0, 1)
     Port = int(sheet.cell_value(3, 1))
     User = sheet.cell_value(1, 1)
     Pass = sheet.cell_value(2, 1)
    
     def details(Host, Port, User, Pass):
           time.sleep(2)
    
           ssh.connect(Host, Port, User, Pass)
           print('connected to ip ', Host)
    
           stdin = ssh.exec_command("")
           remote_conn = ssh.invoke_shell()
           print("Interactive SSH session established")
    
           output = remote_conn.recv(1000)
           remote_conn.send("\n")
           remote_conn.send("xstatus Cameras\n")
    
           time.sleep(5)
           output = remote_conn.recv(10000)
           print(output)
    
     details(Host, Port, User, Pass)
    
    
    0 讨论(0)
  • 2020-12-03 18:38

    *Interactive example : ====Part 1, this show the sh output in server ,at the end of is ">" need some input to continual or exit ======

    selilsosx045:uecontrol-CXC_173_6456-R32A01 lteue$ ./uecontrol.sh -host localhost UE Control:Start UE control using: UE Control:java -Dlogdir= -Duecontrol.configdir=./etc -jar ./server/server-R32A01.jar -host localhost Loading properties from file /Users/lteue/Downloads/uecontrol-CXC_173_6456-R32A01/etc/uecontrol.properties Starting remote CLI towards host localhost Enter the command Q to exit the CLI, or the command HELP to get information on available commands. The CLI is ready for input. uec>

    ===========Pyhton code with peramiko ============*

    Try below method : while not stdout.channel.exit_status_ready():

    def shCommand(server_list):
    server_IP = server_list[0]
    username  = server_list[1]
    password  = server_list[2]
    
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(server_IP,22,username, password)strong text
    
    commandList = ['list \n']
    alldata = getUeInfo(ssh,commandList)
    ssh.close()
    
    def getUeInfo(ssh,commandList):
    data_buffer = ""
    num_of_input = 0
    stdin, stdout, stderr = ssh.exec_command('cmd')
    while not stdout.channel.exit_status_ready():
       solo_line = ""        
    
       if stdout.channel.recv_ready():
    
          solo_line = stdout.channel.recv(1024)  # Retrieve the first 1024 bytes
          data_buffer += solo_line               
    
    
       if(cmp(solo_line,'uec> ') ==0 ):    #len of solo should be 5 ,
         if num_of_input == 0 :
          data_buffer = ""    
          for cmd in commandList :
           #print cmd
           stdin.channel.send(cmd)
          num_of_input += 1
         if num_of_input == 1 :
          stdin.channel.send('q \n') 
          num_of_input += 1
    
    return data_buffer 
    
    0 讨论(0)
  • 2020-12-03 18:53

    You have closed the connection before reading lines:

    import paramiko
    client=paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    com="ls ~/desktop"
    client.connect('MyIPAddress',MyPortNumber, username='username', password='password')
    output=""
    stdin, stdout, stderr = client.exec_command(com)
    
    print "ssh succuessful. Closing connection"
    stdout=stdout.readlines()
    client.close()
    print "Connection closed"
    
    print stdout
    print com
    for line in stdout:
        output=output+line
    if output!="":
        print output
    else:
        print "There was no output for this command"
    
    0 讨论(0)
提交回复
热议问题