Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: USB Mount Script

  1. #1
    Join Date
    Sep 2007
    Beans
    85

    Exclamation USB Mount Script

    I wondered if someone could possibly help

    Basically, I wondered if there was a chance of being able to run a script as soon as a usb flash disk was inserted into the machine?

    I know this sounds like a virus creation tool.. but its not.

    A system running M$ windows has been infected by a USB virus, in which it latches files onto the removable media as soon as it is inserted. once that stick is introduced onto another system, that system then becomes infected.

    so, im thinking it's going to contain a line like

    avgscan /media/disk1

    but thats as far as i got

    I thought about using Cron, but obviously, thats going to have to be a scheduled task.

    Any ideas?! I've had to Ban USB sticks in my house!!!

  2. #2
    Join Date
    Jun 2009
    Location
    If only I knew....
    Beans
    424
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: USB Mount Script

    No idea really, but I'm interested in doing something similar, so I'll stay subscribed to the thread.
    Dell Latitude E6400 | Intel Core 2 Duo T9400 @ 2.53 GHz | 4GB DDR2 SDRAM @ 800 MHz | 250GB HDD 7200 RPM | NVIDIA Quadro NVS 160M 512 MB
    Use a Dell E-Latitude? | I lost The Game

  3. #3
    Join Date
    Sep 2007
    Beans
    85

    Re: USB Mount Script

    Quick bump as still lacking the solution!

  4. #4
    Join Date
    Jan 2009
    Location
    Millbury, MA
    Beans
    414
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: USB Flash Drive Mount Detection Script

    I don't have the actual code for you, just an idea. How about a script with a looping structure that periodically checks for the existence of the mounted flash drive in /media and then acts accordingly. Not a very graceful solution but it ought to work. Technically you could do it with cron too but you'd be limited to checking at most once per minute with the possibility of a lag between insertion and discovery of up to a minute which may be too late for your purposes.

    Ideally you are looking for an event which is triggered upon insertion of a USB flash drive and that can be intercepted. I'm not aware of one but there probably is something like that deep in Ubuntu's bowels.

    BTW, are you concerned about a virus that has embedded itself in the flash drive via Windows infecting your Ubuntu system? Because that's just not going to happen. I, like many others don't even run an antivirus program in Ubuntu and it lives peacefully right alongside a Windows Vista installation.

    tgeer

    p.s. Note the title of my reply. It more accurately describes what you are looking for and is more likely to elicit a proper response. You may want to consider editing the title in your original post. As it reads now, it looks like you're searching for a script to mount a USB drive rather than to detect its insertion.
    "Programming is an art form that fights back."

    HP Pavillion DV9500T 17" Notebook • 2.2GHz Core2 Duo • 4GB RAM • Nvidia Geforce 8600M GS 512MB
    Running Ubuntu 10.04 [64-bit]+Gnome+Compiz from HD / Puppy Linux 4.2.1SMP in RAM from USB Flash

  5. #5
    Join Date
    Dec 2007
    Location
    New York
    Beans
    323

    Re: USB Flash Drive Mount Detection Script

    have you guys heard of google?

    bash-script-detect-list-usb-flash-drives.html

  6. #6
    Join Date
    Dec 2007
    Location
    New York
    Beans
    323

    Re: USB Flash Drive Mount Detection Script

    and after some modifications:

    #!/bin/bash

    for DEV in /sys/block/sd*
    do

    if readlink $DEV | grep -q usb ;then
    DEV=`basename $DEV`
    if [ -d /sys/block/${DEV}/${DEV}1 ] ;then
    echo $DEV
    echo "Has partitions " /sys/block/$DEV/$DEV[0-9]*
    MOUNT=`df | grep $DEV | awk '{print $6}'`
    echo scanning $MOUNT
    clamscan $MOUNT
    #else
    # echo "Has no partitions"
    fi
    echo
    fi
    done
    im having some problems with this script detecting a phantom usb device (/dev/sde), where nothing is mounted, and i dont know why. after i solve that i can make it run on a loop
    Last edited by Johnny B; August 14th, 2009 at 01:41 AM.

  7. #7
    Join Date
    Dec 2007
    Location
    New York
    Beans
    323

    Re: USB Flash Drive Mount Detection Script

    getting better, but still testing, how long do files in /tmp last?

    Code:
    #!/bin/bash
    
    function scan {
    for DEV in /sys/block/sd*
    do
    	if readlink $DEV | grep -q usb ;then
    		DEV=`basename $DEV`
    		if [ -d /sys/block/${DEV}/${DEV}1 ] ;then
    			if [ ! -f /tmp/$DEV.lockfile ] ;then
    				echo $DEV > /tmp/clamscan
    				echo "Has partitions " /sys/block/$DEV/$DEV[0-9]* >> /tmp/clamscan
    				MOUNT=`df | grep $DEV | awk '{print $6}'`
    				echo scanning $MOUNT >> /tmp/clamscan
    				clamscan -r  --infected $MOUNT >> /tmp/clamscan
    				touch /tmp/$DEV.lockfile
    				zenity --info --text="`cat /tmp/clamscan`"
    			#else
    				#echo " /tmp/$DEV.lockfile exists"
    			fi
    		#else
    		#	echo "Has no partitions"
    		fi
    		#echo
    	fi
    done }
    
    while true ;do scan ;done
    Last edited by Johnny B; August 14th, 2009 at 02:15 AM.

  8. #8
    Join Date
    Mar 2008
    Beans
    4,715
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: USB Mount Script

    Here is an account of how to get Ubuntu to automatically run a backup script when a USB drive has been plugged into the machine.
    You can obviously substitute any script you want in place of the backup script:

    http://ninetynine.be/blog/2009/03/ub...rive-on-mount/

  9. #9
    Join Date
    Jan 2009
    Location
    Millbury, MA
    Beans
    414
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: USB Mount Script

    kg87,

    If you're still having problems after trying all of the above then post back here or send me a message. I've got another way of doing it that's easy and pretty bulletproof. Unfortunately I've got pressing matters at the moment so I'll give you the info only if you need it.

    tgeer
    "Programming is an art form that fights back."

    HP Pavillion DV9500T 17" Notebook • 2.2GHz Core2 Duo • 4GB RAM • Nvidia Geforce 8600M GS 512MB
    Running Ubuntu 10.04 [64-bit]+Gnome+Compiz from HD / Puppy Linux 4.2.1SMP in RAM from USB Flash

  10. #10
    Join Date
    Sep 2007
    Beans
    85

    Re: USB Mount Script

    Quote Originally Posted by unutbu View Post
    Here is an account of how to get Ubuntu to automatically run a backup script when a USB drive has been plugged into the machine.
    You can obviously substitute any script you want in place of the backup script:

    http://ninetynine.be/blog/2009/03/ub...rive-on-mount/
    Ok, the issue with this script is that im presuming you've got to edit the udev for each individual usb stick. Now that would be great if they were all the same, But i have at least 7-8 sticks, which to me looks like a lot of editing! (im shy, or lazy...)_

    Johnny B, your script looks very promising, however, im presuming this would have to be manually executed each time a device was inserted?!

    tgeer43, I am not worried about windows virii and cross contamination, but i dont trust the propriatry AV that my family are using on their pc's (Mcafee..) The Ubuntu machine is primarily a sheepdip test, and my main machine

    I've given this some thought, and thinking on a sublinear plane.
    Would it make it easy to disable automount of usb, and only allow it if the user executed a script?

    Once again, Thanks for the support guys!

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •