python - 如何使用 Paramiko 获取 SSH 返回码?

标签 python ssh paramiko

client = paramiko.SSHClient()
stdin, stdout, stderr = client.exec_command(command)

有没有办法获取命令返回码?

很难解析所有的 stdout/stderr 并知道命令是否成功完成。

最佳答案

一个更简单的示例,不涉及直接调用“较低级别” channel 类(即 - NOT 使用 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

关于python - 如何使用 Paramiko 获取 SSH 返回码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3562403/

相关文章:

python - Paramiko 会被移植到 Python 3.x 吗?

python - 使用 Paramiko 递归目录下载?

python - 分配给python中的不连续切片

bash 脚本通过 ssh 进入一个盒子并让我进入 python shell

python - 矢量的二维正交投影到带有 numpy 的线上会产生错误的结果

git - 使用 Git 的最佳方式是什么,是通过 HTTPS 还是通过 SSH?

mysql - 从远程服务器到远程服务器的 SSH 隧道(将 RStudio 连接到 MySQL)

ssh - asyncio 和 paramiko 用于并发 ssh 连接

python - CrawlSpider 仅获取第一页中匹配链接的子集,然后移动到抓取第二页中的链接

python - 是否可以用python解析python并提取可执行表达式?