source: trunk/push-web @ 394

Last change on this file since 394 was 372, checked in by g7moreau, 6 years ago
  • Update licence and copyright
  • Property svn:executable set to *
File size: 8.3 KB
RevLine 
[85]1#!/bin/bash
2#
[372]3# Copyright (C) 2005-2018 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>
4# License GNU GPL version 2 or later
5#
[355]6# 2007/10/23 Gabriel Moreau
7# 2010/11/03 Gabriel Moreau
8# 2011/03/30 Gabriel Moreau - make it generic
9# 2017/11/04 Gabriel Moreau - all reworks - use tab
[85]10
[344]11export PATH=/bin:/sbin:/usr/bin:/usr/sbin
12export LANG=C
13
[85]14SITE_NAME=''
15REMOTE_SERVER=localhost
16REMOTE_USER=''
17REMOTE_FOLDER=/var/www/klask/
18
[292]19GRAPH_MODULO=9
20GRAPH_SHIFT=1
21
[87]22if [ -f /etc/klask/push-web.conf ]
[85]23then
24   . /etc/klask/push-web.conf
25fi
26
[87]27TMP='/tmp/klask-push-web'
[85]28
[87]29
30# remote command
31DOT=/usr/bin/dot
[85]32
[87]33
[344]34mkdir -p ${TMP}
[87]35if [ ! -d ${TMP} ]
[85]36then
37   echo "Error: Folder ${TMP} do not exist !"
38   exit 1
39fi
40cd ${TMP}
41
42
[344]43LOCALTIME=$(date "+%Y-%m-%d %H:%M")
[85]44
45
[361]46klask exportsw --format dot --modulo ${GRAPH_MODULO} --shift ${GRAPH_SHIFT} > ${TMP}/switch-map.txt.new
[85]47
48
[344]49klask exportdb    > ${TMP}/hostname-db.txt
50klask exportsw    > ${TMP}/switch-db.txt
51klask ip-free     > ${TMP}/ipfree-db.txt
52klask bad-vlan-id > ${TMP}/badvlan-db.txt
[85]53
54
[344]55cat <<END > ${TMP}/index.html
[349]56<!DOCTYPE html>
[350]57<html lang="en">
[85]58<head>
59 <link rel="stylesheet" type="text/css" href="style-klask.css" />
[359]60 <!-- <meta http-equiv="refresh" content="1800"> -->
[349]61 <title>Klask mapping net-tools</title>
[85]62 <noscript>
63 </noscript>
64 <script src="sorttable-klask.js"></script>
65 <script>
66function delayedRefresh() {
67 setTimeout( "refresh()", 1800*1000 );
68 }
69
70function refresh() {
71 history.go(0);
72 }
[355]73 
74function selectTab(event, tabName) {
75 var i;
76 var x = document.getElementsByClassName("selectableTabContent");
77 var tablinks = document.getElementsByClassName("selectableTabButton");
78 for (i = 0; i < x.length; i++) {
79  x[i].style.display = "none";
80  }
81 for (i = 0; i < x.length; i++) {
82  tablinks[i].id = "";
83  }
84 document.getElementById(tabName).style.display = "block";
85 event.currentTarget.id = "selected";
86 }
[365]87
88// https://stackoverflow.com/questions/17591447/how-to-reload-current-page-without-losing-any-form-data
89// http://www.uitrick.com/javascript/how-to-keep-a-global-variable-alive-even-after-page-refresh-javascript/
90// https://blog.udemy.com/javascript-page-refresh/
91
92// localStorage.setItem('bgcolor', document.getElementById('bgcolor').value);
93// localStorage.getItem('bgcolor');
94
95// https://stackoverflow.com/questions/5683087/chaining-getelementbyid
96function getById(id, context) {
97 var el = document.getElementById(id);
98 return context.contains(el) ? el : null;
99 }
100function getByName(name, context) {
101 var i;
102 var x = document.getElementsByName(name);
103 for (i = 0; i < x.length; i++) {
104  if ((x[i].getAttribute('name') == name) && (context.contains(x[i]))) {
105   return x[i];
106   }
107  }
108 return null;
109 }
110
111// Run on page load
112window.onload = function() {
113 if (typeof(Storage) !== "undefined") {
114  // Code for localStorage/sessionStorage.
115  var tabName = localStorage.getItem('navbar-selected');
116  if (tabName == "undefined") { return; }
117  var navbar = document.getElementById('navbar');
118  var tab = getByName(tabName, navbar);
119  var i;
120  var x = document.getElementsByClassName("selectableTabContent");
121  var tablinks = document.getElementsByClassName("selectableTabButton");
122
123  for (i = 0; i < x.length; i++) {
124   x[i].style.display = "none";
125   }
126  for (i = 0; i < x.length; i++) {
127   tablinks[i].id = "";
128   }
129
130  document.getElementById(tabName).style.display = "block";
131  tab.id = "selected";
132  }
133 else {
134  // Sorry! No Web Storage support..
135  }
136 }
137
138// Before refreshing the page, save the form data to sessionStorage
139window.onbeforeunload = function() {
140 if (typeof(Storage) !== "undefined") {
141  // Code for localStorage/sessionStorage.
142  // var navbar = document.getElementById('navbar');
143  var selected = document.getElementById('selected'); // getById('selected', navbar);
144
145  var tabName = selected.getAttribute('name');
146  localStorage.setItem('navbar-selected', tabName);
147  }
148 else {
149  // Sorry! No Web Storage support..
150  }
151 }
[85]152 </script>
153</head>
[359]154<body> <!-- onload="delayedRefresh" -->
[85]155<h1>
[338]156<b><a href="http://servforge.legi.grenoble-inp.fr/projects/klask" class="circle">Klask</a></b>
[359]157Tool for <a href="switch-map.png">mapping</a> the local network - site ${SITE_NAME}
158<a class="refresh" href="./">${LOCALTIME}</a>
[85]159</h1>
160
[331]161<div id="navbar">
162 <ul>
[365]163  <li class="selectableTabButton" name="IP_detected"   onclick="selectTab(event, 'IP_detected')" id="selected">IP detected</li>
164  <li class="selectableTabButton" name="IP_available"  onclick="selectTab(event, 'IP_available')">IP available</li>
165  <li class="selectableTabButton" name="VLAN_mismatch" onclick="selectTab(event, 'VLAN_mismatch')">VLAN mismatch</li>
166  <li class="selectableTabButton" name="MAP"           onclick="selectTab(event, 'MAP')">MAP (png)</li>
167  <li class="selectableTabButton" name="RAW_data"      onclick="selectTab(event, 'RAW_data')">RAW data</li>
[331]168 </ul>
169</div>
[85]170
[331]171<div id="content">
[355]172
173 <div id="IP_detected" class="selectableTabContent">
174  <p>
[356]175  List of all IP addresses detected on the local network (on all VLANs)
176  since the origin of the database
177  (except those that have been deleted manually and voluntarily).
[355]178  </p>
[85]179END
180
[344]181klask exportdb --format html >> ${TMP}/index.html
[85]182
[344]183cat <<END >> ${TMP}/index.html
[355]184 </div>
185
186 <div id="IP_available" class="selectableTabContent" style="display:none">
187  <p>
[356]188  List all free IP addresses (which have never been used or not used for two years).
189  These IP addresses are available to assign to all new computers.
[355]190  </p>
[85]191END
192
[355]193klask ip-free --format html >> ${TMP}/index.html
[85]194
[355]195cat <<END >> ${TMP}/index.html
196 </div>
[85]197
[355]198 <div id="VLAN_mismatch" class="selectableTabContent" style="display:none">
199  <p>
[356]200  List all computers connected to switches where the VLAN is misconfigured.
201  Returns the port and VLAN to configure on these switches.
[355]202  </p>
[85]203END
204
[355]205klask bad-vlan-id --format html >> ${TMP}/index.html
[85]206
[355]207cat <<END >> ${TMP}/index.html
208 </div>
[85]209
[355]210 <div id="MAP" class="selectableTabContent" style="display:none">
211   <p>
[356]212   Direct link to local network map in the formats <a href="switch-map.png">png</a> and <a href="switch-map.svg">svg</a>.
[363]213   Because the map could be big,
214   you will have a better web experience with
215   <a href="https://addons.mozilla.org/en-US/firefox/addon/image-zoom/">Image Zoom<a> plugin (or equivalent) installed.
[355]216   </p>
217   <img class="map" src="switch-map.png">
218 </div>
[329]219
[355]220 <div id="RAW_data" class="selectableTabContent" style="display:none">
221  <p>
[356]222   The main raw results of Klask are accessible directly,
223   via <tt>wget</tt> or <tt>curl</tt> tools for example,
224   for any specific treatment:
[355]225   <ul>
226    <li><a href="hostname-db.txt">Computers DB</a> (command <tt>klask exportdb --format txt</tt>),</li>
227    <li><a href="switch-db.txt">Switches DB</a> (command <tt>klask exportsw --format txt</tt>),</li>
228    <li><a href="ipfree-db.txt">IP available</a> (command <tt>klask ip-free --format txt</tt>),</li>
229    <li><a href="badvlan-db.txt">VLAN mismatch</a> (command <tt>klask bad-vlan-id --format txt</tt>),</li>
[356]230    <li>Map format: <a href="switch-map.txt">dot source</a> (graphviz), <a href="switch-map.png">png</a>, <a href="switch-map.svg">svg</a>.</li>
[355]231   </ul>
232  </p>
233 </div>
[331]234</div>
[329]235
[343]236<p class="copyright">
[341]237Klask (version $(klask version | grep ^Version | cut -f 2 -d ' ')) - $(klask version | grep ^Copyright | cut -f 1 -d '<')
238</p>
[363]239<!-- Try to prefect switch map image -->
240<link rel="prefetch" href="switch-map.png">
[329]241</body>
242</html>
243END
244
[361]245cmp --silent ${TMP}/switch-map.txt.new ${TMP}/switch-map.txt || mv -f ${TMP}/switch-map.txt.new ${TMP}/switch-map.txt
[329]246
[95]247if [ "${REMOTE_SERVER}" = "localhost" ]
[85]248then
[362]249   rsync --times \
[85]250      /usr/share/klask/sorttable-klask.js \
251      /usr/share/klask/style-klask.css \
252      ${TMP}/index.html \
253      ${TMP}/*-db.txt \
254      ${TMP}/switch-map.txt \
255      ${REMOTE_FOLDER}/
256
[361]257   [ -x ${DOT} ] && (cd ${REMOTE_FOLDER};
258      [ switch-map.txt -nt switch-map.svg ] && ${DOT} -T svg switch-map.txt > switch-map.svg;
259      [ switch-map.txt -nt switch-map.png ] && ${DOT} -T png switch-map.txt > switch-map.png)
[85]260
261else
[362]262   rsync --times \
[85]263      /usr/share/klask/sorttable-klask.js \
264      /usr/share/klask/style-klask.css \
265      ${TMP}/index.html \
266      ${TMP}/*-db.txt \
267      ${TMP}/switch-map.txt \
268      ${REMOTE_USER}@${REMOTE_SERVER}:${REMOTE_FOLDER}/
269
270
[361]271   ssh ${REMOTE_USER}@${REMOTE_SERVER} "(cd ${REMOTE_FOLDER};
272      [ switch-map.txt -nt switch-map.svg ] && ${DOT} -T svg switch-map.txt > switch-map.svg;
273      [ switch-map.txt -nt switch-map.png ] && ${DOT} -T png switch-map.txt > switch-map.png)"
[85]274fi
Note: See TracBrowser for help on using the repository browser.