Get the IP address of WIFI interface
Jun Hu
hujun.work at gmail.com
Mon May 16 19:44:27 EDT 2011
Thanks, this code works perfectly in ubuntu 10.04.
one question though, is dbus usually implemented in other distribution of
linux?
On Mon, May 16, 2011 at 12:57 PM, Anssi Saari <as at sci.fi> wrote:
> Neal Becker <ndbecker2 at gmail.com> writes:
>
> One possible solution in Linux is asking NetworkManager, if it's in
> use. It knows which interfaces are active and what kind they are (LAN,
> WLAN, WWAN etc.) NetworkManager communicates via dbus and even
> includes python example scripts. So here's my scriptlet based on
> NetworkManager example nm-state.py. This one prints out all active
> devices and their type and IP address. Easily modified to print only
> WLAN types.
>
> import dbus, socket, struct
>
> bus = dbus.SystemBus()
>
> proxy = bus.get_object("org.freedesktop.NetworkManager",
> "/org/freedesktop/NetworkManager")
> manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
>
> # Get device-specific state
> devices = manager.GetDevices()
> for d in devices:
> dev_proxy = bus.get_object("org.freedesktop.NetworkManager", d)
> prop_iface = dbus.Interface(dev_proxy,
> "org.freedesktop.DBus.Properties")
>
> # Get the device's current state and interface name
> state = prop_iface.Get("org.freedesktop.NetworkManager.Device", "State")
> name = prop_iface.Get("org.freedesktop.NetworkManager.Device",
> "Interface")
> ifa = "org.freedesktop.NetworkManager.Device"
> type = prop_iface.Get(ifa, "DeviceType")
> addr = prop_iface.Get(ifa, "Ip4Address")
>
> # and print them out
> if state == 8: # activated
> addr_dotted = socket.inet_ntoa(struct.pack('<L', addr))
>
> s = "Device %s is activated and has type %s and address %s"
> print s % (name, type, addr_dotted)
> else:
> print "Device %s is not activated" % name
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110516/9c9fbf93/attachment-0001.html>
More information about the Python-list
mailing list