2015-02-08 18 views
0

在PHP命令我嘗試問題:發出多個慶典從遠程計算機

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 [email protected]{$_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 [email protected]{$_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泛型x86_64的)*文檔:爲https://help.ubuntu.com/系統信息Sun Feb 8 17:20:45 UTC 2015系統負載:0.69進程:104 /的使用情況:7.74GB的9.8%登錄用戶:0內存使用情況:1%eth0的IP地址:10.74.190.178交換使用情況:0%此數據並在以下位置管理此係統:https://landscape.canonical.com/通過Ubuntu Advantage Cloud獲得雲支持來賓:http://www.ubuntu.com/business/services/cloud可以更新0個軟件包。 0更新是安全更新。上次登錄: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

  • 很多空行和現在,然後像 ALAL 或 升-l雙消息 -

- >請注意,命令在截斷 '安裝'

這將解釋爲什麼通信並沒有執行。

誰能給我一隻手就這一個,我一直在尋找近兩週,似乎沒有進步很多...

感謝,

桑德

回答

0

顯然,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 

是不夠的,應該與proceded:

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.