在 Paramiko 中执行 curl 时,其输出在 stderr 中,而不是 stdout
When executing curl in Paramiko, its output is in stderr, not stdout
我有一个手动任务,我 ssh
到服务器,cd
到文件夹,然后 运行 curl
命令将文件下载到该文件夹.我现在正尝试在 Python 程序中自动执行此操作。我可以使用 paramiko
ssh 到服务器和 运行 任何命令:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(credentials)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('cd /path/to/folder/ && sudo curl -H \'X-JFrog-Art-Api:api_key\' -O "https://path/to/download/file.tar.xz"', timeout=600)
print(f'curl ssh_stdout: {ssh_stdout.readlines()}')
print(f'curl ssh_stderr: {ssh_stderr.readlines()}')
文件下载正确,但我在 stdout
中预期的输出出现在 stderr
中。这是 stderr
:
curl ssh_stderr: [' % Total % Received % Xferd Average Speed Time Time Time Current\n', ' Dload Upload Total Spent Left Speed\n', '\r 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\r 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0\r 0 0 0 0 0 0 0 0 --:--:-- 0:00:02 --:--:-- 0
...
0 0:04:36 0:04:35 0:00:01 57.6M\r 99 14.9G 99 14.9G 0 0 55.5M 0 0:04:36 0:04:36 --:--:-- 57.6M\r100 14.9G 100 14.9G 0 0 55.5M 0 0:04:36 0:04:36 --:--:-- 57.8M\n']
这似乎不是使我的流程自动化的好方法。有没有办法与其他一些可能更适合此类操作的库一起执行此操作?我在想 requests
.
curl
将其进度输出到标准错误。
您的代码和 Paramiko 都没有问题。
在shell中尝试:
curl https://www.example.com/ > stdout 2> stderr
您会看到 stdout
将包含 example.com HTML 内容,而 stderr
将包含 curl
进度输出。
我有一个手动任务,我 ssh
到服务器,cd
到文件夹,然后 运行 curl
命令将文件下载到该文件夹.我现在正尝试在 Python 程序中自动执行此操作。我可以使用 paramiko
ssh 到服务器和 运行 任何命令:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(credentials)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('cd /path/to/folder/ && sudo curl -H \'X-JFrog-Art-Api:api_key\' -O "https://path/to/download/file.tar.xz"', timeout=600)
print(f'curl ssh_stdout: {ssh_stdout.readlines()}')
print(f'curl ssh_stderr: {ssh_stderr.readlines()}')
文件下载正确,但我在 stdout
中预期的输出出现在 stderr
中。这是 stderr
:
curl ssh_stderr: [' % Total % Received % Xferd Average Speed Time Time Time Current\n', ' Dload Upload Total Spent Left Speed\n', '\r 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\r 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0\r 0 0 0 0 0 0 0 0 --:--:-- 0:00:02 --:--:-- 0
...
0 0:04:36 0:04:35 0:00:01 57.6M\r 99 14.9G 99 14.9G 0 0 55.5M 0 0:04:36 0:04:36 --:--:-- 57.6M\r100 14.9G 100 14.9G 0 0 55.5M 0 0:04:36 0:04:36 --:--:-- 57.8M\n']
这似乎不是使我的流程自动化的好方法。有没有办法与其他一些可能更适合此类操作的库一起执行此操作?我在想 requests
.
curl
将其进度输出到标准错误。
您的代码和 Paramiko 都没有问题。
在shell中尝试:
curl https://www.example.com/ > stdout 2> stderr
您会看到 stdout
将包含 example.com HTML 内容,而 stderr
将包含 curl
进度输出。