繁体   English   中英

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

[英]Python - netifaces module on remote linux hosts

带有netifaces 的Python 非常适合在 localhost 上收集 ip、网络掩码信息,但我很难从远程服务器收集相同的信息。 我猜 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

这就是它失败的方式:

# 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

谢谢

exec_command执行命令,而不是 python 函数。 例如,您可以执行ifconfig ,然后解析其输出。 或者,您可以使用RPyC远程执行实际的 Python 代码。 它需要在服务器上运行一个特殊的服务,所以它不像SSH那么简单。 但是运行 Python 代码并直接与结果交互可能会有所帮助。

暂无
暂无

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

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