问题
I have a simple script I'm trying to run:
<?php
print exec('whoami');
$output2 = exec('ssh someotherhost ls -l /path/to/dir',$output);
print_r($output);
print_r($output2);
print $output2;
?>
The goal of this script is to run a command on another networked server. If I run the above ssh
command (replacing the dummy data with real data) from the command line:
ssh someotherhost ls -l /path/to/dir
It outputs the proper ls lines. However, when I run the above script from the same dir with the same command, it does not output in any of the three bottom print lines. However, the exec()
with whoami
at the top does print out as expected. So my question is, why does the first command work and not the second?
Note that the two networked servers are on an internal network and are setup with ssh network key pairings. The command works, just not from within php.
Thanks for your help.
回答1:
PHP may be running the ssh command with a different user than you're doing it from the CLI. Maybe the user PHP is running it as doesn't have the servers key in its key file or something.
Personally, I would just use phpseclib, a pure PHP SSH implementation.
回答2:
I had to find a way to do this a while ago to make a custom control panel for an internal web development server and i looked around a lot and found that there is a SSH package for PHP and it usually comes with ssh in it. You might want to try it out :)
You will have to generate a key on your server to allow your server to connect to the target without password, to do that:
ssh-keygen -t rsa
ssh-copy-id root@targetmachine
Lookup the net for more information regarding RSA key generation, there are tons on the net. And then, just make a little function like this and you are ready to execute tons of commands :)
<?php
/**
*
* Runs several SSH2 commands on the devl server as root
*
*/
function ssh2Run(array $commands){
$connection = ssh2_connect('localhost');
$hostkey = ssh2_fingerprint($connection);
ssh2_auth_pubkey_file($connection, 'root', '/home/youruser/.ssh/id_rsa.pub', '/home/youruser/.ssh/id_rsa');
$log = array();
foreach($commands as $command){
// Run a command that will probably write to stderr (unless you have a folder named /hom)
$log[] = 'Sending command: '.$command;
$log[] = '--------------------------------------------------------';
$stream = ssh2_exec($connection, $command);
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
// Enable blocking for both streams
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
// Whichever of the two below commands is listed first will receive its appropriate output. The second command receives nothing
$log[] = 'Output of command:';
$log[] = stream_get_contents($stream);
$log[] = '--------------------------------------------------------';
$error = stream_get_contents($errorStream);
if(strlen($error) > 0){
$log[] = 'Error occured:';
$log[] = $error;
$log[] = '------------------------------------------------';
}
// Close the streams
fclose($errorStream);
fclose($stream);
}
//Return the log
return $log;
}
Also, you might be interrested at the docs for SSH2 for php: http://ca3.php.net/manual/fr/book.ssh2.php
来源:https://stackoverflow.com/questions/8671337/how-to-use-exec-in-php-in-combination-with-ssh-hostname-command