Paramiko:从远程执行命令的标准输出读取

2024-04-26 13:53:42 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我和paramiko一起进行了一些基本的SSH测试,但没有任何输出到stdout。这是我的密码。

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"
client.close()
print "Connection closed"
stdout=stdout.readlines()
print stdout
print com
for line in stdout:
    output=output+line
if output!="":
    print output
else:
    print "There was no output for this command"

所以每当我运行这个命令时,就会执行这个命令(如我执行cp之类的操作时所看到的,文件会被复制),但我总是得到“这个命令没有输出”。当打印stdout=stdout.readlines()时,[]是输出。此外,如果我在for循环中添加print语句,它将永远不会运行。有人能帮我吗?谢谢!


Tags: 命令comclientparamiko密码foroutputstdout
3条回答
# 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)

您已在读取行之前关闭连接:

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"

*交互式示例: ===第1部分,这显示服务器中的sh输出,在结尾处是“>;” 需要一些输入才能继续或退出

selilsosx045:uecontrol-CXC_173_6456-R32A01 lteue$/uecontrol.sh-主机本地主机 UE控件:使用以下命令启动UE控件: UE控件:java-Dlogdir=-Duecontrol.configdir=./etc-jar./server/server-R32A01.jar-host本地主机 从文件/Users/lteue/Downloads/uecontrol-CXC_173_6456-R32A01/etc/uecontrol.properties加载属性 向主机localhost启动远程CLI 输入命令Q退出CLI或命令帮助 获取有关可用命令的信息。 CLI已准备好输入。 uec>;

===带peramiko的Pyhton代码===*

尝试以下方法:而不是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 

相关问题 更多 >