无法使用 paramiko 查看 ifconfig 输出 [英] Unable to see ifconfig output using paramiko

查看:50
本文介绍了无法使用 paramiko 查看 ifconfig 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码在远程机器上执行命令,

I am using below code to execute commands on a remote machine,

import paramiko
import os
dssh = paramiko.SSHClient()
dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
dssh.connect('192.168.1.5', username='root', password='asdfghhh')

import os

stdin, stdout, stderr = dssh.exec_command('ls')
print stdout.read()
stdin, stdout, stderr = dssh.exec_command('ifconfig')
print stdout.read()
stdin, stdout, stderr = dssh.exec_command('ps')
print stdout.read()
dssh.close()

当我执行程序时,它能够显示 ls 和 ps 以及其他命令输出.但是没有观察到 ifconfig o/p.

when I execute the program, Its able to show the ls and ps as well as other commands output. however ifconfig o/p is not observed.

知道如何解决这个问题吗?提前致谢...

any idea how to solve this problems? Thanks in advance...

推荐答案

您的服务器可能会区分交互式和非交互式 SSH 会话,为不同的会话运行不同的启动脚本.尝试通过 paramiko SSH 会话和常规交互会话在远程主机上运行 echo $PATH 并比较输出.

Your server may be discriminating between interactive and non-interactive SSH sessions, running different startup scripts for the different sessions. Try running echo $PATH on the remote host through the paramiko SSH session and a regular interactive one and compare the outputs.

作为解决方法,您可以在交互式会话中的远程服务器上执行 which ifconfig 以获取绝对路径并在您的 paramiko 命令中使用它.

For a workaround you can do a which ifconfig on the remote server in an interactive session to get the absolute path and use that in your paramiko command.

stdin, stdout, stderr = dssh.exec_command('/abs/path/to/ifconfig')

注意在我的一台主机上,来自 paramiko SSH 客户端的 echo $PATH 的结果是 /usr/bin:/bin,而在交互式会话中它是 /usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin,而ifconfig确实位于/usr/sbin, 即在 paramiko 会话的路径之外.

NOTE On one of my hosts the result of echo $PATH from the paramiko SSH client was /usr/bin:/bin, while in an interactive session it was /usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin, and ifconfig was indeed located in /usr/sbin, i.e. outside the path of the paramiko session.

这篇关于无法使用 paramiko 查看 ifconfig 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