LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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


Reply
  Search this Thread
Old 08-03-2012, 03:16 AM   #1
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Rep: Reputation: 1
Make a autorun USB that executes a shell script on the USB drive?


How do i make a autorun usb that executes a shell script that is on the USB drive? This would be only for my system so if there is something i have to enable, no problem (i imagine something like this would be disabled for security reasons)

I believe the shell used would be either ash

Thanks
 
Old 08-03-2012, 07:31 AM   #2
cortman
Member
 
Registered: Jan 2012
Location: ZZ9 Plural Z Alpha
Distribution: Crunchbang 11, LFS 7.3, DSL 4.1.10, Lubuntu 12.10, Debian 7
Posts: 219

Rep: Reputation: 43
Quote:
Originally Posted by riahc3 View Post
How do i make a autorun usb that executes a shell script that is on the USB drive? This would be only for my system so if there is something i have to enable, no problem (i imagine something like this would be disabled for security reasons)

I believe the shell used would be either ash

Thanks
Don't think that's possible. That's one of the reasons Linux is secure.
A question like this makes me wonder what your intent is.
 
Old 08-03-2012, 08:03 AM   #3
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Are you talking about an autorun functionality inside a desktop environment or, perhaps, at boot time?
 
Old 08-03-2012, 12:37 PM   #4
floppy_stuttgart
Senior Member
 
Registered: Nov 2010
Location: EU mainland
Distribution: Debian like
Posts: 1,155
Blog Entries: 5

Rep: Reputation: 107Reputation: 107
my thoughts:
- you create an udev rule on the PC (see the net for it)
- this rule, when recognizing an USB with label=ABCD_XX, then it could start the
skript_of_usb.sh on it with root
I never has done it; but could imagine it runs.
I have only created a udev rule until now, detecting a usb modem xxx:yyy, then starting usb_modeswitch with the parameters adapted to the xxx:yyy (other usbs would not be recognized).

Last edited by floppy_stuttgart; 08-03-2012 at 12:39 PM.
 
1 members found this post helpful.
Old 08-03-2012, 03:43 PM   #5
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,996

Rep: Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628
I know of no way to do it as one does it with Windows. All sorts of issues prevent it in linux.

On top of that is the ash shell. Who has that?
 
Old 08-03-2012, 04:20 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by jefro View Post
All sorts of issues prevent it in linux.
Which ones would that be?


Quote:
Originally Posted by jefro View Post
On top of that is the ash shell. Who has that?
I have but then again I run Heirloom, Dash and other shells as well: "The ash shell is a clone of Berkeley's Bourne shell (sh). Ash supports all of the standard sh shell commands, but is considerably smaller than sh. The ash shell lacks some Bourne shell features (for example, command-line histories), but it uses a lot less memory. You should install ash if you need a lightweight shell with many of the same capabilities as the sh shell."


Quote:
Originally Posted by riahc3 View Post
How do i make a autorun usb that executes a shell script that is on the USB drive? This would be only for my system so if there is something i have to enable, no problem (i imagine something like this would be disabled for security reasons)
I'm sure Udev rules should work but I agree it is not a feature one would want or need: Linux autorun vulnerabilities? (2011).
 
Old 08-03-2012, 07:45 PM   #7
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,996

Rep: Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628
I see my error. I assumed it was to be for any computer. My post was incorrect.

I get it now. At some event like mount or hotplug event you want some script to run some file or such that is on the usb. So you'd need to select some reportable action, then use some way to test for that action, then run script. If not a test for action some action that is modified.

Last edited by jefro; 08-03-2012 at 07:48 PM.
 
Old 08-10-2012, 03:04 AM   #8
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
let me explain a bit of the situation as i see a lot of people are having security woes....


im setting up a embedded ARM Linux router (similar to DD-WRT devices) running ash. This is to be used as a OpenVPN client which connects to (say) 123.123.123.123 I (my company) preconfiguures the router and send it off to the client installed. I have the OpenVPN connection BUT what if my 123.123.123.123 server changes to another ip? thats where all of this comes in: I simply send a shell script, tell him to put it on the usb, tell him to plug it into his router, and the ip autoreplaces it in the openvpn client configuration updating the router without have the client to do anything except download the shell from his email and put it on the usb

thats why security is fully controlled by me


now, on to doubts....


Quote:
Originally Posted by floppy_stuttgart View Post
my thoughts:
- you create an udev rule on the PC (see the net for it)
- this rule, when recognizing an USB with label=ABCD_XX, then it could start the
skript_of_usb.sh on it with root
I never has done it; but could imagine it runs.
I have only created a udev rule until now, detecting a usb modem xxx:yyy, then starting usb_modeswitch with the parameters adapted to the xxx:yyy (other usbs would not be recognized).
I actually tried to create a udev rule but it does not work. a simple one that just does a "touch" on a file where i (and anyone) can write and permissions are correct. i also have the problem that i do not know the usb device the other end has so vender/product id/label does not work for me. it has to autorun all usb devices that are mass storage.


