Question

How can I do the following things in python:

  1. List all the IP interfaces on the current machine.
  2. Receive updates about changes in network interfaces (goes up, goes down, changes IP address).

Any python package available in Ubuntu Hardy will do.

Was it helpful?

Solution

I think the best way to do this is via dbus-python.

The tutorial touches on network interfaces a little bit:

import dbus
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.NetworkManager',
                       '/org/freedesktop/NetworkManager/Devices/eth0')
# proxy is a dbus.proxies.ProxyObject

OTHER TIPS

I have been using the following code,

temp =  str(os.system("ifconfig -a | awk '$2~/^Link/{_1=$1;getline;if($2~/^addr/){print _1" "}}'"))

it will give the 'up' network interfaces

e.g. eth0, eth2, wlan0

No, no... You don't need to bother os.system() or dbus API.

What you really need is to use netlink API to implement this. Either use libnl interface (netlink.route.link) or handle netlink messages by yourself. Take a look at this example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top