Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

bash - PHP: mount USB device

I am writing a PHP script for the GUI of a Cent OS server. For testing purposes I have set up the Apache as localhost. Cent OS does not automount a connected USB device. In PHP I need to check if a USB device is plugged in then mount it and list the file content for the user. This is quite simple in bash, however I am unable to make it work in PHP. I have played around with both the exec() and the shell_exec() functions with no avail. I have tried the commands both manually i.e.:

shell_exec("sudo mount /dev/sdb1 /mnt");

and through a bash script:

exec("./mountlist.sh");

Is there a function in PHP I can call that will do the same thing, or am I missing something with the exec and shell_exec functions. Both functions work fine in the shell. Since mount is a root command I included sudo in the bash script.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't sudo from inside a PHP script - there is no way to type in the sudo password.

You could create a shell script and use the STICKY bit to have it run as the root user

http://www.dba-oracle.com/linux/sticky_bit.htm

That's how the passwd command can write to the password file owned by root even though you are running it as a normal user.

Also - you could grant the web user permission to mount/unmount file systems (add him to the fuse group on most systems), but that's more open-ended and rather dangerous if your web server gets hacked, so I would go with shell scripts and sticky bits for your purposes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...