从远程计算机上的 php 发出多个 bash 命令

Issuing multiple bash commands from php on remote machine

我尝试发布:

sudo apt-get update

sudo apt-get install openjdk-7-jre joe wget

在来自 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);

这段代码似乎根本没有执行, (命令已打印,但输出数组似乎为空。)

其次: 来自 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 . "\n"); 
    $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/>";
            } 
        } 
    } 
}   

如果我使用此代码命令:

echo '[start]';sudo apt-get update;sudo apt-get install -y openjdk-7-jre joe wget;echo '[end]'

如果我登录到机器并发出

就会显示
history

但似乎没有执行,因为如果我发出

java

我收到消息说我需要安装它。

如果我发出

!1

apt-get 更新和 apt-get 安装正确执行。

如果我在浏览器中 运行 php 文件,我得到以下输出:

欢迎使用 Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-36-generic x86_64) * 文档: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 个包可以更新。 0 更新是安全更新。上次登录:Sun Feb 8 17:20:50 2015 来自 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

-> 注意命令 t运行 位于 'install'

这可以解释为什么命令没有被执行。

任何人都可以帮助我解决这个问题,我已经搜索了将近两个星期了,但似乎没有太大进展...

谢谢,

桑德

显然 php.ini 中的 max_execution_time 是这里的问题。

我用

调试了第二个脚本
...
}elseif(preg_match('/\[end\]/',$line)) { 
    echo "[end]:".$line;
    return $output; 
}elseif($start){ 
...

    } //while loop
    echo "out of time";
}//function user_exec($shell,$cmd) { 

并且都没有在浏览器页面的输出中执行。

$max_time = 900; //time in seconds 

不够,应该继续:

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";
}   
?>

希望这对遇到类似问题的人有所帮助。

S.