2013-04-11 47 views
-1

我有一個php代碼作爲服務器使用命令行運行。我使用telnet客戶端連接到服務器沒有問題。但我不希望客戶端是telnet,我希望它成爲一個網頁,以便來自世界各地的客戶端可以連接。如何將網頁連接到服務器?連接網頁到命令行服務器

+1

安裝Apache? – 2013-04-11 10:24:41

+0

我已經跟我 – 2013-04-11 10:28:28

+0

安裝WAMP能否請您解釋更好的細節問題則 – 2013-04-11 10:29:55

回答

2

爲了使用web界面連接到一個shell,你需要用PHP完整的Apache服務器,像燈/ WAMP

步驟:

  • 創建一個網絡接口發送到服務器的命令看到結果。 ajax UI可能對此有用。我會建議你知道html/php/javascript,這對你來說不是一個問題。
  • 的「高管」 PHP函數來調用命令: http://php.net/manual/es/function.exec.php

例子:

<?php 
// Test the user!! you don't want that any one may use the command line of your server. 
... 
// Get the command to execute 
$command=''; 
if (isset($_POST['command'])) 
{ 
    $command = $_POST['command']; 
} 
// Execute your command 
if ($command!='') echo exec($command); 
?> 

你應該考慮的錯誤發送到標準錯誤,所以如果你想看到的錯誤,它需要重定向: echo exec($ command。'2> & 1');

安全考慮:

  • 使用SSL的所有通信
  • 要非常小心,不要讓每一個來訪問你的shell,甚至用奇怪的代碼或網頁的修改。
  • 測試是否允許$命令,不要執行任何操作。
  • 請小心使用允許上傳文件的命令,例如「wget」。

其他注意事項: 這個腳本只允許執行簡單的指令,但不允許使用複雜的軟件全雙向通信,例如,你不能運行遊戲服務器,因爲這。 如果這是你的意圖,在Linux上有一個應用程序「屏幕」,允許重新連接到一個shell。

//Define a screen name, may be whatever, but must be unique 
$myScreenName='MyWebApp'; 

// To launch the app: 
echo exec('screen -S ' . $myScreenName . ' -d -m ' . $command . ' 2>&1 1>log.log'); 

// To re-connect to the shell in order to send new content: 
echo exec('screen -S ' . $myScreenName . " -X -p0 stuff $'" . $command . "\n'"); 

// to test if the screen is active: 
function testIfActive($myScreenName) 
{ 
    exec('screen -ls', $screenLS); 
    screenLs = implode('', $screenLS); 
    return (stripos($screenLs, $myScreenName)!==false); 
} 
// to read the output, just read the log.log file. 
+0

當我們已經有fsockopen來連接任何服務器套接字時,爲什麼需要做這麼多的exec。 – 2013-04-11 11:28:01

+0

@ kuldeep.kamboj fsockopen是連接到套接字,而不是執行命令。我認爲我不瞭解你。 – 2013-04-11 11:36:58

+0

是的你是對的,但是我的觀點是,對於artibitary服務器連接fsockopen是標準化的想法。您需要通過fwrite/fgets文件處理系列函數向其寫入/讀取數據。 – 2013-04-11 11:47:00