def run(self): try: try: from pyudev.glib import MonitorObserver def device_event(observer, device): self.sessaoMultiseat.evento_dispositivo( device.action, device.device_path) except: from pyudev.glib import GUDevMonitorObserver as MonitorObserver def device_event(observer, action, device): self.sessaoMultiseat.evento_dispositivo( action, device.device_path) context = Context() monitor = Monitor.from_netlink(context) #monitor.filter_by(subsystem='usb'); observer = MonitorObserver(monitor) observer.connect('device-event', device_event) monitor.start() self.loop.run() except: logging.exception('')
def main2(): context = Context() monitor = Monitor.from_netlink(context) monitor.filter_by(subsystem='input') observer = MonitorObserver(monitor) observer.connect('device-event', device_event) monitor.start()
def disk_src(self): try: from pyudev.glib import MonitorObserver def device_event(observer, device): print 'event {0} on device {1}'.format(device.action, device) if device.action == "add": manager.disk_mnt(context) elif device.action == "remove": manager.disk_umnt(device) except: from pyudev.glib import GUDevMonitorObserver as MonitorObserver def device_event(observer, action, device): print 'event {0} on device {1}'.format(action, device) if action == "add": manager.disk_mnt(context) elif action == "remove": manager.disk_umnt(device) context = Context() manager = Storage(self.cfg, self.log) monitor = Monitor.from_netlink(context) monitor.filter_by(subsystem='block', device_type='partition') observer = MonitorObserver(monitor) observer.connect('device-event', device_event) monitor.start() glib.MainLoop().run()
def __init__(self): from pyudev.glib import MonitorObserver GObject.GObject.__init__(self) self.context = pyudev.Context() self.monitor = pyudev.Monitor.from_netlink(self.context) self.monitor.filter_by(subsystem='sound') self.observer = MonitorObserver(self.monitor) self.observer.connect('device-event', self.__device_event)
def get_usb_devices(): context = Context() monitor = Monitor.from_netlink(context) monitor.filter_by(subsystem='usb') observer = MonitorObserver(monitor) observer.connect('device-event', device_event) monitor.start() glib.MainLoop().run()
def _listenOnUsb(self): context = Context() monitor = Monitor.from_netlink(context) monitor.filter_by(subsystem="usb") observer = MonitorObserver(monitor) observer.connect("device-event", self._deviceEvent) monitor.start() glib.MainLoop().run()
def main(): """ main() -- entry point for this program """ global SEND_SMS global DAEMONIZE global PID_FILE # Parse CLI options args = parse_cli() DAEMONIZE = args.daemonize PID_FILE = args.pid_file SEND_SMS = args.sms context = Context() # Populate list of current USB devices for device in context.list_devices(subsystem="usb"): busnum = get_device_info(device, "busnum") devnum = get_device_info(device, "devnum") id_product = get_device_info(device, "idProduct") id_vendor = get_device_info(device, "idVendor") manufacturer = get_device_info(device, "manufacturer") product = get_device_info(device, "product") if busnum: USB_DEVICES.append((device.device_path, busnum, devnum, id_vendor, id_product, manufacturer, product)) monitor = Monitor.from_netlink(context) monitor.filter_by(subsystem='usb') observer = MonitorObserver(monitor) observer.connect('device-event', device_event) monitor.start() if DAEMONIZE: usbwatch_pid = os.fork() if usbwatch_pid != 0: return os.EX_OK write_pid_file(PID_FILE, os.getpid()) xprint("[+] usb-watch by Daniel Roberson @dmfroberson Started. PID %s" % \ os.getpid()) try: glib.MainLoop().run() except KeyboardInterrupt: print "[-] Caught Control-C. Andross has ordered us to take you down." print "[-] Exiting." return os.EX_OK
def loopForDevices(): context = Context() monitor = Monitor.from_netlink(context) monitor.filter_by(subsystem='block',device_type='partition') # monitor.filter_by(subsystem='usb') observer = MonitorObserver(monitor) observer.connect('device-event', device_event) monitor.start() glib.MainLoop().run()
class SoundCardMonitor(GObject.GObject): __gsignals__ = { "add": (GObject.SIGNAL_RUN_FIRST, None, [GObject.TYPE_PYOBJECT]), "change": (GObject.SIGNAL_RUN_FIRST, None, [GObject.TYPE_PYOBJECT]), "remove": (GObject.SIGNAL_RUN_FIRST, None, [GObject.TYPE_PYOBJECT]), } def __init__(self): from pyudev.glib import MonitorObserver GObject.GObject.__init__(self) self.context = pyudev.Context() self.monitor = pyudev.Monitor.from_netlink(self.context) self.monitor.filter_by(subsystem='sound') self.observer = MonitorObserver(self.monitor) self.observer.connect('device-event', self.__device_event) def start(self): self.monitor.start() def __device_event(self, observer, device): action = device.action if action in ['add', 'change']: logging.debug('SOUND DEVICE %s: %s', action, device) if 'id' in device.attributes: c = { 'id': device.attributes['id'], 'model': device.get('ID_MODEL', ''), 'model_db': device.get('ID_MODEL_FROM_DATABASE', ''), 'path': device.get('DEVPATH', '') } self.emit(action, c) elif action == 'remove': logging.debug('SOUND DEVICE %s: %s', action, device) c = {'path': device.get('DEVPATH', '')} self.emit(action, c) return True def get_devices(self): context = pyudev.Context() cards = context.list_devices().match_subsystem('sound') devs = [] for device in cards: if 'id' in device.attributes: c = { 'id': device.attributes['id'], 'model': device.get('ID_MODEL', ''), 'model_db': device.get('ID_MODEL_FROM_DATABASE', ''), 'path': device.get('DEVPATH', '') } devs.append(c) return devs
def __init__(self): """ Start listening for storage events. """ context = Context() monitor = Monitor.from_netlink(context) monitor.filter_by(subsystem='block') monitor_observer = MonitorObserver(monitor) monitor_observer.connect('device-event', self._device_event) monitor.start()
def main(): context = Context() monitor = Monitor.from_netlink(context) #monitor.filter_by(subsystem='input') observer = MonitorObserver(monitor) reload_rules() observer.connect('device-event', device_event) monitor.start() for device in iter(monitor.poll, None): print('{0.action} on {0.device_path}'.format(device)) print('=> {0} is {2} ({1})'.format(device.device_node, device.device_type, device.driver))
def start_usb_detection(self): """ Starts listening to udev events for usb activity """ try: #Remove comments to enable usb local update on boot #if os.path.exists(DEVICE_PART1): # syslog("start_usb_detection: Mount point exists") # self.check_mount_point() context = Context() monitor = Monitor.from_netlink(context) monitor.filter_by(subsystem='usb') observer = MonitorObserver(monitor) observer.connect('device-event', self.device_event) monitor.start() except Exception as e: syslog("usbupd:start_usb_detection: %s" % e)
def monitor(): context = Context() monitor = Monitor.from_netlink(context) monitor.filter_by(subsystem='usb') observer = MonitorObserver(monitor) observer.connect('device-removed', remove_event) observer.connect('device-added', add_event) monitor.start() glib.MainLoop().run()
class InputMonitor(GObject.GObject): __gsignals__ = { "added": (GObject.SIGNAL_RUN_FIRST, None, [GObject.TYPE_PYOBJECT, GObject.TYPE_PYOBJECT]), } def __init__(self): from pyudev.glib import MonitorObserver GObject.GObject.__init__(self) self.context = pyudev.Context() self.monitor = pyudev.Monitor.from_netlink(self.context) self.monitor.filter_by(subsystem='video4linux') self.observer = MonitorObserver(self.monitor) self.observer.connect('device-event', self.__device_event) def start(self): self.monitor.start() def __device_event(self, observer, device): if device.action == 'add': logging.debug('DEVICE ADD: %s', device) for probe in ALL_PROBES: ret = probe(device, self.context) if ret: logging.debug('PROBING OK: %s', ret) self.emit('added', ret[0], ret[1]) return True return True def get_devices(self): devices = [] context = pyudev.Context() cameras = context.list_devices().match_subsystem('video4linux') for device in cameras: for probe in ALL_PROBES: ret = probe(device, context) if ret: devices.append(ret) break #devices.append( (TestInput, {}) ) return devices
def main(): try: from pyudev.glib import MonitorObserver def device_event(observer, device): remap_pokerii(device) remap_filco(device) except: from pyudev.glib import GUDevMonitorObserver as MonitorObserver def device_event(observer, action, device): remap_pokerii(device) remap_filco(device) context = Context() monitor = Monitor.from_netlink(context) monitor.filter_by(subsystem='usb') observer = MonitorObserver(monitor) observer.connect('device-event', device_event) monitor.start() glib.MainLoop().run()
class TestGlibObserver(ObserverTestBase): def setup(self): self.event_sources = [] self.glib = pytest.importorskip('glib') # make sure that we also have gobject pytest.importorskip('gobject') def teardown(self): for source in self.event_sources: self.glib.source_remove(source) def create_observer(self, monitor): from pyudev.glib import MonitorObserver self.observer = MonitorObserver(monitor) def connect_signal(self, callback): # drop the sender argument from glib signal connections def _wrapper(obj, *args, **kwargs): return callback(*args, **kwargs) self.observer.connect('device-event', _wrapper) def create_event_loop(self, self_stop_timeout=5000): self.mainloop = self.glib.MainLoop() self.event_sources.append( self.glib.timeout_add(self_stop_timeout, self.stop_event_loop)) def start_event_loop(self, start_callback): def _wrapper(*args, **kwargs): start_callback(*args, **kwargs) return False self.event_sources.append(self.glib.timeout_add(0, _wrapper)) self.mainloop.run() def stop_event_loop(self): self.mainloop.quit() return False
def device_event(observer, device): print('\n\nevent {0} on device {1}'.format(device.action, device)) time.sleep(3) print("device details", device.device_node) partitions = [ device.device_node for device in context.list_devices( subsystem='block', DEVTYPE='partition', parent=device) ] print("All removable partitions: {}".format(", ".join(partitions))) print("Mounted removable partitions:") for p in psutil.disk_partitions(): if p.device in partitions: print(" {}: {}".format(p.device, p.mountpoint)) except Exception as e: print("error :", e) from pyudev.glib import GUDevMonitorObserver as MonitorObserver def device_event(observer, action, device): print('event {0} on device {1}'.format(action, device)) monitor = Monitor.from_netlink(context) monitor.filter_by(subsystem='usb') observer = MonitorObserver(monitor) observer.connect('device-event', device_event) monitor.start() glib.MainLoop().run()
print('{"action": "%s", "device": "%s"}' % (action, device["ID_MODEL_FROM_DATABASE"])) # for attr in device: # print("attr: %s => %s" % (attr, device[attr])) try: from pyudev.glib import MonitorObserver def device_event(observer, device): broadcastUsbChange(device.action, device) except: from pyudev.glib import GUDevMonitorObserver as MonitorObserver def device_event(observer, action, device): broadcastUsbChange(device.action, device) context = Context() monitor = Monitor.from_netlink(context) monitor.filter_by(subsystem='usb') observer = MonitorObserver(monitor) observer.connect('device-event', device_event) monitor.start() glib.MainLoop().run()
ret = {} config = ConfigParser.ConfigParser() config.read("config") global DEBUG ret["url"] = config.get('MQTT', 'url') ret["port"] = int(config.get('MQTT', 'port')) ret["keepalive"] = int(config.get('MQTT', 'keepalive')) return ret def device_event(observer, device): if device.device_type=='usb_interface': if device.action == 'add': print('{} connected'.format(device)) clnt.publish('usb_inserted', str(device)) if device.action == 'remove': print('{} disconnected'.format(device)) clnt.publish('usb_removed', str(device)) if __name__ == '__main__': conf = init() clnt = initMQTT(conf["url"], conf["port"], conf["keepalive"]) context = Context() monitor = Monitor.from_netlink(context) monitor.filter_by(subsystem='usb') observer = MonitorObserver(monitor) observer.connect('device-added', device_event) observer.connect('device-removed', device_event) monitor.start() glib.MainLoop().run()
def create_observer(self, monitor): from pyudev.glib import MonitorObserver self.observer = MonitorObserver(monitor)