<file_sep><?php 
$action = $_REQUEST['action_to_do'];
if($action == 'shutdown'){
	$result = shell_exec("shutdown.sh");	
}

else if($action == 'reboot'){
    $result = shell_exec("reboot.sh");	
    
}

else{
	$result = "Please select an action";
}
var_dump($result);
exit;
header('Location: index.php?result='.$result);

?><file_sep><!DOCTYPE html>
<html lang="en">
<head>
  <title>Remote Server Management</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
 <body>
    <div class="container">
        
 	<?php
 	    if(isset($_GET['result'])){
            echo '<div class=""><pre>'.$_GET['result'].'</pre></div>';
        }

        echo "<table class='table table-bordered'><tr><th>Server Name</th><th>Address</th></tr>";
        $file_handle = fopen("lan-servers.txt", "r");
        while (!feof($file_handle)) {
            $line = fgets($file_handle);
            $data = explode(" ", $line);
            echo "<tr><td>".$data[0]."</td><td>".$data[count($data)-1]."</td></tr>";
        }
        fclose($file_handle);
 	?>
    </table>
    <h3 class="align-center">Select Action</h3>
    <form method="post" action="exec_shell.php"> 
    	<select name="action_to_do">
    		<option value="">Select</option>
    		<option value="shutdown">Shutdown</option>
    		<option value="reboot">Reboot</option>
    	</select>
        <INPUT TYPE="submit" VALUE="Submit" NAME="Submit"> 
    </form>
    </div>
 </body>
</html><file_sep>#!/bin/bash

# where the server list sits
LAN_LIST="$HOME/lan-servers.txt"

# Function to check if the server responds to ping
function testServer() {
    if ping -c1 $ip>/dev/null 2>&1; then 
	return 0
    else 
	return 1
    fi
}

# get a list of IPs for the servers
function getServers() {
    cat $LAN_LIST  | egrep -v '^(#|\s*$)'  | perl -ple 's/\s+/ /g;' | cut -d' ' -f 2
}


	# check if I can now log in to the server with no password
	if ! ssh -o "NumberOfPasswordPrompts 0" root@$ip '/bin/true' >/dev/null 2>&1; then
	    # if I still can't then something is broken
	    echo "Failed to complete key exchange with $ip - skipping this server"
	    continue;
	fi
    fi

    # start to shutdown the server
    echo -n "Rebooting $(ssh root@$ip hostname)"

    # halt the remote server
    ssh root@$ip reboot
    count=0
    # wait until its down or 300 seconds have passed
    while testServer $ip; do 
        sleep 1; echo -n "." 
        count=$(( $count + 1 )) # count up to 300
        if [ "$count" -gt 300 ]; then # wait till 300s then ping the server
    	    if  testServer $ip; then # if its alive then its already rebooted
                echo "Rebooting done $ip"
	       fi
	fi
    done
    echo ""
done

<file_sep>Remote server controller web interface.

********* File description *************
Index file: There is an index file (index.php) which calls the exec_shell.php file based on the selection of rebooting or shutdown. 

lan_servers.txt contains the server name and IP addresses seperated by space. There are two shell scripts one for shut down another for rebooting. I assumed, the server pc already have the super user priviledge to conduct shutdown and reboot operations.


********* How to launch *************

Put the folder in htdocs folder under xampp. Run the xampp server and then launch the index.php from browser. 

The idea is adopted from: https://geek.co.il/2009/07/20/script-day-shutting-down-multiple-servers-at-once


