Python 如何使用 Paramiko 获取 SSH 返回码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3562403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you get the SSH return code using Paramiko?
提问by Beyonder
client = paramiko.SSHClient()
stdin, stdout, stderr = client.exec_command(command)
Is there any way to get the command return code?
有什么办法可以得到命令返回码吗?
It's hard to parse all stdout/stderr and know whether the command finished successfully or not.
很难解析所有 stdout/stderr 并知道命令是否成功完成。
采纳答案by JanC
SSHClient is a simple wrapper class around the more lower-level functionality in Paramiko. The API documentationlists a recv_exit_status()method on the Channel class.
SSHClient 是一个简单的包装类,它围绕着 Paramiko 中更底层的功能。的API文档列出了recv_exit_status()上的频道类方法。
A very simple demonstration script:
一个非常简单的演示脚本:
$ cat sshtest.py
import paramiko
import getpass
pw = getpass.getpass()
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
client.connect('127.0.0.1', password=pw)
while True:
cmd = raw_input("Command to run: ")
if cmd == "":
break
chan = client.get_transport().open_session()
print "running '%s'" % cmd
chan.exec_command(cmd)
print "exit status: %s" % chan.recv_exit_status()
client.close()
$ python sshtest.py
Password:
Command to run: true
running 'true'
exit status: 0
Command to run: false
running 'false'
exit status: 1
Command to run:
$
回答by apdastous
A much easier example that doesn't involve invoking the "lower level" channel class directly (i.e. - NOTusing the client.get_transport().open_session()command):
一个更简单的例子,不涉及直接调用“低级”通道类(即 -不使用client.get_transport().open_session()命令):
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('blahblah.com')
stdin, stdout, stderr = client.exec_command("uptime")
print stdout.channel.recv_exit_status() # status is 0
stdin, stdout, stderr = client.exec_command("oauwhduawhd")
print stdout.channel.recv_exit_status() # status is 127
回答by perillaseed
Thanks for JanC, I added some modification for the example and tested in Python3, it really useful for me.
感谢 JanC,我为示例添加了一些修改并在 Python3 中进行了测试,它对我来说非常有用。
import paramiko
import getpass
pw = getpass.getpass()
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
#client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def start():
try :
client.connect('127.0.0.1', port=22, username='ubuntu', password=pw)
return True
except Exception as e:
#client.close()
print(e)
return False
while start():
key = True
cmd = input("Command to run: ")
if cmd == "":
break
chan = client.get_transport().open_session()
print("running '%s'" % cmd)
chan.exec_command(cmd)
while key:
if chan.recv_ready():
print("recv:\n%s" % chan.recv(4096).decode('ascii'))
if chan.recv_stderr_ready():
print("error:\n%s" % chan.recv_stderr(4096).decode('ascii'))
if chan.exit_status_ready():
print("exit status: %s" % chan.recv_exit_status())
key = False
client.close()
client.close()
回答by Youngmin Kim
In my case, output buffering was the problem. Because of buffering, the outputs from the application doesn't come out non-blocking way. You can find the answer about how to print output without buffering in here: Disable output buffering. For short, just run python with -u option like this:
就我而言,输出缓冲是问题所在。由于缓冲,应用程序的输出不会以非阻塞方式出现。您可以在此处找到有关如何在没有缓冲的情况下打印输出的答案:禁用输出缓冲。简而言之,只需像这样使用 -u 选项运行 python:
> python -u script.py
> python -u script.py

