[英]PHP - How to control a Solaris server from php to run root commands to execute scripts?
Am a newbie in PHP. 是PHP的新手。
I wanted to have a dropdown list for specific solaris servers, in which when a server is selected from the drop down list, the server will execute a script on itself. 我想为特定的solaris服务器创建一个下拉列表,其中从下拉列表中选择服务器时,服务器将自行执行脚本。 How do I even do that?
我该怎么办?
<?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>
Any help is deeply appreciated. 任何帮助深表感谢。
First of all, important disclaimer :) 首先,重要的免责声明:)
Note, shell_exec
used in a backend is rather dangerous function, as it might pose security threats. 注意,后端中使用的
shell_exec
是相当危险的功能,因为它可能构成安全威胁。 Isolate shell_exec
from user input as much as possible and apply strict security policies 尽可能将
shell_exec
与用户输入隔离,并应用严格的安全策略
You have to implement two "artifacts": 您必须实现两个“工件”:
On each server you need a backend file that execute desired script. 在每台服务器上,您需要一个执行所需脚本的后端文件。 This file should be accessible over the network.
该文件应该可以通过网络访问。 Lets say it's accessible via url
http://*solarisservername*/justdoit
It might be something like 可以说它可以通过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);
On your page you need to implement quering the URL we set up in a previous step. 在您的页面上,您需要实施查询我们在上一步中设置的URL。 You have two options: using
cURL
and using AJAX
. 您有两个选择:使用
cURL
和使用AJAX
。 I'll describe AJAX
method with jQuery
as it is much simpler and fancy :). 我将用
jQuery
描述AJAX
方法,因为它更加简单和有趣:)。 Note, it's Javascript 注意,这是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>
add this to your report.php 将此添加到您的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.