如何解决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。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。