Hello I am working on an embedded linux device with a usb port that uses the g_ether driver for usb networking.
When the usb plug is connected the dmesg output is:
Unfortunately, there is no udev event produced on connect/disconnect on gadget side, so it is almost to impossible to monitor these events.
You could monitor kernel messages (dmesg). It seems to be stupid idea. Or watch some files in sysfs. Possible better way is kernel patching.
update: I do not understand why this answer got negative rating.
Maybe some people mix USB host part (which produces UDEV events on device plug/unplug) and USB device/gadget part (which doesn't produce such events)
If your linux computer works as a gadget (USB device which is connected to some USB host) there is no good way to catch plug/unplug events.
Proof: message by Greg Kroah-Hartman
another copy if previous link is down