How to get an email notification when a USB storage device is inserted?
The way to do this is to use a udev rule which will apply to all USB storage devices from any manufacturer. For the email part, note that Ubuntu Desktop does NOT include any command-line email client, so unless you wish to install one, the notification script should communicate directly via raw SMTP commands to your local mail server.
You can use Cuttlefish ( https://apps.ubuntu.com/cat/applications/cuttlefish/ )!
Cuttlefish is a program that can perform actions when events occur. For example, you can run a command when a USB is plugged in:
How to work with Cuttlefish:
- Make a new Reflex from the upper left corner:
- Give it the name of your preference, enable
Activated by stimulus
, and click onNone
so as to select a stimulus: - Click on the left
Hardware
and on the rightUSB device plugged in
. Then click OK. - Click on the reaction tab and click on the plus sign on the bottom left:
- Head to Applications->Start Application (advanced mode). Then click OK.
- Now, on the right, you can see that you can select an executable to run with any parameters you wish, so, you have to let Cuttlefish run on the system tray and it will run your command each time a USB device is plugged in:
I assume that you know a command which you can use in order to send your email. If not, please post it as a comment so as to include it into my answer.
As per this answer, add this code to /etc/udev/rules.d/90-local.rules
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z]1", ATTRS{vendor}=="SanDisk ", RUN+="/usr/local/bin/usb.sh"
and in the usb.sh
file , add this code
#!/bin/bash
ifconfig | grep ip >>/tmp/usb.log
echo “USB inserted.” | mutt -a /tmp/usb.log -s “attachment” [email protected]
source
Now i am gonna try to automate it, so you can add this rule/script in 600 Ubuntu systems.
sudo echo " ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z]1", ATTRS{vendor}=="SanDisk ", RUN+="/usr/local/bin/usb.sh" ">>/etc/udev/rules.d/90-local.rules
#then the `usb.sh`
sudo echo " #!/bin/bash
ifconfig | grep ip >>/tmp/usb.log
echo “USB inserted.” | mutt -a /tmp/usb.log -s “attachment” [email protected] " >>/usr/local/bin/usb.sh
This script is not tested, and the automating process will be improved.
As soon as I set up some test PC's.