简体   繁体   中英

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. I guess netifaces doesn't like paramiko as well as subprocess or os.system on remote server.

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. You can execute ifconfig , for example, and then parse its output. Alternatively, you can use RPyC to execute actual Python code remotely. It requires running a special service on the server, so it's not as simple as SSH. But running Python code and interacting with the results directly could help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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