Quote:
Originally Posted by unSpawn View Post
I'm sure Udev rules should work but I agree it is not a feature one would want or need: Linux autorun vulnerabilities? (2011).
well it is a factory so security on there isnt something top notch on priorities; we do not intent to make their system more secure, just provide a solution.
 
Old 08-10-2012, 03:15 AM   #9
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
another options is using crontab to run script 1 which is on the device every minute or so

i would run script 1 similar to this every minute or so (pseudocode)

if /tmp/media/sda1 exists or /tmp/media/sda2 exists or /tmp/media/sda3 exists or /tmp/media/sda4 exists
then do
variable storage=what ever above first returns true (lets say /tmp/media/sda3)
//storage=/tmp/media/sda3
if storage+"/"+.runmescript exist
then do
./runmescript
end if
end if

which ive made more or less into this (still need help)


if [ '/tmp/media/sda1' exists ] || [ '/tmp/media/sda2' exists ] || [ = '/tmp/media/sda3' exists ] || [ '/tmp/media/sda4' exists]
then
$theonethatexists = theonethatreturnedtrueabove
if [ -f $theonethatexists+'/'+'.runmescript' ]
then
./$theonethatexists+'/'+'.runmescript'
fi
else
echo "Error: File Not Found";
fi


.runmescript would be this pseudocode:

$variable=182.34.145.125
load /etc/someconf
search for "dot=123.123.123.123" in /etc/someconf
replace "123.123.123.123" with $variable
save
reboot device
 
Old 08-10-2012, 03:19 AM   #10
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Also the udev rule that I had that did not work is this:

ACTION=="add",SUBSYSTEM=="usb_device",SYSFS{idVendor}=="090c",SYSFS{idProduct}="1000",RUN+="/home/admin/runme.sh"


it was placed in

/etc/udev/rules.d/10-my_autorun.rules

and yes, both files have permissions

And runme.sh was:

#!/bin/ash
echo "Hello world"
touch /me
 
1 members found this post helpful.
Old 08-10-2012, 03:28 AM   #11
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
I think the crontab script is better and these device are only used for OpenVPN connections. They dont have a huge workload that would cause lag because crontab is running a script every minute.
 
Old 08-10-2012, 04:36 AM   #12
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
You could resort to another approach which, I guess, should work fine: an inotify-based daemon C program.
You can use the inotify system to monitor the directory where USB mass storage devices are mounted (if they're not automounted upon insertion then a custom udev rule for that could be useful) and, upon being awakened by a mount event inside that directory, you could check for the existence of your script inside one of the subdirectories and execute it if needed.
Given that inotify provides file descriptors to be read by the read() system call, I guess the daemon process would just sleep most of the time.
 
Old 08-10-2012, 05:31 AM   #13
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by 414N View Post
You could resort to another approach which, I guess, should work fine: an inotify-based daemon C program.
You can use the inotify system to monitor the directory where USB mass storage devices are mounted (if they're not automounted upon insertion then a custom udev rule for that could be useful) and, upon being awakened by a mount event inside that directory, you could check for the existence of your script inside one of the subdirectories and execute it if needed.
Given that inotify provides file descriptors to be read by the read() system call, I guess the daemon process would just sleep most of the time.

seems the inotify liberary is not avaliable (or at least I do not find it). Remember this is a embedded small ARM based router.
 
Old 08-10-2012, 08:21 AM   #14
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
inotify support should be enabled in the kernel and, from userspace, you'd need this library to use it.
 
Old 08-13-2012, 02:09 AM   #15
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by 414N View Post
inotify support should be enabled in the kernel and, from userspace, you'd need this library to use it.
Is this library compatible with the ARM instruction set? And this kernel is not built from me. The kernel is:

Linux 2.6.36 #1 Thu May 24 18:41:45 CEST 2012 ppc GNU/Linux

I perfer the crontab method as it is more univeral and I can move it a lot easier without relying on libraries and a C program/interpreter. The only thing is that I dont know how to write the script.
 
  


Reply



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
make a USB drive bootable cccc Linux - General 2 10-25-2008 11:23 PM
How do you make a usb drive bootable? comcastuser Linux - General 4 06-25-2007 02:19 AM
execute shell script when USB drive plugged in OneSeventeen Linux - Hardware 2 10-28-2005 10:27 AM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM
can you make a usb drive bootable? citrus Linux - Newbie 4 05-16-2004 04:00 AM

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

All times are GMT -5. The time now is 06:19 AM.

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