Link to home
Start Free TrialLog in
Avatar of 7string
7string

asked on

How to execute a shell script from a PHP program.


Hello,

How do I go about executing a shell script via PHP? I have a PHP program that creates a directory, but it creates the directory as “nobody”. I want the directory to be created as the web account login ID.

For example, the directory is created as nobody:

bash-2.05a$ ls -l
drwxr-xr-x    2 nobody   nobody       4096 Mar 21 16:59 test8

But I want the directory to be created as the login ID of the web account:

bash-2.05a$ ls -l
drwxr-xr-x    2 loginID   loginID       4096 Mar 21 16:59 test8

loginID would be the web account login.

CPanel is installed on the server and cPanel has a file manager that you can create and delete the directories logged in your web account with your web account login ID. If I can modify this program to do the same thing that would be great, but, an acceptable work around would be to execute a shell script from the PHP program to “chown loginID:loginID directory” then move the directory to where I want it to go.

Thanks,

Dan.

Here are the two .php files. (the first is index.php and the second is create_directory.php)

This is the index.php file:

<html>
<head>
<title>Create Directory</title>
</head>
 
<body>
 
<form action="create_directory.php" method="post">
<br>
Enter a name to create a directory: <input <?php echo "value=\"$create_dir\""; ?> type="text" name="create_dir" size="10" maxlength="10">
<br>
Enter numbers letters - and _ only. NO spaces. 10 characters max.
<br><br>
<input type="submit" name="submit" value="submit information">
</form>
 
</body>
</html>

This is the create_directory.php file:

<?php
 
$dirname = $_POST['create_dir'];
$errors = array();
 
if($dirname == "") {
    array_push($errors, "You have not entered a name for the new directory!");
}
 
elseif(preg_match('/[^_0-9a-z-]/i', $dirname)) {
    array_push($errors, "The directory name can only contain underscores (_), hyphens (-) and alphanumeric characters!");
}
elseif(is_dir("/full_path_to_sub-directory/$dirname")) {
    array_push($errors, "The directory already exists!");
}
?>
 
<html>
<head>
<title>Create the directory</title>
</head>
 
<body>
 
<?php if(count($errors) > 0) {?>
   The following errors occured...<br><br>
   <?php foreach($errors as $e) {
      echo $e; ?>
       <br>
   <?php } ?>
   <br>
 
   Please click the back button on your browser and correct the mistakes.
<?php } else {?>
   Creating new directory (<?php echo $dirname; ?>)...
   <?php
   // Create folder with read and write access for owner, just read access for everybody else
   mkdir("/full_path_to_sub-directory/$dirname", 0644);
   ?>
   <br><br>
   Done!
<?php } ?>
 
<br>
 
<a href="index.php">Back</a>
 
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of venkateshwarr
venkateshwarr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 7string
7string

ASKER


I tried a few of the functions with no luck. I did try the code above, but the problem is, with the login ID I am using, I am not allowed to chown the directories from nobody to my login ID even though they are in my directory. I also tried the different functions to create the directory, but it only works when the directory I am creating it in is writable to everyone, then it creates the directorie as nobody. I do have root access but I don't want to use the root password to achieve this. Is there another way to get around this?

My server is Linux with cPanel and I am at PHP 4.2.3

Thanks,

Dan
Avatar of 7string

ASKER

I got success using ftp_mkdir() to do what I want. However, I will test the example above tomorrow after work.

Thanks for the help!

Dan

I am not familiar with php. But to change permissions like that there  are two options

1. use rexec() to remotely execute the commnad as a different user, in this case "loginID".  
2. use expect script to login as loginID and change permissions

venkat.
Avatar of 7string

ASKER

Do you mean exec() ??? There is no rexec() in the php manual when I do a search.

Also, if you are not familiar with php, how is it that you are able to help me with php? Just curious.

Anyway, I'm going to see if I can get the script to execute "something". I will post the results here in a few hours.

I wonder if there is a login function in php. I'm going to do a search.

Thanks,

Dan
Avatar of 7string

ASKER

I am able to execute a command, but "only" as nobody. So to do what I want, I think I will have to use one of the ftp functions.
Avatar of 7string

ASKER

I accepted the above answer because it is technically correct. However, all of the commands are executed as “nobody”. I don’t see a way around the problem without having the ability to login. Therefore, I don’t think this is the best way for me to go about it. I think that using one of the ftp functions requiring a loginID and password would be the best way to “create” a directory. That way, the directory is created with the correct owner and group.

Thanks for the help.

Dan

Sorry, I am no familiar with CPanel.

And rexec (remote shell execute )is the unix shell command. (sorry about the ())
Using this you can execute a  command on a machine as a particular user.
Using this command you can change the permission

 $output = shell_exec("rexec serverip -l username -p password 'chown loginID:loginID $dir_path'"); //

http://www.mkssoftware.com/docs/man1/rexec.1.asp

I hope it helps...

venkat.
Avatar of 7string

ASKER

cPanel is a web interface. It has nothing to do with php. Sorry about that.

Thanks for the rexec explanation. That helps. But I think that it would create a security problem because I believe only root can chown. No way am I going to do that over the web! So, I’m going to investigate using the ftp functions in php. I think that would be the safest way to do this.

Thanks for the help!

Dan

Yes I agree with you... But I had the same problem and I ended up writing expect script to do the job.
if you use the exec command, you'll have the same right as th typical www user.

try

exec(rexec serverip -l username -p password 'chown loginID:loginID $dir_path,$output);
//show the output
echo join($output);
way more common is to put the commands into a shell script which has you as owner, then it will execute with permissions of owner.  perhaps you may need the setuid bit on the shell script, but let's see a stupid server admin allow that one.  Give them both a shot.
the following uses option #1 and changes owner to dave.  Try it out on your server, your mileage may vary.

[joe@www functions_php]$ cat mkdir.sh
#!/bin/sh
mkdir maker
sudo chown dave maker
[joe@www functions_php]$ cat shell_exec.php
<?php
  $dirname="dirname";
  rmdir("maker");
  $output = shell_exec("sh mkdir.sh"); //
?>
[joe@www functions_php]$ ls -lah
total 36K
drwxr-xr-x    3 joe      joe          4.0K Apr 22 02:05 .
drwxr-xr-x   16 joe      users        4.0K Apr  4 20:31 ..
-rwxr-xr-x    1 joe      joe           837 Feb 26 03:25 functions.htm
drwxr-xr-x    2 dave     joe          4.0K Apr 22 02:05 maker
-rw-rw-r--    1 dave     joe            44 Apr 22 02:05 mkdir.sh
-rw-rw-r--    1 joe      joe            63 Apr 12 10:03 php_exec.php
-rw-rw-r--    1 joe      joe           877 Apr 12 10:09 phpExec.txt
-rw-rw-r--    1 joe      joe           506 Apr 22 02:05 shell_exec.php
-rw-r--r--    1 joe      joe           468 Feb 26 03:25 system.php