LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Usb virus scanning bash script (https://www.linuxquestions.org/questions/linux-newbie-8/usb-virus-scanning-bash-script-4175616515/)

or1on 10-27-2017 03:23 PM

Usb virus scanning bash script
 
Can someone help me out on how to program a bash script to scan a USB upon insert? And in the script I would like to send a notification to user about the progress. Like "Scanning USB for virus". Then "USB Scan Completed?". I would like to be able to open clamtk when the user plugs the USB. Please help?

joe_2000 10-28-2017 05:37 AM

Quote:

Originally Posted by or1on (Post 5774524)
Can someone help me out on how to program a bash script to scan a USB upon insert? And in the script I would like to send a notification to user about the progress. Like "Scanning USB for virus". Then "USB Scan Completed?". I would like to be able to open clamtk when the user plugs the USB. Please help?

There are two seperate problems you need to solve.

The first one is to run a script upon insertion of the usb stick. This can be achieved via udev rules.

Showing output to the user can be achieved in many different ways, one of which is the xmessage command. Try experimenting with it on the command line before including it into your script. (Depending on your disto you might have to install it first, on Arch the package name is xorg-xmessage, in Debian-land (so probably also the *buntus) it should be x11-utils

Note that when called from within a script that is triggered via udev you won't see anything by default unless you set the DISPLAY environment variable beforehand.
Example:

Code:

export DISPLAY=:0
xmessage "Virus scan complete"

The value :0 may be different on your machine.
You can find out the correct value for DISPLAY by running
Code:

echo $DISPLAY
Alternatively to xmessage you can also open a terminal emulator that runs your script.
Code:

export DISPLAY=:0
xterm -e "/path/to/script.sh"

The advantage is that you can inform the user about progress via messages to stdout.

or1on 10-28-2017 09:12 AM

what is the udev rule to use for usb insert to run the script?????

or1on 10-28-2017 09:38 AM

DOES THIS CODE MAKE ANY SENSE::>>>

#!/bin/bash

if ! [ -f /etc/udev/rules.d/80-doOnUSBinsert.rules ]
then # rule not added
cp "$0" /usr/bin/usbscanner
chmod u x /usr/bin/usbscanner

echo 'SUBSYSTEM=="usb", ACTION=="add", RUN ="/usr/bin/usbscanner & "' | tee /etc/udev/rules.d/80-doOnUSBinsert.rules
if [ $? -eq 0 ]
then
echo 'Rule Successfully added. See file "/usr/bin/doOnUSBinsert" if you wish to edit the command'
exit 0
else
echo 'ERROR while adding rule'
exit 1
fi
fi

export DISPLAY=:0
xmessage -buttons "Continue" "Scan USB for Virus"
clamtk
xmessage -buttons "Finish" "USB Scan Complete"

michaelk 10-28-2017 09:43 AM

Look at the link I posted in your other thread

joe_2000 10-28-2017 04:42 PM

Quote:

Originally Posted by or1on (Post 5774715)
DOES THIS CODE MAKE ANY SENSE::>>>

#!/bin/bash

if ! [ -f /etc/udev/rules.d/80-doOnUSBinsert.rules ]
then # rule not added
cp "$0" /usr/bin/usbscanner
chmod u x /usr/bin/usbscanner

echo 'SUBSYSTEM=="usb", ACTION=="add", RUN ="/usr/bin/usbscanner & "' | tee /etc/udev/rules.d/80-doOnUSBinsert.rules
if [ $? -eq 0 ]
then
echo 'Rule Successfully added. See file "/usr/bin/doOnUSBinsert" if you wish to edit the command'
exit 0
else
echo 'ERROR while adding rule'
exit 1
fi
fi

export DISPLAY=:0
xmessage -buttons "Continue" "Scan USB for Virus"
clamtk
xmessage -buttons "Finish" "USB Scan Complete"

Some thoughts:

Your code is hard to read like that, please use code tags and indentation to make the code more readable.

Generally speaking I would not mix the installation code with the actual script you want to run upon usb insertion.
If this script is run via udev it makes no sense to check whether the rule is installed - it must be, otherwise the script would not be running.

You should also not put any of your own scripts into /usr/bin. This is the area where your distro puts its stuff. A better place for your own scripts is /usr/local/bin. That way you can avoid collisions with executables provided by your distro.

I have no experience with clamtk, but maybe you can get it to limit its scan to only the content of the USB device?

ondoho 10-29-2017 04:37 AM

i'm a little miffed that op has started another, imo identical, thread, because i posted there and now come here to see that my reply would have been different if i had seen this first.
sound confusing?
exactly.

i am reporting this as a duplicate thread, but tbh as an admin i wouldn't know what to do with it at this point.


All times are GMT -5. The time now is 02:54 PM.