Getting network devices with Python and udev - 2

  |   Source

Thank you Ben for your kind reply :)

In my first post, I was trying to distinguish Ethernet-like device types.

Here's the final code:

import gudev

client = gudev.Client(['rfkill', 'net'])

for dev in client.query_by_subsystem('net'):
    if dev.get_sysfs_attr_as_int("type") != 1:
        continue

    driver = dev.get_driver()
    if not driver:
        parent = dev.get_parent()
        if parent:
            driver = parent.get_driver()

    # available: wlan, wwan, wimax
    if dev.get_devtype() == 'wlan':
        type = 'Wireless'
    else:
        type = 'Wired'

    print type, dev.get_name(), driver, dev.get_sysfs_path()

However, I have a few remarks.

First, I don't understand why ethernet devices don't have a "devtype" attribute. How can I be sure that, failing to be "wlan", "wwan" or "wimax", it's a wired one? (not that any other kind of device comes to my mind, heh).

Second, I had to look through Linux sources to find the possible values for DEVTYPE; in particular, I grepped for device_type structs inside ./drivers/net/. And the documentation you pointed me to, while being useful for other reasons (the rfkill support), didn't solve my doubts about the sysfs hierarchy.

Rather, your comment about DEVTYPE helped quite a lot in this particular problem.

Thank you! :)

Comments powered by Disqus
Contents © 2013 David Paleino - Powered by Nikola