29

We are running more than 600 Ubuntu systems in our company. It is a data centre so we have certain policies. We have disabled the usage of storage devices in all the Ubuntu systems. However we would like to configure email alerts. If someone inserts storage devices, we should get an email Alert with subject as below,

Email Alert:

STORAGE DEVICE FOUND on IP: 172.29.35.18

Note:

  • Where as for Windows system, we have certain policies applied in our DC. So there is no problem with Windows system.
  • We need to receive alerts for Ubuntu system also. Any way to accomplish the above task would be great.

Update:

  • Few things i would like to clarify, If the user disconnects the network cable and if the user uses usb for data transfer? I know that i wont get any intimations but later if they connect network is it possible to get those pending notifications??
  • Some uses usb for charging their mobile phones, is it possible get email notifications for that also??
karthick87
  • 81,947
  • I think you should go see this post on superuser : http://superuser.com/questions/305723/using-udev-rules-to-run-a-script-on-usb-insertion

    Ben

    – Ben Sep 24 '12 at 08:12

4 Answers4

24

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.

karthick87
  • 81,947
ish
  • 139,926
  • This is Great +1 from me :D – karthick87 Sep 27 '12 at 15:45
  • Great answer, and it gave all the output need, both email and ip – LnxSlck Sep 28 '12 at 17:10
  • @izx request you to have a look at my question for an update.. – karthick87 Sep 29 '12 at 11:31
  • Perhaps, the /etc/udev/rules.d/usbnotif.sh script should be stored somewhere else to avoid confusion with the rules files. Although technically it can stay there. – elomage Oct 01 '12 at 16:06
  • @karthick87: thanks, I'll look at the phone/network update in a few hours. The phone notification shouldn't be a problem, but the network notification delay may require having a proper SMTP server/client on each machine that can queue mail until delivered. But I'll see what I can do :) – ish Oct 01 '12 at 22:16
  • Maye the OP could use the udev rule to prevent mounting the drive? – xenoid Jan 25 '20 at 13:27
18

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: enter image description here

How to work with Cuttlefish:

  1. Make a new Reflex from the upper left corner: enter image description here
  2. Give it the name of your preference, enable Activated by stimulus, and click on None so as to select a stimulus: enter image description here
  3. Click on the left Hardware and on the right USB device plugged in. Then click OK. enter image description here
  4. Click on the reaction tab and click on the plus sign on the bottom left: enter image description here
  5. Head to Applications->Start Application (advanced mode). Then click OK. enter image description here
  6. 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: enter image description here

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.

hytromo
  • 4,904
  • Do you want me to install cuttlefish & configure it in all 600 ubuntu systems? Then it is a big and difficult task.. – karthick87 Sep 26 '12 at 09:05
  • 7
    After you configured cuttlefish on one of the systems, you can easily clone ~/.cuttlefish. Also if you tick on the autostart at login setting of cuttlefish (you can find it in the preferences), you can setup the autostart on every machine by cloning ~/.config/autostart/cuttlefish.desktop. – Alex Sep 26 '12 at 09:13
  • @karthick87, what Alex said. As far as I know there isn't something you can do by default. So, you should anyway do something on all the 600 systems... – hytromo Sep 26 '12 at 09:16
  • Does that mean that in the end you find this way comfortable enough, after all? – hytromo Sep 27 '12 at 15:59
  • How do you prevent the user from disabling cuttlefish? – xenoid Jan 25 '20 at 13:28
5

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” example@hotmail.com

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” example@hotmail.com " >>/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.

blade19899
  • 26,704
  • This seems to be good +1 from me, please test it and let me know the working status. So that i can apply it in all ubuntu systems.. – karthick87 Sep 26 '12 at 11:59
  • Will this detect USB devices other than made by "SanDisk"? – Takkat Sep 26 '12 at 20:41
  • 5
    No, this will only work for devices made by Sandisk. Additionally, mutt is not installed by default on Ubuntu Desktop, and ifconfig | grep ip gives a null output. – ish Sep 26 '12 at 23:27
1

You'll be able to see any new usb device that does not only grab the power of the server (witch normally includes the phones)

For the USB part : How can I listen for 'usb device inserted' events in Linux, in Python?

Sending an e-mail in python : email - Python - How to send utf-8 e-mail?

For the ethernet use netplugd and use the up or down statements :
Ubuntu Manpage: netplugd -- network cable hotplug management daemon