source: trunk/push-web @ 367

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