I try to issue:
sudo apt-get update
and
sudo apt-get install openjdk-7-jre joe wget
on a remote machine from php,
I've been looking around and tried two approaches:
first:
session_start();
$command = "ssh -o 'StrictHostKeyChecking no' -i /var/www/.ssh/my-keypair555.pem ubuntu@{$_SESSION['host']} \"sudo apt-get update\"";
echo $command."<br/>";
echo exec($command, $output);
print_r($output);
$command = "ssh -o 'StrictHostKeyChecking no' -i /var/www/.ssh/my-keypair555.pem ubuntu@{$_SESSION['host']} \"sudo apt-get install -y openjdk-7-jre joe wget\"";
echo $command."<br/>";
echo exec($command, $output);
print_r($output);
this code doesn't seem to get executed at all, (The commands get printed but the output array seems empty.)
and secondly: an adapted version from http://php.net/manual/en/function.ssh2-exec.php
session_start();
$connection = ssh2_connect($_SESSION['host'], 22, array('hostkey'=>'ssh-rsa'));
if (ssh2_auth_pubkey_file(
$connection,
'ubuntu',
'/var/www/.ssh/id_rsa.pub',
'/var/www/.ssh/id_rsa')
) {
$shell = ssh2_shell($connection,"bash");
$cmd = "echo '[start]';sudo apt-get update;sudo apt-get install -y openjdk-7-jre joe wget;echo '[end]'";
$output = user_exec($shell,$cmd);
fclose($shell);
echo $output."<br/>";
} else {
die('Public Key Authentication Failed');
}
function user_exec($shell,$cmd) {
fwrite($shell,$cmd . "
");
$output = "";
$start = false;
$start_time = time();
$max_time = 900; //time in seconds
while(((time()-$start_time) < $max_time)) {
$line = fgets($shell);
//echo $line;
if(!strstr($line,$cmd)) {
if(preg_match('/\[start\]/',$line)) {
$start = true;
echo "start";
}elseif(preg_match('/\[end\]/',$line)) {
return $output;
}elseif($start){
$output[] = $line;
echo $line."<br/>";
}
}
}
}
If I use this code the command:
echo '[start]';sudo apt-get update;sudo apt-get install -y openjdk-7-jre joe wget;echo '[end]'
does show up if I log in to the machine and issue
history
but doesn't seem to be executed because if I issue
java
I get the message that I need to install it.
if I issue
!1
the apt-get update and apt-get install do get executed properly.
If I run the php file in a browser I get following output:
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-36-generic x86_64) * Documentation: https://help.ubuntu.com/ System information as of Sun Feb 8 17:20:45 UTC 2015 System load: 0.69 Processes: 104 Usage of /: 9.8% of 7.74GB Users logged in: 0 Memory usage: 1% IP address for eth0: 10.74.190.178 Swap usage: 0% Graph this data and manage this system at: https://landscape.canonical.com/ Get cloud support with Ubuntu Advantage Cloud Guest: http://www.ubuntu.com/business/services/cloud 0 packages can be updated. 0 updates are security updates. Last login: Sun Feb 8 17:20:50 2015 from ec2-54-77-56-210.eu-west-1.compute.amazonaws.com echo '[start]';sudo apt-get update;sudo apt-get install -y openjdk-7-jre joe wget;echo '[end]' ubuntu@ip-10-74-190-178:~$ echo '[start]';sudo apt-get update;sudo apt-get in
- a lot of empty lines and now and then a double message like alal or l -l -
-> note that the command is truncated at 'install'
which would explain why the command isn't executed.
Can anyone give me a hand on this one, I've been searching for almost two weeks now and don't seem to progress a lot...
thanks,
Sander