Detect USB keyboard and other peripherals

Hi there,

I have a Debian machine without any peripherals (no screen, no keyboard, etc.). I'd like to be able to detect and log when someone plugs a USB keyboard. Something like : 2009-07-04 12:21 warning: keyboard pluged!
Is that possible?

I see two ways :

  1. Either actively react to the connection of a device and execute a script that logs the alert.
  2. Or cron a script that checks for USB devices every 5 minutes.
    I have no idea how to do either ways.
    Can anyone help me on that?

I would be glad to just be able to alert when any USB device is plugged. No matter if it's a keyboard or a flash key. But if I can make the difference between each device type... Better!

Thanks for your help
Santiago

The obvious idea that comes to mind is to watch for messages from dmesg (or the /var/adm/messages file?) as USB devices are plugged in, it is how I normally find out what the device path for a USB stick is anyway...

Thanks TonyFullerMalv,
I'm investigating this idea.
Though, I have no such /var/adm/ folder.

Depending on how current your Debian installation is you might have DBUS & HAL installed. There's a command-line tool for DBUS which you can instruct to listen for added devices: (example is a flash drive)

> dbus-monitor --system --profile 'interface=org.freedesktop.Hal.Manager, member=DeviceAdded'
sig     1246865406      537113  2       /org/freedesktop/DBus   org.freedesktop.DBus    NameAcquired
sig     1246865413      849690  6387    /org/freedesktop/Hal/Manager    org.freedesktop.Hal.Manager     DeviceAdded
sig     1246865413      898142  6390    /org/freedesktop/Hal/Manager    org.freedesktop.Hal.Manager     DeviceAdded
sig     1246865414      883198  6393    /org/freedesktop/Hal/Manager    org.freedesktop.Hal.Manager     DeviceAdded
sig     1246865414      883255  6394    /org/freedesktop/Hal/Manager    org.freedesktop.Hal.Manager     DeviceAdded
sig     1246865414      903331  6403    /org/freedesktop/Hal/Manager    org.freedesktop.Hal.Manager     DeviceAdded
sig     1246865415      53744   6430    /org/freedesktop/Hal/Manager    org.freedesktop.Hal.Manager     DeviceAdded
sig     1246865415      97264   6449    /org/freedesktop/Hal/Manager    org.freedesktop.Hal.Manager     DeviceAdded

If you pipe this into a loop you should be able to detect just about any HW change.

Sorry I was thinking Solaris! Try looking in /var/log/messages!

Also try taking a look at the lsusb(8) command.