wake on lan and terminal management page

This code was written to get a management interface for my own servers. But as I switched to another option the code was never in productive state.

<?php ############# HEAD  ############################
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n";
echo "<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n";
############# docu ################################
/* create user account on target machine for shutdown (shutdownuser)
 * enable shutdown command for user on target machine (sudoers)
 * copy public key file for HTTP Server account to target machines .ssh/authorized_keys
 */
############# style sheets ########################
echo "<title>Terminal Server</title>\n<link rel='stylesheet' type='text/css' href='terminal.css'></head>\n<body>\n\n";
############# Configuration ##########################
/*
 * start server via relais
 * link zu shell in box fuer direkten connect
 */
$mac_adress_file = "terminal-list";# path where mac adresses are listed, must be accessible on webserver machine with webserver user rights
$server_port_to_check=22;# port to check for availability
$domain = ".steppenwolf.de";# domain all server reside in
$table_headers = array("Check", "Server (Terminal)", "Start", "Stop", "Comment");
$redirect_header = "/bananas";# if we were redirected we append level to URI
$shutdown_command = "ls ";
$shutdown_user = "shutdownuser";# user for shutdown command
$shutdown_public_keyfile = "id_rsa.pub"; # public_keyfile, must be accessible on webserver machine with webserver user rights
$Shutdown_private_keyfile = "id_rsa"; # private_keyfile, must be accessible on webserver machine with webserver user rights
###### links to shell in a box #################################
/*
 * shellinaboxd -t -s /:LOGIN -s /who:nobody:nogroup:/:w
 * In addition to the login shell at http://localhost:4200, show a list of currently logged in users when accessing http://localhost:4200/who. This command must be run as root in order to be able to change to nobody:nogroup as requested by the service description
 * shellinaboxd -t -s '/:root:root:/:wy60 -c /bin/login'
 * Instead of the standard ANSI/VT100 terminal, publish a Wyse 60 terminal. Again, this command should be run as root.
 * shellinaboxd -s /:SSH:example.org

    The terminal connects to a ssh session on example.org. 

shellinaboxd -t -s /:AUTH:HOME:/bin/bash

    Interactively request the user's name and password prior to launching a Bourne shell. This command can be run by unprivileged users. But if doing so, it only allows this particular user to log in. 
    
    oder spezieller Nutzer mit PRG als shell
*/		
###### parse contents of configfile and store everything in two arrays
function parse_file_contents($filename){
	global $inputvalues;
	$return = true;
	if ($data = file_get_contents($filename)){
		$contents = explode("\n", $data);
		foreach ($contents as $line) { # parse each line of the input file seperately
			$line = trim($line); $value = ""; # and override default values with config file values
			if (strlen($line) && substr($line, 0, 1) != '#') { # no comment lines and only lines with contents are parsed
				$key = trim(strtok($line, " \t")); # name of the specific option is first parameter
				$inputvalues["values"][$key] = trim(strtok(" \t")); # second entry is value for option
				while ($inputpart = strtok(" \t")) $value = $value.trim($inputpart)."\t"; # rest of the line is combined as comment
					$inputvalues["comment"][$key] = trim($value);
			} # comment lines and empty lines are ignored
		}# parsing of all lines done
	}# could not get file contents via function so die
	else {
		$return = false;
	}
	return $return;
}
###### check availability of server
function check_vitality($server, $timeout = 2){# ICMP ping packet with a pre-calculated checksum
return "1 OK toki";
	global $server_port_to_check;
	$contents = "";
	$fp = fsockopen($server, $server_port_to_check, $errno, $errstr, $timeout);
	if (!$fp) {
		$return = "0"."$errstr ($errno)";
	} else {
		stream_set_timeout($fp, $timeout);
	    $contents = $contents.fgets($fp, 128);
	    fclose($fp);
	    $return = "1".$contents;
	}
	return $return;
}
###### start server via GPIO
function start_server($server){
	$return = "Alles gut";
	return $return;
}
###### shutdown server
function shutdown_server($server){
	global $shutdown_command, $shutdown_user, $shutdown_public_keyfile, $Shutdown_private_keyfile;
	$connection = ssh2_connect($server, 22, array('hostkey', 'ssh-rsa'));
	if (ssh2_auth_pubkey_file($connection, $shutdown_user,$shutdown_public_keyfile, $Shutdown_private_keyfile)) {
		$return = "Public Key Authentication Successful\n<br>Shutting down server<br>\n";
		$stream = ssh2_exec($connection, $shutdown_command);
		$return = $return."<br>done ...<br>\n";
		fclose($stream);
	} else {
		$return = "Public Key Authentication Failed";
	}
	return $return;
}
###### edit terminal list
function edit_conf_file() {
	global $inputvalues;
	echo "<table border=1 width=700>\n"; # main table with all server names listed
	echo "<tr>\n<td colspan=3 class=header-style><br> Edit terminal list <br>&nbsp</td></tr>\n";
	echo "<tr>\n<td class=subheader-style> Server </td><td class=subheader-style> Terminal </td><td class=subheader-style> Comment </td></tr>\n";
	$counter = 0;
	foreach ($inputvalues["values"] as $key => $value){
		$counter ++;
		echo "<tr><td><input type='input' value='".$key."' name='server".$counter."'></td>";
		echo "<td><input type='input' value='".$value."' name='tty".$counter."'></td>";
		echo "<td  class=Comment-style><input class=Comment-style type='input' size=50 value='".$inputvalues['comment'][$key]."' name='comment".$counter."'></td></tr> ";
	}
	echo "<tr>\n<td colspan=2 align=center><input type='submit' name='CANCEL' value='CANCEL'></td><td align=center><input class=attention-style type='submit' name='SAVE' value='SAVE'></td></tr>\n";
	echo "<input type='hidden' name='counter' value='".$counter."'>\n";
	return;
}
###### save values to file
function save_conf_file($filename){
	$ergebnis = false;
	$output = "";
	for ($i=1;$i <= $_GET['counter']; $i++){# number of entries is stored in variable
		$output = $output.$_GET['server'.$i]."\t".$_GET['tty'.$i]."\t".$_GET['comment'.$i]."\n";
	}
	if ($handle = fopen($filename, "w")){ # open configfile for write
		fwrite($handle, $output);
		fclose($handle);
		$ergebnis = true;
	}
	else { # can not open output file
		$ergebnis = false;
	}
	return $ergebnis;
}
###### debug output of POST
function debug_output(){
	echo "<pre><br><h1>Hier kommt der schamass!</h1>";
	print_r($_GET);
	echo "</pre>"; return ;
}
############################## MAIN ##################################
if (isset($_SERVER['HTTP_X_FORWARDED_SERVER'])){ # if redirected, we use a different URI
	$action_script = $redirect_header.htmlspecialchars($_SERVER['PHP_SELF']);
}else {
	$action_script = htmlspecialchars($_SERVER['PHP_SELF']);
}
echo "<form method='get' action='".$action_script."'>\n";# page header with headline and table headers
if ( !parse_file_contents($mac_adress_file)) { # get contents of external file
	echo "<h1>Error reading config file!!</h1>";
} else {
	echo "<table border=1 width=700>\n"; # main table with all server names listed
	echo "<tr><td colspan=4 class=header-style><br> mini Server Admin Page <br>&nbsp</td><td align='center'><input class=attention-style type='submit' name='EDIT' value='EDIT'></td></tr>\n";
	echo "<tr>";
	foreach ($table_headers as $header)
		echo "<th class='".$header."-style' > ".$header."</th>";
	echo "</tr>\n";
	foreach ($inputvalues["values"] as $key => $value){# iterate over complete list of items 
		$title_text = check_vitality($key); # text to display on mouseover for button
		if (substr($title_text, 0, 1) == "1") 
			$text_color = "green";
		else 	
			$text_color = "red";
		echo "<tr><td align='center'><input type='submit' title='".substr($title_text, 1)."' name='CHECK' value='".$key."'></td>";
		echo "<td align='left' bgcolor='".$text_color."'> <a href='".$inputvalues['values'][$key]."' > ".$key.$domain." </td>";# link to terminal
		echo "<td class=Start-style><input type='submit' name='START' value='".$key."'></td>";# start server
		echo "<td align='center' class=Stop-style><input type='submit' name='STOP' value='".$key."'></td>";# stop server
		echo "<td class=Comment-style> ".$inputvalues['comment'][$key]." </td></tr>\n";# add comments field
	}
	echo "<tr><td colspan=5></td></tr>\n";
	echo "<tr><td colspan=5 align='center'><input type='submit' value='REFRESH' name='REFRESH'></td></tr>\n</table>\n";# just refresh the screen
	if (isset($_GET['STOP']) && array_key_exists($_GET['STOP'], $inputvalues['values'])){############ shutdown system ########################
		$server = $_GET['STOP'];
		echo "<h2>Stop Server: ".$server." </h2>\n<br><br>\n";
		echo shutdown_server($server);
	}
	elseif (isset($_GET['START']) && array_key_exists($_GET['START'], $inputvalues['values'])){############ wake up system ###################
		$server = $_GET['START'];
		echo "<h2>Wake Up System: ".$server."</h2>\n<br><br>\n";
		echo start_server($server);
	}
	elseif (isset($_GET['CHECK']) && array_key_exists($_GET['CHECK'], $inputvalues['values'])){############ get response on connectiontrial ###################
		$server = $_GET['CHECK'];
		echo "<h2>System returns: </h2>\n<br><br>".substr(check_vitality($server), 1)."\n<br><br>\n";
	}
	elseif (isset($_GET['EDIT'])) { ########### edit list of terminals #######################
		echo "<h2>Edit Terminallist</h2>\n";
		edit_conf_file();
	}
	elseif (isset($_GET['SAVE'])) { ########### save new entries to conf file ################
		echo "<h2>Save values to conf</h2>";
		if (save_conf_file($mac_adress_file)) echo "Updating Conf File successful (Choose REFRESH to see changes)!";
		else echo "Error during updating!";
	}
	elseif (!isset($_GET['REFRESH']) && !empty($_GET) && !isset($_GET['CANCEL'])) echo "<h1>Wrong Server choosen</h1>";############ user tries to cheat ################
	}
