多个文件输出被截断的paramiko ls

2024-05-23 17:31:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我在ls中成功地使用了paramiko的exec_命令,直到我用一个文件列表作为参数进行了尝试。我的职能是:

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'时,它只返回file1.txt。直接在目标服务器上运行的后一个cmd返回所有3个文件。在

如何在帕拉米科工作?在

我发现了paramiko中的另一种语法:cmd='ls -l /my/path/{file1.txt,file2.txt,file3.txt}',但我仍然想知道是什么导致了前面提到的一种语法的失败。在


Tags: 文件pathtxtcmdhostparamikomyusername
1条回答
网友
1楼 · 发布于 2024-05-23 17:31:52

这是您的cmd问题: 例如,我有3个文件夹下的文件:

➜ ls example
file1 file2 file4

找不到文件2:

^{2}$

ls -l example/file1 file2告诉lsfile2在当前目录下,而不是在文件夹示例下。在

如果我们只对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所有文件都在{}下

相关问题 更多 >