source: trunk/push-web @ 390

Last change on this file since 390 was 372, checked in by g7moreau, 6 years ago
  • Update licence and copyright
  • Property svn:executable set to *
File size: 8.3 KB
Line 
1#!/bin/bash
2#
3# Copyright (C) 2005-2018 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>
4# License GNU GPL version 2 or later
5#
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
10
11export PATH=/bin:/sbin:/usr/bin:/usr/sbin
12export LANG=C
13
14SITE_NAME=''
15REMOTE_SERVER=localhost
16REMOTE_USER=''
17REMOTE_FOLDER=/var/www/klask/
18
19GRAPH_MODULO=9
20GRAPH_SHIFT=1
21
22if [ -f /etc/klask/push-web.conf ]
23then
24   . /etc/klask/push-web.conf
25fi
26
27TMP='/tmp/klask-push-web'
28
29
30# remote command
31DOT=/usr/bin/dot
32
33
34mkdir -p ${TMP}
35if [ ! -d ${TMP} ]
36then
37   echo "Error: Folder ${TMP} do not exist !"
38   exit 1
39fi
40cd ${TMP}
41
42
43LOCALTIME=$(date "+%Y-%m-%d %H:%M")
44
45
46klask exportsw --format dot --modulo ${GRAPH_MODULO} --shift ${GRAPH_SHIFT} > ${TMP}/switch-map.txt.new
47
48
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
53
54
55cat <<END > ${TMP}/index.html
56<!DOCTYPE html>
57<html lang="en">
58<head>
59 <link rel="stylesheet" type="text/css" href="style-klask.css" />
60 <!-- <meta http-equiv="refresh" content="1800"> -->
61 <title>Klask mapping net-tools</title>
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 }
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 }
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 }
152 </script>
153</head>
154<body> <!-- onload="delayedRefresh" -->
155<h1>
156<b><a href="http://servforge.legi.grenoble-inp.fr/projects/klask" class="circle">Klask</a></b>
157Tool for <a href="switch-map.png">mapping</a> the local network - site ${SITE_NAME}
158<a class="refresh" href="./">${LOCALTIME}</a>
159</h1>
160
161<div id="navbar">
162 <ul>
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>
168 </ul>
169</div>
170
171<div id="content">
172
173 <div id="IP_detected" class="selectableTabContent">
174  <p>
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).
178  </p>
179END
180
181klask exportdb --format html >> ${TMP}/index.html
182
183cat <<END >> ${TMP}/index.html
184 </div>
185
186 <div id="IP_available" class="selectableTabContent" style="display:none">
187  <p>
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.
190  </p>
191END
192
193klask ip-free --format html >> ${TMP}/index.html
194
195cat <<END >> ${TMP}/index.html
196 </div>
197
198 <div id="VLAN_mismatch" class="selectableTabContent" style="display:none">
199  <p>
200  List all computers connected to switches where the VLAN is misconfigured.
201  Returns the port and VLAN to configure on these switches.
202  </p>
203END
204
205klask bad-vlan-id --format html >> ${TMP}/index.html
206
207cat <<END >> ${TMP}/index.html
208 </div>
209
210 <div id="MAP" class="selectableTabContent" style="display:none">
211   <p>
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>.
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.
216   </p>
217   <img class="map" src="switch-map.png">
218 </div>
219
220 <div id="RAW_data" class="selectableTabContent" style="display:none">
221  <p>
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:
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>
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>
231   </ul>
232  </p>
233 </div>
234</div>
235
236<p class="copyright">
237Klask (version $(klask version | grep ^Version | cut -f 2 -d ' ')) - $(klask version | grep ^Copyright | cut -f 1 -d '<')
238</p>
239<!-- Try to prefect switch map image -->
240<link rel="prefetch" href="switch-map.png">
241</body>
242</html>
243END
244
245cmp --silent ${TMP}/switch-map.txt.new ${TMP}/switch-map.txt || mv -f ${TMP}/switch-map.txt.new ${TMP}/switch-map.txt
246
247if [ "${REMOTE_SERVER}" = "localhost" ]
248then
249   rsync --times \
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
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)
260
261else
262   rsync --times \
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
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)"
274fi
Note: See TracBrowser for help on using the repository browser.