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