I want to write a simple script that will send email to a specific address as soon as a USB device is plugged into a system or unplugged. Would someone please provide me a code snippet for this? I want to run it on different flavors of Linux where Ruby will be installed already.
问问题
556 次
1 回答
2
You could add a new udev rule as follows. Create a file /etc/udev/rules.d/99-my-custom-rule
, whose contents looks as follows:
SUBSYSTEM=="usb", ACTION=="add", RUN+="usb_notify_admin add %b"
SUBSYSTEM=="usb", ACTION=="remove", RUN+="usb_notify_admin remove %b"
Then put a script usb_notify_admin
somewhere in the PATH:
#!/bin/sh
echo $@ | mail -s "USB Notify Script" admin@example.com
Details:
- Writing udev rules: http://www.reactivated.net/writing_udev_rules.html
- mail man page: http://linux.die.net/man/1/mail
于 2012-04-16T11:01:19.340 回答