{"id":451,"date":"2015-10-11T13:43:32","date_gmt":"2015-10-11T11:43:32","guid":{"rendered":"https:\/\/olkn.myvnc.com\/?p=451"},"modified":"2015-10-11T13:45:18","modified_gmt":"2015-10-11T11:45:18","slug":"wake-on-lan-and-terminal-management-page","status":"publish","type":"post","link":"https:\/\/olkn.myvnc.com\/?p=451","title":{"rendered":"wake on lan and terminal management page"},"content":{"rendered":"<p>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.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php ############# HEAD  ############################\r\necho &quot;&lt;!DOCTYPE html PUBLIC \\&quot;-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\\&quot; \\&quot;http:\/\/www.w3.org\/TR\/html4\/loose.dtd\\&quot;&gt;\\n&quot;;\r\necho &quot;&lt;html&gt;\\n&lt;head&gt;\\n&lt;meta http-equiv=\\&quot;Content-Type\\&quot; content=\\&quot;text\/html; charset=UTF-8\\&quot;&gt;\\n&quot;;\r\n############# docu ################################\r\n\/* create user account on target machine for shutdown (shutdownuser)\r\n * enable shutdown command for user on target machine (sudoers)\r\n * copy public key file for HTTP Server account to target machines .ssh\/authorized_keys\r\n *\/\r\n############# style sheets ########################\r\necho &quot;&lt;title&gt;Terminal Server&lt;\/title&gt;\\n&lt;link rel='stylesheet' type='text\/css' href='terminal.css'&gt;&lt;\/head&gt;\\n&lt;body&gt;\\n\\n&quot;;\r\n############# Configuration ##########################\r\n\/*\r\n * start server via relais\r\n * link zu shell in box fuer direkten connect\r\n *\/\r\n$mac_adress_file = &quot;terminal-list&quot;;# path where mac adresses are listed, must be accessible on webserver machine with webserver user rights\r\n$server_port_to_check=22;# port to check for availability\r\n$domain = &quot;.steppenwolf.de&quot;;# domain all server reside in\r\n$table_headers = array(&quot;Check&quot;, &quot;Server (Terminal)&quot;, &quot;Start&quot;, &quot;Stop&quot;, &quot;Comment&quot;);\r\n$redirect_header = &quot;\/bananas&quot;;# if we were redirected we append level to URI\r\n$shutdown_command = &quot;ls &quot;;\r\n$shutdown_user = &quot;shutdownuser&quot;;# user for shutdown command\r\n$shutdown_public_keyfile = &quot;id_rsa.pub&quot;; # public_keyfile, must be accessible on webserver machine with webserver user rights\r\n$Shutdown_private_keyfile = &quot;id_rsa&quot;; # private_keyfile, must be accessible on webserver machine with webserver user rights\r\n###### links to shell in a box #################################\r\n\/*\r\n * shellinaboxd -t -s \/:LOGIN -s \/who:nobody:nogroup:\/:w\r\n * 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\r\n * shellinaboxd -t -s '\/:root:root:\/:wy60 -c \/bin\/login'\r\n * Instead of the standard ANSI\/VT100 terminal, publish a Wyse 60 terminal. Again, this command should be run as root.\r\n * shellinaboxd -s \/:SSH:example.org\r\n\r\n    The terminal connects to a ssh session on example.org. \r\n\r\nshellinaboxd -t -s \/:AUTH:HOME:\/bin\/bash\r\n\r\n    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. \r\n    \r\n    oder spezieller Nutzer mit PRG als shell\r\n*\/\t\t\r\n###### parse contents of configfile and store everything in two arrays\r\nfunction parse_file_contents($filename){\r\n\tglobal $inputvalues;\r\n\t$return = true;\r\n\tif ($data = file_get_contents($filename)){\r\n\t\t$contents = explode(&quot;\\n&quot;, $data);\r\n\t\tforeach ($contents as $line) { # parse each line of the input file seperately\r\n\t\t\t$line = trim($line); $value = &quot;&quot;; # and override default values with config file values\r\n\t\t\tif (strlen($line) &amp;&amp; substr($line, 0, 1) != '#') { # no comment lines and only lines with contents are parsed\r\n\t\t\t\t$key = trim(strtok($line, &quot; \\t&quot;)); # name of the specific option is first parameter\r\n\t\t\t\t$inputvalues&#x5B;&quot;values&quot;]&#x5B;$key] = trim(strtok(&quot; \\t&quot;)); # second entry is value for option\r\n\t\t\t\twhile ($inputpart = strtok(&quot; \\t&quot;)) $value = $value.trim($inputpart).&quot;\\t&quot;; # rest of the line is combined as comment\r\n\t\t\t\t\t$inputvalues&#x5B;&quot;comment&quot;]&#x5B;$key] = trim($value);\r\n\t\t\t} # comment lines and empty lines are ignored\r\n\t\t}# parsing of all lines done\r\n\t}# could not get file contents via function so die\r\n\telse {\r\n\t\t$return = false;\r\n\t}\r\n\treturn $return;\r\n}\r\n###### check availability of server\r\nfunction check_vitality($server, $timeout = 2){# ICMP ping packet with a pre-calculated checksum\r\nreturn &quot;1 OK toki&quot;;\r\n\tglobal $server_port_to_check;\r\n\t$contents = &quot;&quot;;\r\n\t$fp = fsockopen($server, $server_port_to_check, $errno, $errstr, $timeout);\r\n\tif (!$fp) {\r\n\t\t$return = &quot;0&quot;.&quot;$errstr ($errno)&quot;;\r\n\t} else {\r\n\t\tstream_set_timeout($fp, $timeout);\r\n\t    $contents = $contents.fgets($fp, 128);\r\n\t    fclose($fp);\r\n\t    $return = &quot;1&quot;.$contents;\r\n\t}\r\n\treturn $return;\r\n}\r\n###### start server via GPIO\r\nfunction start_server($server){\r\n\t$return = &quot;Alles gut&quot;;\r\n\treturn $return;\r\n}\r\n###### shutdown server\r\nfunction shutdown_server($server){\r\n\tglobal $shutdown_command, $shutdown_user, $shutdown_public_keyfile, $Shutdown_private_keyfile;\r\n\t$connection = ssh2_connect($server, 22, array('hostkey', 'ssh-rsa'));\r\n\tif (ssh2_auth_pubkey_file($connection, $shutdown_user,$shutdown_public_keyfile, $Shutdown_private_keyfile)) {\r\n\t\t$return = &quot;Public Key Authentication Successful\\n&lt;br&gt;Shutting down server&lt;br&gt;\\n&quot;;\r\n\t\t$stream = ssh2_exec($connection, $shutdown_command);\r\n\t\t$return = $return.&quot;&lt;br&gt;done ...&lt;br&gt;\\n&quot;;\r\n\t\tfclose($stream);\r\n\t} else {\r\n\t\t$return = &quot;Public Key Authentication Failed&quot;;\r\n\t}\r\n\treturn $return;\r\n}\r\n###### edit terminal list\r\nfunction edit_conf_file() {\r\n\tglobal $inputvalues;\r\n\techo &quot;&lt;table border=1 width=700&gt;\\n&quot;; # main table with all server names listed\r\n\techo &quot;&lt;tr&gt;\\n&lt;td colspan=3 class=header-style&gt;&lt;br&gt; Edit terminal list &lt;br&gt;&amp;nbsp&lt;\/td&gt;&lt;\/tr&gt;\\n&quot;;\r\n\techo &quot;&lt;tr&gt;\\n&lt;td class=subheader-style&gt; Server &lt;\/td&gt;&lt;td class=subheader-style&gt; Terminal &lt;\/td&gt;&lt;td class=subheader-style&gt; Comment &lt;\/td&gt;&lt;\/tr&gt;\\n&quot;;\r\n\t$counter = 0;\r\n\tforeach ($inputvalues&#x5B;&quot;values&quot;] as $key =&gt; $value){\r\n\t\t$counter ++;\r\n\t\techo &quot;&lt;tr&gt;&lt;td&gt;&lt;input type='input' value='&quot;.$key.&quot;' name='server&quot;.$counter.&quot;'&gt;&lt;\/td&gt;&quot;;\r\n\t\techo &quot;&lt;td&gt;&lt;input type='input' value='&quot;.$value.&quot;' name='tty&quot;.$counter.&quot;'&gt;&lt;\/td&gt;&quot;;\r\n\t\techo &quot;&lt;td  class=Comment-style&gt;&lt;input class=Comment-style type='input' size=50 value='&quot;.$inputvalues&#x5B;'comment']&#x5B;$key].&quot;' name='comment&quot;.$counter.&quot;'&gt;&lt;\/td&gt;&lt;\/tr&gt; &quot;;\r\n\t}\r\n\techo &quot;&lt;tr&gt;\\n&lt;td colspan=2 align=center&gt;&lt;input type='submit' name='CANCEL' value='CANCEL'&gt;&lt;\/td&gt;&lt;td align=center&gt;&lt;input class=attention-style type='submit' name='SAVE' value='SAVE'&gt;&lt;\/td&gt;&lt;\/tr&gt;\\n&quot;;\r\n\techo &quot;&lt;input type='hidden' name='counter' value='&quot;.$counter.&quot;'&gt;\\n&quot;;\r\n\treturn;\r\n}\r\n###### save values to file\r\nfunction save_conf_file($filename){\r\n\t$ergebnis = false;\r\n\t$output = &quot;&quot;;\r\n\tfor ($i=1;$i &lt;= $_GET&#x5B;'counter']; $i++){# number of entries is stored in variable\r\n\t\t$output = $output.$_GET&#x5B;'server'.$i].&quot;\\t&quot;.$_GET&#x5B;'tty'.$i].&quot;\\t&quot;.$_GET&#x5B;'comment'.$i].&quot;\\n&quot;;\r\n\t}\r\n\tif ($handle = fopen($filename, &quot;w&quot;)){ # open configfile for write\r\n\t\tfwrite($handle, $output);\r\n\t\tfclose($handle);\r\n\t\t$ergebnis = true;\r\n\t}\r\n\telse { # can not open output file\r\n\t\t$ergebnis = false;\r\n\t}\r\n\treturn $ergebnis;\r\n}\r\n###### debug output of POST\r\nfunction debug_output(){\r\n\techo &quot;&lt;pre&gt;&lt;br&gt;&lt;h1&gt;Hier kommt der schamass!&lt;\/h1&gt;&quot;;\r\n\tprint_r($_GET);\r\n\techo &quot;&lt;\/pre&gt;&quot;; return ;\r\n}\r\n############################## MAIN ##################################\r\nif (isset($_SERVER&#x5B;'HTTP_X_FORWARDED_SERVER'])){ # if redirected, we use a different URI\r\n\t$action_script = $redirect_header.htmlspecialchars($_SERVER&#x5B;'PHP_SELF']);\r\n}else {\r\n\t$action_script = htmlspecialchars($_SERVER&#x5B;'PHP_SELF']);\r\n}\r\necho &quot;&lt;form method='get' action='&quot;.$action_script.&quot;'&gt;\\n&quot;;# page header with headline and table headers\r\nif ( !parse_file_contents($mac_adress_file)) { # get contents of external file\r\n\techo &quot;&lt;h1&gt;Error reading config file!!&lt;\/h1&gt;&quot;;\r\n} else {\r\n\techo &quot;&lt;table border=1 width=700&gt;\\n&quot;; # main table with all server names listed\r\n\techo &quot;&lt;tr&gt;&lt;td colspan=4 class=header-style&gt;&lt;br&gt; mini Server Admin Page &lt;br&gt;&amp;nbsp&lt;\/td&gt;&lt;td align='center'&gt;&lt;input class=attention-style type='submit' name='EDIT' value='EDIT'&gt;&lt;\/td&gt;&lt;\/tr&gt;\\n&quot;;\r\n\techo &quot;&lt;tr&gt;&quot;;\r\n\tforeach ($table_headers as $header)\r\n\t\techo &quot;&lt;th class='&quot;.$header.&quot;-style' &gt; &quot;.$header.&quot;&lt;\/th&gt;&quot;;\r\n\techo &quot;&lt;\/tr&gt;\\n&quot;;\r\n\tforeach ($inputvalues&#x5B;&quot;values&quot;] as $key =&gt; $value){# iterate over complete list of items \r\n\t\t$title_text = check_vitality($key); # text to display on mouseover for button\r\n\t\tif (substr($title_text, 0, 1) == &quot;1&quot;) \r\n\t\t\t$text_color = &quot;green&quot;;\r\n\t\telse \t\r\n\t\t\t$text_color = &quot;red&quot;;\r\n\t\techo &quot;&lt;tr&gt;&lt;td align='center'&gt;&lt;input type='submit' title='&quot;.substr($title_text, 1).&quot;' name='CHECK' value='&quot;.$key.&quot;'&gt;&lt;\/td&gt;&quot;;\r\n\t\techo &quot;&lt;td align='left' bgcolor='&quot;.$text_color.&quot;'&gt; &lt;a href='&quot;.$inputvalues&#x5B;'values']&#x5B;$key].&quot;' &gt; &quot;.$key.$domain.&quot; &lt;\/td&gt;&quot;;# link to terminal\r\n\t\techo &quot;&lt;td class=Start-style&gt;&lt;input type='submit' name='START' value='&quot;.$key.&quot;'&gt;&lt;\/td&gt;&quot;;# start server\r\n\t\techo &quot;&lt;td align='center' class=Stop-style&gt;&lt;input type='submit' name='STOP' value='&quot;.$key.&quot;'&gt;&lt;\/td&gt;&quot;;# stop server\r\n\t\techo &quot;&lt;td class=Comment-style&gt; &quot;.$inputvalues&#x5B;'comment']&#x5B;$key].&quot; &lt;\/td&gt;&lt;\/tr&gt;\\n&quot;;# add comments field\r\n\t}\r\n\techo &quot;&lt;tr&gt;&lt;td colspan=5&gt;&lt;\/td&gt;&lt;\/tr&gt;\\n&quot;;\r\n\techo &quot;&lt;tr&gt;&lt;td colspan=5 align='center'&gt;&lt;input type='submit' value='REFRESH' name='REFRESH'&gt;&lt;\/td&gt;&lt;\/tr&gt;\\n&lt;\/table&gt;\\n&quot;;# just refresh the screen\r\n\tif (isset($_GET&#x5B;'STOP']) &amp;&amp; array_key_exists($_GET&#x5B;'STOP'], $inputvalues&#x5B;'values'])){############ shutdown system ########################\r\n\t\t$server = $_GET&#x5B;'STOP'];\r\n\t\techo &quot;&lt;h2&gt;Stop Server: &quot;.$server.&quot; &lt;\/h2&gt;\\n&lt;br&gt;&lt;br&gt;\\n&quot;;\r\n\t\techo shutdown_server($server);\r\n\t}\r\n\telseif (isset($_GET&#x5B;'START']) &amp;&amp; array_key_exists($_GET&#x5B;'START'], $inputvalues&#x5B;'values'])){############ wake up system ###################\r\n\t\t$server = $_GET&#x5B;'START'];\r\n\t\techo &quot;&lt;h2&gt;Wake Up System: &quot;.$server.&quot;&lt;\/h2&gt;\\n&lt;br&gt;&lt;br&gt;\\n&quot;;\r\n\t\techo start_server($server);\r\n\t}\r\n\telseif (isset($_GET&#x5B;'CHECK']) &amp;&amp; array_key_exists($_GET&#x5B;'CHECK'], $inputvalues&#x5B;'values'])){############ get response on connectiontrial ###################\r\n\t\t$server = $_GET&#x5B;'CHECK'];\r\n\t\techo &quot;&lt;h2&gt;System returns: &lt;\/h2&gt;\\n&lt;br&gt;&lt;br&gt;&quot;.substr(check_vitality($server), 1).&quot;\\n&lt;br&gt;&lt;br&gt;\\n&quot;;\r\n\t}\r\n\telseif (isset($_GET&#x5B;'EDIT'])) { ########### edit list of terminals #######################\r\n\t\techo &quot;&lt;h2&gt;Edit Terminallist&lt;\/h2&gt;\\n&quot;;\r\n\t\tedit_conf_file();\r\n\t}\r\n\telseif (isset($_GET&#x5B;'SAVE'])) { ########### save new entries to conf file ################\r\n\t\techo &quot;&lt;h2&gt;Save values to conf&lt;\/h2&gt;&quot;;\r\n\t\tif (save_conf_file($mac_adress_file)) echo &quot;Updating Conf File successful (Choose REFRESH to see changes)!&quot;;\r\n\t\telse echo &quot;Error during updating!&quot;;\r\n\t}\r\n\telseif (!isset($_GET&#x5B;'REFRESH']) &amp;&amp; !empty($_GET) &amp;&amp; !isset($_GET&#x5B;'CANCEL'])) echo &quot;&lt;h1&gt;Wrong Server choosen&lt;\/h1&gt;&quot;;############ user tries to cheat ################\r\n\t}\r\n#print_r($inputvalues);\r\n#debug_output();\r\necho &quot;&lt;\/form&gt;\\n&lt;\/Body&gt;&lt;\/HTML&gt;\\n&quot;;\r\n########### END ###################################?&gt;\r\n<\/pre>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n@CHARSET &quot;ISO-8859-1&quot;;\r\nbarbox_a {\r\n  position: absolute;\r\n  top: 330px;\r\n  left: 250px;\r\n  margin: 0px 0px 0px -160px;\r\n  width: 304px;\r\n  height: 24px;\r\n  background-color: black;\r\n}\r\n.per {\r\n  position: absolute;\r\n  top: 330px;\r\n  font-size: 18px;\r\n  left: 250px;\r\n  margin: 1px 0px 0px 150px;\r\n  background-color: #FFFFFF;\r\n}\r\n\r\n.bar {\r\n  position: absolute;\r\n  top: 332px;\r\n  left: 250px;\r\n  margin: 0px 0px 0px -158px;\r\n  width: 0px;\r\n  height: 20px;\r\n  background-color: #777777;\r\n  border-style: solid;\r\n  border-width: 1px;\r\n}\r\n\r\n.blank {\r\n  background-color: white;\r\n  width: 300px;\r\n  border-style: solid;\r\n  border-width: 1px;\r\n}\r\n.Stop-style {\r\n\tcolor: lightgray;\r\n\tbackground-color: firebrick;\r\n\t\r\n}\r\n.header-style {\r\n\tfont-size: 20px;\r\n}\r\n.Start-style {\r\n\ttext-align: center;\r\n\tbackground-color: blue;\r\n\tcolor: white;\r\n}\r\n.Comment-style {\r\n\tcolor: gray;\r\n\tbackground-color: lightgray;\r\n}\r\na {\r\n\tcolor: lightgray;\r\n}\r\n.attention-style {\r\n\tbackground-color: red;\r\n\tcolor: white;\r\n}\r\n.subheader-style {\r\n\tcolor: white;\r\n\tbackground-color: dimgray;\r\n}\r\n<\/pre>\n<p>Here is an example of the external file which lists the servers and its interfaces.<\/p>\n<p><code><br \/>\nbackup\tttyUSB0\tBackup\tServer<br \/>\nbananas\tttyUSB1\tNas\tServer\ton\tBanana\tPi<br \/>\ntoradex\tttyUSB2\tCertification\tAuthority\ton\tToradex<br \/>\nraspberrypi\tttyUSB3\tRaspberry\tPi\tfor\ttrials<br \/>\ncubietruck\tttyUSB4\tCubie\tMain\tServer<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. &lt;?php ############# HEAD ############################ echo &quot;&lt;!DOCTYPE html PUBLIC \\&quot;-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\\&quot; \\&quot;http:\/\/www.w3.org\/TR\/html4\/loose.dtd\\&quot;&gt;\\n&quot;; echo &quot;&lt;html&gt;\\n&lt;head&gt;\\n&lt;meta http-equiv=\\&quot;Content-Type\\&quot; content=\\&quot;text\/html; charset=UTF-8\\&quot;&gt;\\n&quot;; ############# docu ################################ \/* create user account on target machine &hellip; <a href=\"https:\/\/olkn.myvnc.com\/?p=451\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">wake on lan and terminal management page<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[35,51,79,116,131],"class_list":["post-451","post","type-post","status-publish","format-standard","hentry","category-programming","tag-administration","tag-debian","tag-linux","tag-server","tag-webfrontend"],"_links":{"self":[{"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/posts\/451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=451"}],"version-history":[{"count":1,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/posts\/451\/revisions"}],"predecessor-version":[{"id":452,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/posts\/451\/revisions\/452"}],"wp:attachment":[{"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}