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

时间:2017-03-08 17:27:30

标签: python

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

谢谢

1 个答案:

答案 0 :(得分:0)

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