USB Serial Monitor

USB Serial Monitor

From time to time I need to connect over a USB2Serial adapter to switches/routers. This can be a pain when connecting to multiple devices as I need to check dmeg or /dev each time to see which /dev/ttyUSBx device I just connected, have hit ttyUSB4 before now. Did some googling and put together the below script to popup when a new USB2Serial device is connected.

import pyudev.glib, glib, os, notify2

notify2.init("usb")

context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
observer = pyudev.glib.GUDevMonitorObserver(monitor)

def device_added(observer, device):
    i=0
    if device.device_type == "usb_interface":
        dev = [os.path.join('/dev', f) for f in os.listdir(device.sys_path) if f.startswith('tty')]
        count=len(dev)
        while i < count:
            usb=dev[i].encode("ascii")
            ttydev = str(usb)
            n = notify2.Notification(ttydev, ttydev)
            n.show()
            i=i+1
        i=0

observer.connect('device-added', device_added)
monitor.start()

mainloop = glib.MainLoop()
mainloop.run()
Show Comments