I use the below to sent commands to multiple Linux computers through a web interface. If I select 30 or 40 clients, I have to wait for the command to run on all of them, before I get any results. Is there a way to start listing the results, as soon as the command is run on each individual computer?
<?php
include('Net/SSH2.php');
$user = "user";
$pass = "pass";
$ip = $_POST['clients'];
$ssh = new Net_SSH2($ip);
$cmds = $_POST['commands'];
if(!empty($_POST['clients'])) {
foreach($_POST['clients'] as $ip) {
$ssh = new Net_SSH2($ip);
if (!$ssh->login($user, $pass)) {exit('Login Failed');}
echo "$ip: " . $ssh->exec($cmds) . "<br>";
}
}
?>
<form action="phpseclib/commands.php" method="post" target="main">
<input type="checkbox" name="clients[]" value="192.168.0.51">Client001<br>
<input type="checkbox" name="clients[]" value="192.168.0.52">Client002<br>
<input type="checkbox" name="clients[]" value="192.168.0.53">Client003<br>
<input type="checkbox" name="clients[]" value="192.168.0.54">Client004<br>
<input type="checkbox" name="clients[]" value="192.168.0.55">Client005<br>
<br/>
<select name="commands">
<option value="">Select Command</option>
<option value="uptime | sed 's/.*up \([^,]*\), .*/\1/'">Uptime</option>
<option value="printf $(free | grep -e-/+ | awk '{print $3/($3+$4) * 100.0 ''}' | cut -d '.' -f1)%%">Memory Usage</option>
</select>
<br>
<br/>
<input type="submit" value="Submit" />
</form>
</div>
You could use AJAX requests instead of relying on POST data from a form. Each time a client is selected an asynchronous request can be sent and updates included in the browser.
Quick side note on security, are you allowing injection of shell commands from the browser? If the contents of those input
option values are executed without question then a user can put anything in there.