3

short version:

is there a library to parse the output of the unix command:

ip address

If the answer is no, do you know any python networking library that I could use to speed up the development of a parser?

Detailed explanation:

I'm trying to build an open source abstraction layer to retrieve information from network devices with different protocols (SSH, SNMP, HTTP) to be used in wireless community networks, I started from SSH (prototype here) and initially I found a parser for the "ifconfig" command, but the parser does not take into account many cases plus my networking friends told me the "ifconfig" has been deprecated and "iproute2" is the future.

So, before start to develop my own, I ask if anybody knows if there is any open source python parser for the output of the "ip address" unix command. I just need to retrieve a list of interfaces, each interface being a dictionary or an object, with all the details contained in the output of the command (name, type, ip, mac, mtu, ecc).

The only thing I've found is this undocumented and unfinished github repo: https://github.com/nwhalen/python-iproute2

10
  • 1
    Instead of parsing the output of a command line tool, I would first try to get the information directly from /proc/net - that's where the ifconfig or ip commands get most of their information from. Commented Nov 24, 2013 at 17:54
  • 2
    As for networking libraries to help you with this, check out the netifaces module. Commented Nov 24, 2013 at 18:05
  • thx Lukas, keep in mind i'm not trying to retrieve the interfaces on the machine where python is installed, but on a remote machine. Commented Nov 24, 2013 at 18:40
  • Oh I see - so you SSH into a remote machine, and want to enumerate the interfaces? How would that work with SNMP and HTTP though, unless there's an SMNP agent / HTTP webserver running on that machine that provides that info? Commented Nov 24, 2013 at 18:44
  • 2
    One more hint: The -o option (oneline) to ip might give you output that's much easier to parse. Sorry to put all this in comments, but I currently don't have the time to write up a decent answer that's portable, covers most of the cases and is tested. Commented Nov 24, 2013 at 19:26

3 Answers 3

1

You can use the python pyroute2 library:

from pyroute2 import IPRoute
ip = IPRoute()
ip.get_addr(label='eth0')[0].get_attr('IFA_ADDRESS')

For more information you can check here: http://pyroute2.org/pyroute2-0.3.14p4/iproute.html

Sign up to request clarification or add additional context in comments.

Comments

1

Please just use the python netifaces library.

pip install netifaces

Usage

>>>import netifaces
>>>netifaces.interfaces()
['lo0', 'gif0', 'stf0', 'en0', 'en1', 'fw0']
>>>netifaces.ifaddresses('lo0')
{18: [{'addr': ''}], 2: [{'peer': '127.0.0.1', 'netmask': '255.0.0.0', 'addr': '127.0.0.1'}], 30: [{'peer': '::1', 'netmask': 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 'addr': '::1'}, {'peer': '', 'netmask': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::1%lo0'}]}

Comments

0

I used scapy for packetforgery and network tricks, for parsers I always use ast library e re (regexp) library, they're all in python stdlib.

instead of theese you can always do:

import socket
socket.gethostbyname(socket.gethostname())

...and all the sockets methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.