[85] | 1 | #!/bin/bash |
---|
| 2 | # |
---|
[355] | 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 |
---|
[85] | 7 | |
---|
[344] | 8 | export PATH=/bin:/sbin:/usr/bin:/usr/sbin |
---|
| 9 | export LANG=C |
---|
| 10 | |
---|
[85] | 11 | SITE_NAME='' |
---|
| 12 | REMOTE_SERVER=localhost |
---|
| 13 | REMOTE_USER='' |
---|
| 14 | REMOTE_FOLDER=/var/www/klask/ |
---|
| 15 | |
---|
[292] | 16 | GRAPH_MODULO=9 |
---|
| 17 | GRAPH_SHIFT=1 |
---|
| 18 | |
---|
[87] | 19 | if [ -f /etc/klask/push-web.conf ] |
---|
[85] | 20 | then |
---|
| 21 | . /etc/klask/push-web.conf |
---|
| 22 | fi |
---|
| 23 | |
---|
[87] | 24 | TMP='/tmp/klask-push-web' |
---|
[85] | 25 | |
---|
[87] | 26 | |
---|
| 27 | # remote command |
---|
| 28 | DOT=/usr/bin/dot |
---|
[85] | 29 | |
---|
[87] | 30 | |
---|
[344] | 31 | mkdir -p ${TMP} |
---|
[87] | 32 | if [ ! -d ${TMP} ] |
---|
[85] | 33 | then |
---|
| 34 | echo "Error: Folder ${TMP} do not exist !" |
---|
| 35 | exit 1 |
---|
| 36 | fi |
---|
| 37 | cd ${TMP} |
---|
| 38 | |
---|
| 39 | |
---|
[344] | 40 | LOCALTIME=$(date "+%Y-%m-%d %H:%M") |
---|
[85] | 41 | |
---|
| 42 | |
---|
[361] | 43 | klask exportsw --format dot --modulo ${GRAPH_MODULO} --shift ${GRAPH_SHIFT} > ${TMP}/switch-map.txt.new |
---|
[85] | 44 | |
---|
| 45 | |
---|
[344] | 46 | klask exportdb > ${TMP}/hostname-db.txt |
---|
| 47 | klask exportsw > ${TMP}/switch-db.txt |
---|
| 48 | klask ip-free > ${TMP}/ipfree-db.txt |
---|
| 49 | klask bad-vlan-id > ${TMP}/badvlan-db.txt |
---|
[85] | 50 | |
---|
| 51 | |
---|
[344] | 52 | cat <<END > ${TMP}/index.html |
---|
[349] | 53 | <!DOCTYPE html> |
---|
[350] | 54 | <html lang="en"> |
---|
[85] | 55 | <head> |
---|
| 56 | <link rel="stylesheet" type="text/css" href="style-klask.css" /> |
---|
[359] | 57 | <!-- <meta http-equiv="refresh" content="1800"> --> |
---|
[349] | 58 | <title>Klask mapping net-tools</title> |
---|
[85] | 59 | <noscript> |
---|
| 60 | </noscript> |
---|
| 61 | <script src="sorttable-klask.js"></script> |
---|
| 62 | <script> |
---|
| 63 | function delayedRefresh() { |
---|
| 64 | setTimeout( "refresh()", 1800*1000 ); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | function refresh() { |
---|
| 68 | history.go(0); |
---|
| 69 | } |
---|
[355] | 70 | |
---|
| 71 | function 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 | } |
---|
[365] | 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 |
---|
| 93 | function getById(id, context) { |
---|
| 94 | var el = document.getElementById(id); |
---|
| 95 | return context.contains(el) ? el : null; |
---|
| 96 | } |
---|
| 97 | function 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 |
---|
| 109 | window.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 |
---|
| 136 | window.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 | } |
---|
[85] | 149 | </script> |
---|
| 150 | </head> |
---|
[359] | 151 | <body> <!-- onload="delayedRefresh" --> |
---|
[85] | 152 | <h1> |
---|
[338] | 153 | <b><a href="http://servforge.legi.grenoble-inp.fr/projects/klask" class="circle">Klask</a></b> |
---|
[359] | 154 | Tool for <a href="switch-map.png">mapping</a> the local network - site ${SITE_NAME} |
---|
| 155 | <a class="refresh" href="./">${LOCALTIME}</a> |
---|
[85] | 156 | </h1> |
---|
| 157 | |
---|
[331] | 158 | <div id="navbar"> |
---|
| 159 | <ul> |
---|
[365] | 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> |
---|
[331] | 165 | </ul> |
---|
| 166 | </div> |
---|
[85] | 167 | |
---|
[331] | 168 | <div id="content"> |
---|
[355] | 169 | |
---|
| 170 | <div id="IP_detected" class="selectableTabContent"> |
---|
| 171 | <p> |
---|
[356] | 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). |
---|
[355] | 175 | </p> |
---|
[85] | 176 | END |
---|
| 177 | |
---|
[344] | 178 | klask exportdb --format html >> ${TMP}/index.html |
---|
[85] | 179 | |
---|
[344] | 180 | cat <<END >> ${TMP}/index.html |
---|
[355] | 181 | </div> |
---|
| 182 | |
---|
| 183 | <div id="IP_available" class="selectableTabContent" style="display:none"> |
---|
| 184 | <p> |
---|
[356] | 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. |
---|
[355] | 187 | </p> |
---|
[85] | 188 | END |
---|
| 189 | |
---|
[355] | 190 | klask ip-free --format html >> ${TMP}/index.html |
---|
[85] | 191 | |
---|
[355] | 192 | cat <<END >> ${TMP}/index.html |
---|
| 193 | </div> |
---|
[85] | 194 | |
---|
[355] | 195 | <div id="VLAN_mismatch" class="selectableTabContent" style="display:none"> |
---|
| 196 | <p> |
---|
[356] | 197 | List all computers connected to switches where the VLAN is misconfigured. |
---|
| 198 | Returns the port and VLAN to configure on these switches. |
---|
[355] | 199 | </p> |
---|
[85] | 200 | END |
---|
| 201 | |
---|
[355] | 202 | klask bad-vlan-id --format html >> ${TMP}/index.html |
---|
[85] | 203 | |
---|
[355] | 204 | cat <<END >> ${TMP}/index.html |
---|
| 205 | </div> |
---|
[85] | 206 | |
---|
[355] | 207 | <div id="MAP" class="selectableTabContent" style="display:none"> |
---|
| 208 | <p> |
---|
[356] | 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>. |
---|
[363] | 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. |
---|
[355] | 213 | </p> |
---|
| 214 | <img class="map" src="switch-map.png"> |
---|
| 215 | </div> |
---|
[329] | 216 | |
---|
[355] | 217 | <div id="RAW_data" class="selectableTabContent" style="display:none"> |
---|
| 218 | <p> |
---|
[356] | 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: |
---|
[355] | 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> |
---|
[356] | 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> |
---|
[355] | 228 | </ul> |
---|
| 229 | </p> |
---|
| 230 | </div> |
---|
[331] | 231 | </div> |
---|
[329] | 232 | |
---|
[343] | 233 | <p class="copyright"> |
---|
[341] | 234 | Klask (version $(klask version | grep ^Version | cut -f 2 -d ' ')) - $(klask version | grep ^Copyright | cut -f 1 -d '<') |
---|
| 235 | </p> |
---|
[363] | 236 | <!-- Try to prefect switch map image --> |
---|
| 237 | <link rel="prefetch" href="switch-map.png"> |
---|
[329] | 238 | </body> |
---|
| 239 | </html> |
---|
| 240 | END |
---|
| 241 | |
---|
[361] | 242 | cmp --silent ${TMP}/switch-map.txt.new ${TMP}/switch-map.txt || mv -f ${TMP}/switch-map.txt.new ${TMP}/switch-map.txt |
---|
[329] | 243 | |
---|
[95] | 244 | if [ "${REMOTE_SERVER}" = "localhost" ] |
---|
[85] | 245 | then |
---|
[362] | 246 | rsync --times \ |
---|
[85] | 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 | |
---|
[361] | 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) |
---|
[85] | 257 | |
---|
| 258 | else |
---|
[362] | 259 | rsync --times \ |
---|
[85] | 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 | |
---|
[361] | 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)" |
---|
[85] | 271 | fi |
---|