#print_r($inputvalues);
#debug_output();
echo "</form>\n</Body></HTML>\n";
########### END ###################################?>
@CHARSET "ISO-8859-1";
barbox_a {
  position: absolute;
  top: 330px;
  left: 250px;
  margin: 0px 0px 0px -160px;
  width: 304px;
  height: 24px;
  background-color: black;
}
.per {
  position: absolute;
  top: 330px;
  font-size: 18px;
  left: 250px;
  margin: 1px 0px 0px 150px;
  background-color: #FFFFFF;
}

.bar {
  position: absolute;
  top: 332px;
  left: 250px;
  margin: 0px 0px 0px -158px;
  width: 0px;
  height: 20px;
  background-color: #777777;
  border-style: solid;
  border-width: 1px;
}

.blank {
  background-color: white;
  width: 300px;
  border-style: solid;
  border-width: 1px;
}
.Stop-style {
	color: lightgray;
	background-color: firebrick;
	
}
.header-style {
	font-size: 20px;
}
.Start-style {
	text-align: center;
	background-color: blue;
	color: white;
}
.Comment-style {
	color: gray;
	background-color: lightgray;
}
a {
	color: lightgray;
}
.attention-style {
	background-color: red;
	color: white;
}
.subheader-style {
	color: white;
	background-color: dimgray;
}

Here is an example of the external file which lists the servers and its interfaces.


backup ttyUSB0 Backup Server
bananas ttyUSB1 Nas Server on Banana Pi
toradex ttyUSB2 Certification Authority on Toradex
raspberrypi ttyUSB3 Raspberry Pi for trials
cubietruck ttyUSB4 Cubie Main Server

Leave a Reply