简体   繁体   English

Python - 远程 Linux 主机上的 netifaces 模块

[英]Python - netifaces module on remote linux hosts

Python with netifaces works great to gather ip,netmask information on localhost but I'm having great difficulties to gather same info from remote server.带有netifaces 的Python 非常适合在 localhost 上收集 ip、网络掩码信息,但我很难从远程服务器收集相同的信息。 I guess netifaces doesn't like paramiko as well as subprocess or os.system on remote server.我猜 netifaces 不喜欢 paramiko 以及远程服务器上的 subprocess 或 os.system 。

def interface_details():

    for iface in netifaces.interfaces():

        if iface == 'lo':

            continue

        iface_details = netifaces.ifaddresses(iface)

        if iface_details.has_key(netifaces.AF_INET):

            ipv4 = iface_details[netifaces.AF_INET]

        return ipv4

And this is how it fails:这就是它失败的方式:

# Using Paramiko to execute cmd.
>>> host_ssh.exec_command(interface_details())

   File "build/bdist.linux-x86_64/egg/paramiko/client.py", line 441, in exec_command
  File "build/bdist.linux-x86_64/egg/paramiko/channel.py", line 60, in _check
  File "build/bdist.linux-x86_64/egg/paramiko/channel.py", line 231, in exec_command
  File "build/bdist.linux-x86_64/egg/paramiko/message.py", line 285, in add_string
  File "build/bdist.linux-x86_64/egg/paramiko/common.py", line 170, in asbytes
Exception: Unknown type

Thanks谢谢

exec_command executes a command, not a python function. exec_command执行命令,而不是 python 函数。 You can execute ifconfig , for example, and then parse its output.例如,您可以执行ifconfig ,然后解析其输出。 Alternatively, you can use RPyC to execute actual Python code remotely.或者,您可以使用RPyC远程执行实际的 Python 代码。 It requires running a special service on the server, so it's not as simple as SSH.它需要在服务器上运行一个特殊的服务,所以它不像SSH那么简单。 But running Python code and interacting with the results directly could help.但是运行 Python 代码并直接与结果交互可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM