Paramiko:如何执行命令并以字符串获取输出

在 Paramiko 中使用 exec_command() 返回元组 (stdin, stdout, stderr)。大多数时候,你只想读取 stdout 并忽略 stdinstderr。你可以使用 stdout.read()(返回字符串)或 stdout.readlines()(返回行列表)获取命令输出。

示例:

exec_command_example.py
stdin, stdout, stderr = ssh.exec_command("ls")
output = stdout.read()
print(output)

另请参阅我们的完整示例:Paramiko SSH 客户端最小示例:如何连接到 SSH 服务器并执行命令


Check out similar posts by category: Paramiko, Python