在远程计算机上从php发出多个bash命令
我试图提出:在远程计算机上从php发出多个bash命令,php,bash,ubuntu,ssh,amazon-ec2,Php,Bash,Ubuntu,Ssh,Amazon Ec2,我试图提出: sudo apt-get update 及 在来自php的远程计算机上 我四处寻找,尝试了两种方法: 第一: 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
sudo apt-get update
及
在来自php的远程计算机上
我四处寻找,尝试了两种方法:
第一:
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);
如果我登录到计算机并发出
history
但似乎没有被执行,因为如果我
java
我收到消息说我需要安装它
如果我发布
!1
apt get update和apt get install执行正确
如果在浏览器中运行php文件,则会得到以下输出:
欢迎使用Ubuntu 14.04.1 LTS GNU/Linux 3.13.0-36-generic x86_64*文档:截至太阳的系统信息2015年2月8日17:20:45 UTC系统负载:0.69进程:104使用/:9.8%的7.74GB用户登录:0内存使用率:1%的IP地址eth0:10.74.190.178交换使用率:0%绘制此数据并在以下位置管理此系统:获取云支持使用Ubuntu Advantage Cloud Guest:可以更新0个软件包。0更新是安全更新。最后一次登录:Sun Feb 8 17:20:50 2015,来自ec2-54-77-56-210.eu-west-1.compute.amazonaws.com echo“[start]”;sudo易于获得更新;sudo-apt-get-install-y openjdk-7-jre-joe-wget;回音“[结束]”ubuntu@ip-10-74-190-178:~$echo'[start]';sudo易于获得更新;苏多容易进去
有很多空行,有时还有一条双重信息,比如
阿拉尔
或
l-l-
->请注意,该命令在“安装”时被截断
这可以解释为什么命令没有执行
谁能帮我一把吗?我已经找了差不多两个星期了,似乎进展不大
谢谢
Sander显然,php.ini中的最大执行时间是这里的问题 我调试了第二个脚本
...
}elseif(preg_match('/\[end\]/',$line)) {
echo "[end]:".$line;
return $output;
}elseif($start){
...
及
并且在浏览器的页面输出中都没有执行
这还不够,应采取以下措施:
set_time_limit(900);
执行了原始命令,但在执行时,php超时停止了执行
同样,第一个脚本也应该遇到同样的问题,只是执行命令和执行过程中返回的php花费了很长时间
我也是,但我不认为这是问题的原因使用了:
$stream = ssh2_exec($connection,$cmd);
代替原来的
fwrite($shell,$cmd . "\n");
整个脚本:
<?php
sleep(30);
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')
) {
$cmd = "echo '[start]';sudo apt-get update;sudo apt-get install -y openjdk-7-jre joe wget;echo '[end]'";
$output = user_exec($connection,$cmd);
fclose($shell);
ob_end_flush();
echo $output."Halleluia!!!";
} else {
die('Public Key Authentication Failed');
}
function user_exec($connection,$cmd) {
$stream = ssh2_exec($connection,$cmd);
$output = "";
$start = false;
$start_time = time();
set_time_limit(900);
$max_time = 900; //time in seconds
while(((time()-$start_time) < $max_time)) {
$line = fgets($stream);
if(!strstr($line,$cmd)) {
if(preg_match('/\[start\]/',$line)) {
$start = true;
}elseif(preg_match('/\[end\]/',$line)) {
echo "[end]:".$line;
return $output;
}elseif($start){
if($line != ""){
ob_flush();
flush();
$output[] = $line;
echo $line."<br/>";
}
}
}
}
echo "out of time";
}
?>
希望这能帮助任何有类似问题的人
美国
$max_time = 900; //time in seconds
set_time_limit(900);
$stream = ssh2_exec($connection,$cmd);
fwrite($shell,$cmd . "\n");
<?php
sleep(30);
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')
) {
$cmd = "echo '[start]';sudo apt-get update;sudo apt-get install -y openjdk-7-jre joe wget;echo '[end]'";
$output = user_exec($connection,$cmd);
fclose($shell);
ob_end_flush();
echo $output."Halleluia!!!";
} else {
die('Public Key Authentication Failed');
}
function user_exec($connection,$cmd) {
$stream = ssh2_exec($connection,$cmd);
$output = "";
$start = false;
$start_time = time();
set_time_limit(900);
$max_time = 900; //time in seconds
while(((time()-$start_time) < $max_time)) {
$line = fgets($stream);
if(!strstr($line,$cmd)) {
if(preg_match('/\[start\]/',$line)) {
$start = true;
}elseif(preg_match('/\[end\]/',$line)) {
echo "[end]:".$line;
return $output;
}elseif($start){
if($line != ""){
ob_flush();
flush();
$output[] = $line;
echo $line."<br/>";
}
}
}
}
echo "out of time";
}
?>