Solved

How to execute a shell script from a PHP program.

Posted on 2004-03-21
13
1,258 Views
Last Modified: 2012-08-13

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>
0
Comment
Question by:7string
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 500 total points
ID: 10646672

Well, there are several functions to execute shell command:
http://www.php.net/manual/en/ref.exec.php

Here is one example:
<?php
  $output = shell_exec("cmd");
  echo "<pre>$output</pre>";
?>

In your case... this code should work..

<?php
  $dir_path =  "/full_path_to_sub-directory/$dirname";
  mkdir($dir_path,0644);
  $output = shell_exec("chown loginID:loginID $dir_path"); //
  echo "<pre>$output</pre>";
 // shell_exec("mv $dir_path newpath"); // to move directory to a different location
?>

venkat.
0
 

Author Comment

by:7string
ID: 10646834

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
0
 

Author Comment

by:7string
ID: 10646901
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
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10650496

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.
0
 

Author Comment

by:7string
ID: 10653799
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
0
 

Author Comment

by:7string
ID: 10654447
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.
0
 

Author Comment

by:7string
ID: 10654487
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
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10654517

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.
0
 

Author Comment

by:7string
ID: 10654659
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
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10655017

Yes I agree with you... But I had the same problem and I ended up writing expect script to do the job.
0
 

Expert Comment

by:seebach
ID: 10813299
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);
0
 
LVL 1

Expert Comment

by:joesp
ID: 10886080
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.
0
 
LVL 1

Expert Comment

by:joesp
ID: 10886139
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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question