问题
I am trying to ultimately use php's shell_exec
function to create new Linux users. I am, however, running into problems even with the debugging. Here is my code
<?PHP
function adduser($username,$password,$server){
try{
//3 debug statements
$output=shell_exec("pwd");
echo $output;
shell_exec("touch test.txt");
//3 debug statements are requested by Christian
echo '<pre>';
print_r(execute('pwd'));
print_r(execute('touch test.txt'));
//actuall code
$output=shell_exec("ssh root@$server \"adduser $username; echo $password | passwd $username --stdin\"");
}
catch(Exception $e){
echo 'could not add user '.$e->getMessage();
}
}
$servers = array('192.168.1.8');
foreach($servers as $server){
echo $_GET['USER']." ".$_GET['PASSWORD'];
adduser($_GET['USER'],$_GET['PASSWORD'],$server);
}
The try-catch
statements don't do anything, leading me to believe that shell errors are not propagated as PHP errors (Python is this way also). The line $output=shell_exec("pwd")
returns the correct directory. The line shell_exec("touch test.txt")
, however, fails to create th file test.txt (even if I give the full path '/home/user/.../test.txt').
Needless to say, the actual code for adding users does not work also.
EDIT I managed to fix some of the code. The touch test.txt
error was as a result of insufficient permissions. Apache logs in with user www-data, I simply created a home folder for that user, and made sure to touch a file in that home folder.
The addition of the three debug statements, as per Christian's requests, are however causing problems now.
EDIT Upon further inspection, it has to do with the inability to ssh as root when logging in as user www-data. ssh -v
returns debug1: read_passphrase: can't open /dev/tty: No such device or address
. My guess is that ssh is asking that generic "would you like to permanently add xxx to known_hosts" but I can't respond. Is there anyway to manually add a user to the known hosts list?
回答1:
- Many (most?) of PHP's internal functions don't throw exceptions, they raise errors. I don't think you will ever see an exception thrown by
shell_exec()
- I might var_dump() the return value, just to ensure you're explicitly aware of what it's returned.
- I would also suggest looking into functions like escapeshellarg() to avoid issues with your input.
As a general case, rather than having PHP execute several commands sequentially, I write a shell script that does everything I need, then call it from PHP. There's one fewer link in the chain when debugging, and I find I a lot easier.
With regards to your SSH command itself, since apache is executing as www-data
, how is it logging into the machine in question as root? Have you added the apache user's key to the remote machine.
回答2:
Please use my function found here.
Run the following code and tell us its output:
echo '<pre>';
print_r(execute('pwd'));
print_r(execute('touch test.txt'));
Edit: If you want to make my script more OO oriented:
function execute_o($cmd,$in){
$out=execute($cmd,$in);
if($out['return']!=0)
throw new Exception('Error '.$out['return'].': '.$out['stderr'].'.');
}
来源:https://stackoverflow.com/questions/5697302/php-shell-exec-touch-redirect-and-adduser