簡體   English   中英

PHP-如何從PHP控制Solaris服務器以運行根命令來執行腳本?

[英]PHP - How to control a Solaris server from php to run root commands to execute scripts?

是PHP的新手。

我想為特定的solaris服務器創建一個下拉列表,其中從下拉列表中選擇服務器時,服務器將自行執行腳本。 我該怎么辦?

 <?php

session_start();

echo ("Select a Solaris System to audit");

?>

<form action="report.php" method="POST">

 <div>
 <select name="options">
      <option value="1">Solaris 001</option>
      <option value="2">Solaris 002</option>
      <option value="3">Solaris 003</option>
 </select>
 <br>
 <br>

 <input type="submit" value="Audit"/>

 </div>
</form>

任何幫助深表感謝。

首先,重要的免責聲明:)

注意,后端中使用的shell_exec是相當危險的功能,因為它可能構成安全威脅。 盡可能將shell_exec與用戶輸入隔離,並應用嚴格的安全策略

您必須實現兩個“工件”:

  1. 在每台服務器上,您需要一個執行所需腳本的后端文件。 該文件應該可以通過網絡訪問。 可以說它可以通過url http://*solarisservername*/justdoit它可能類似於

     //Check for authentication, if authenticated set $authenticated = true if ($authenticated){ $script_response = shell_exec("<script to execute>"); } //if you need, you may return result to the calling script //I'm using json format, but you may want to use plain text, XML or whatever. $result = array("script_response"=>$script_responce,"success"=>true); echo json_encode($result); 
  2. 在您的頁面上,您需要實施查詢我們在上一步中設置的URL。 您有兩個選擇:使用cURL和使用AJAX 我將用jQuery描述AJAX方法,因為它更加簡單和有趣:)。 注意,這是Javascript

     <script type='text/javascript'> $(document).ready(function(){ $("#submit").click(function(){ var url = $("select[name='options']").val(); url = url + "justdoit"; //url now contains http://*solarisservername*/justdoit $.ajax({ type:post, dataType: 'json' url: url, success: function(data,textStatus){ //process your response here //access script response as data.script_response. } }); }); }); </script> <!--you dont't need method and action here, as we don't even post the form--> <form> <div> <select name="options"> <option value="http://solarisserver1">Solaris 001</option> <option value="http://solarisserver2">Solaris 002</option> <option value="http://solarisserver3">Solaris 003</option> </select> <br> <br> <!--onclick='return false' prevents form from submitting--> <input type="submit" id='submit' onclick='return false' value="Audit"/> </div> </form> 

將此添加到您的report.php

<?php

if (isset($_POST['options'])){
   // you can get value now
   $server = $_POST['options'];

   echo $server; // will print the current selected value
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM