Python-Paramiko:使用 SSH 连接到服务器,未从“ls”命令获得输出

问题描述

我使用 Paramiko 制作了一个客户端服务器程序。我设法获得连接并发送命令。 例如,如果我发送一个“ifconfig”命令,它确实会返回所需的输出。然而,当我尝试发送 'ls' 命令时,它什么也不返回。我需要服务器让我使用更复杂的命令,比如打开文件,编辑 wordpress 等等。

这会发送 sshCommand:

def sshCommand(command,port=22):
    sshClient = paramiko.SSHClient()  # create SSHClient instance

    sshClient.set_missing_host_key_policy(
        paramiko.AutoAddPolicy())  # AutoAddPolicy automatically adding the hostname and new host key
    sshClient.load_system_host_keys()
    sshClient.connect(host,port,username,password)
    stdin,stdout,stderr = sshClient.exec_command(command)
    print(stdout.read())

当我发送 -

sshCommand('ifconfig')

一切正常。 但是,发送 -

sshCommand('ls')

可能是因为某种原因我没有访问这些文件的权限?我还有用于连接的root密码

任何帮助将不胜感激。

解决方法

如果您想调试命令不起作用的原因,另请阅读stderr

Command executed with Paramiko does not produce any output


不过,如果您要操作文件,请不要使用 shell 命令。使用 SFTP。

How to list all the folders and files in the directory after connecting through SFTP in Python