簡體   English   中英

執行交互式工具的命令並使用Paramiko讀取其輸出

[英]Executing commands of interactive tool and reading their output with Paramiko

我正在准備一個代碼,以使用帶有Paramiko的ssh訪問遠程服務器,並運行一些命令並查看stdout的輸出。

我已經在Ubuntu服務器上測試了該代碼,並且可以正常工作,但是當我在另一台服務器(這是Windows服務器和電信機的接口)上測試該代碼時,不會讀取stdout,(( “成功執行的命令在遠程服務器上” )被打印,但未打印以下內容( “讀取行” ),因此我得出結論,該代碼掛在stdout=stdout.readlines()該代碼被復制到下面,您能幫我弄清楚是什么可能是這次失敗的原因嗎?

我還要補充一點,如果我使用PuTTY在該服務器上執行命令,則會得到正確的輸出。

import paramiko
import os
user_name = "****"
passwd = "******"
ip = "*.*.*.*"

print ("Please wait creating ssh client ...")
ssh_client = paramiko.SSHClient()     #Create sshclient instance
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print ("Please wait, connecting to remote server")
ssh_client.connect(hostname=ip,username=user_name,password=passwd)
cmd="mml \n command2"
print ("Please wait, executing command on remote server")
stdin,stdout,stderr=ssh_client.exec_command(cmd)
print ("Successfully executed command on remote server")
stdout=stdout.readlines()
print ("lines are read")
stdout="".join(stdout)
ssh_client.close()
print ("Connection closed")
print (stdout)
os.system("pause")

您正在執行的命令是交互式的。 啟動后,它將等待子命令。 在執行它時,等待命令完成(通過調用readlines )。 永遠不會發生。

您必須將子命令提供給命令以使其執行某些操作。
請參閱使用Python Paramiko通過SSH將輸入/變量傳遞到命令/腳本

您還必須使命令退出(通過發送子命令,如exit / quit / bye )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM