paramiko ls 有几个文件输出被截断
paramiko ls with several files output truncated
我已经成功地将 paramiko 的 exec_command 与 ls 一起使用,直到我尝试使用文件列表作为参数。我的函数是:
def jz_orion_ssh_sout_list(cmd):
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(orion['host'], username=orion['username'], password=orion['password'])
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd)
sout = ssh_stdout.readlines()
serr = ssh_stderr.readlines()
return sout
在cmd='ls -l /my/path/file.txt'
时可以正常工作,但在cmd='ls -l /my/path/file1.txt file2.txt file3.txt'
时只能returnsfile1.txt。后面cmd 运行直接在目标服务器上returns所有3个文件。
如何让它在 paramiko 中工作?
PS。我发现了另一种适用于 paramiko 的语法:cmd='ls -l /my/path/{file1.txt,file2.txt,file3.txt}'
但我仍然想知道是什么原因导致前面提到的失败。
这是您的 cmd
问题:
例如,我在示例文件夹下有 3 个文件:
➜ ls example
file1 file2 file4
找不到文件 2:
➜ ls -l example/file1 file2
ls: file2: No such file or directory
-rw-r--r-- 1 haifzhan staff 391 28 Nov 10:23 example/file1
ls -l example/file1 file2
告诉 ls
file2 在当前目录下,而不是在文件夹示例下。
如果我们 运行 只针对 file1 和 file2 的命令
➜ ls -l example/file[1-2]
-rw-r--r-- 1 haifzhan staff 391 28 Nov 10:23 example/file1
-rw-r--r-- 1 haifzhan staff 81 28 Nov 10:26 example/file2
或者:
ls -l example/file1 example/file2
-rw-r--r-- 1 haifzhan staff 391 28 Nov 10:23 example/file1
-rw-r--r-- 1 haifzhan staff 81 28 Nov 10:26 example/file2
在您的情况下,ls -l /my/path/{file1.txt,file2.txt,file3.txt}
的工作原理是您告诉 ls
您的所有文件都在 /my/path
下
我已经成功地将 paramiko 的 exec_command 与 ls 一起使用,直到我尝试使用文件列表作为参数。我的函数是:
def jz_orion_ssh_sout_list(cmd):
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(orion['host'], username=orion['username'], password=orion['password'])
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd)
sout = ssh_stdout.readlines()
serr = ssh_stderr.readlines()
return sout
在cmd='ls -l /my/path/file.txt'
时可以正常工作,但在cmd='ls -l /my/path/file1.txt file2.txt file3.txt'
时只能returnsfile1.txt。后面cmd 运行直接在目标服务器上returns所有3个文件。
如何让它在 paramiko 中工作?
PS。我发现了另一种适用于 paramiko 的语法:cmd='ls -l /my/path/{file1.txt,file2.txt,file3.txt}'
但我仍然想知道是什么原因导致前面提到的失败。
这是您的 cmd
问题:
例如,我在示例文件夹下有 3 个文件:
➜ ls example
file1 file2 file4
找不到文件 2:
➜ ls -l example/file1 file2
ls: file2: No such file or directory
-rw-r--r-- 1 haifzhan staff 391 28 Nov 10:23 example/file1
ls -l example/file1 file2
告诉 ls
file2 在当前目录下,而不是在文件夹示例下。
如果我们 运行 只针对 file1 和 file2 的命令
➜ ls -l example/file[1-2]
-rw-r--r-- 1 haifzhan staff 391 28 Nov 10:23 example/file1
-rw-r--r-- 1 haifzhan staff 81 28 Nov 10:26 example/file2
或者:
ls -l example/file1 example/file2
-rw-r--r-- 1 haifzhan staff 391 28 Nov 10:23 example/file1
-rw-r--r-- 1 haifzhan staff 81 28 Nov 10:26 example/file2
在您的情况下,ls -l /my/path/{file1.txt,file2.txt,file3.txt}
的工作原理是您告诉 ls
您的所有文件都在 /my/path