运行 使用 Paramiko 的数据库脚本失败并显示退出代码

Running database script with Paramiko fails with exit code

输出文件是在数据库结果可用之前创建的。

传递一个简单的 os 命令工作正常:

# command = "whoami > result.txt"

工作正常。当我打开 result.txt 文件时,我会得到我的用户名。问题是等待数据库 return 结果。即使有数据 return 来自实际查询

,它还是空的
import paramiko


def get_report(command):
    # reference: 
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('server123', username='username', password='password')

    stdin, stdout, stderr = client.exec_command(command)
    exit_status = stdout.channel.recv_exit_status()

    if exit_status == 0:
        print("File processed")
    else:
        print("Error", exit_status)
    client.close()

command = "sql_query.script > result.txt"
get_report(command=command)

我希望收到 first_name、last_name 和位置的数据集,但我却收到错误 108。

如果命令在使用 Paramiko 执行时不起作用,请通过读取其错误输出来调试它。

为此使用 stderr.readlines()


如果相同的命令在常规 shell 中有效,但在 Paramiko 中无效,问题通常与 SSH "exec" 通道使用的不同环境有关,[=12] =].参见: