Hello everyone,
after some testing and considering that I'm new with java
to solve my problem I have proceeded as follows:
MANGO->proxy->agent
a) I created a html control with a button that makes a local call to a proxy java (proxyProcess) by ajax
<script language="javascript">
<!--
var myRequest = null;
function CreateXmlHttpReq( handler)
{
alert("create XmllHttpReq");
var xmlhttp = null;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = handler;
return xmlhttp;
}
function myHandler()
{
if (myRequest.readyState == 4 && myRequest.status == 200)
{
alert(myRequest.responseText);
}
}
function sendRequest( operation)
{
var path = '?process=snmpd&command=' + operation;
var urlProxy = 'http://xxx.xxx.xxx.xxx:8080/proxyPROCESS' + path;
alert("sendRequest to proxy \n" + urlProxy);
try
{
myRequest = CreateXmlHttpReq(myHandler);
myRequest.open( 'GET', urlProxy, true);
myRequest.send( null);
}
catch(e)
{
alert(e.message);
}
}
-->
</script>
<input type="button" value="start " onclick="sendRequest('start')" />
b) I created a servlet (positioned between the servlet Mango) proxyProcess.class that received the request makes a remote call (http://yyy.yyy.yyy.yyy/provaXMango/PROCESSI/processCommands.php?process = "+ param1 + "& command =" + param2) to the server where I want to execute the command
public class proxyPROCESS extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
String param1 = request.getParameter("process");
String param2 = request.getParameter("command");
URL url = new URL("http://yyy.yyy.yyy.yyy/provaXMango/PROCESSI/processCommands.php?process=" + param1 + "&command=" + param2);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
int code = conn.getResponseCode();
//conn.disconnect();
}
catch(MalformedURLException ex)
{
ex.printStackTrace();
}
catch(IOException ioex)
{
ioex.printStackTrace();
}
}
}
c) I created an php agent on the remote server to run command
<?php
$availableProcess = array();
getProcess( $availableProcess);
$qs = $_SERVER['QUERY_STRING'];
$process = $_GET['process'];
$command = $_GET['command'];
//comando to execute
$completeCommand = "sudo /etc/init.d/" . $process . " " . $command;
$tmp = exec( $completeCommand, $outputs, $ret_var);
?>
To execute commands with www user with sudo, you need to enable the www user to run the desired command in sudoers, for example
apache yyy.yyy.yyy.yyy=NOPASSWD: /etc/init.d/snmpd
Everything works correctly!
Probably this is not the optimal solution, but it works!
Ciao