LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Closed Thread
  Search this Thread
Old 10-27-2017, 03:23 PM   #1
or1on
LQ Newbie
 
Registered: Oct 2017
Posts: 22

Rep: Reputation: Disabled
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?
 
Old 10-28-2017, 05:37 AM   #2
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by or1on View Post
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.
 
2 members found this post helpful.
Old 10-28-2017, 09:12 AM   #3
or1on
LQ Newbie
 
Registered: Oct 2017
Posts: 22

Original Poster
Rep: Reputation: Disabled
what is the udev rule to use for usb insert to run the script?????
 
Old 10-28-2017, 09:38 AM   #4
or1on
LQ Newbie
 
Registered: Oct 2017
Posts: 22

Original Poster
Rep: Reputation: Disabled
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"
 
Old 10-28-2017, 09:43 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,498

Rep: Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110
Look at the link I posted in your other thread
 
1 members found this post helpful.
Old 10-28-2017, 04:42 PM   #6
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by or1on View Post
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?
 
1 members found this post helpful.
Old 10-29-2017, 04:37 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,243
Blog Entries: 10

Rep: Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160
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.
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Virus Scanning on Linux LXer Syndicated Linux News 0 10-29-2011 06:41 AM
Bash script: Scanning for text tboss888 Programming 3 01-27-2009 10:27 AM
Virus Scanning Windoze... yogaboy2 General 2 08-21-2007 03:37 PM
Virus scanning XP across network w/ Clam? ExoZagNoid Linux - Networking 2 01-23-2006 10:01 PM
Virus Scanning? gauge73 Linux - Software 1 07-07-2003 11:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration