Added advanced filters to meshctrl listdevices, #3109

Ylian Saint-Hilaire committed Sep 7, 2021 at 13:32 UTC 68c8a5c9013b42652f4371cfa17650bd233027eb
1 file changed +133 -6
meshctrl.js
+133 -6
@@ -390,6 +390,23 @@ if (args['_'].length == 0) {
390 console.log(" --count - Only return the device count.");
391 console.log(" --json - Show result as JSON.");
392 console.log(" --csv - Show result as comma seperated values.");
393 + console.log(" --filter \"[filter]\" - Filter devices using a filter string.");
394 + console.log(" \"x\" - Devices with \"x\" in the name.");
395 + console.log(" \"user:x or u:x\" - Devices with \"x\" in the name of currently logged in user.");
396 + console.log(" \"ip:x\" - Devices \"x\" IP address.");
397 + console.log(" \"group:x or g:x\" - Devices with \"x\" in device group name.");
398 + console.log(" \"tag:x or t:x\" - Devices with \"x\" in device tag.");
399 + console.log(" \"atag:x or a:x\" - Devices with \"x\" in device agent tag.");
400 + console.log(" \"os:x\" - Devices with \"x\" in the device OS description.");
401 + console.log(" \"amt:x\" - Devices with Intel AMT provisioning state (0, 1, 2).");
402 + console.log(" \"desc:x\" - Devices with \"x\" in device description.");
403 + console.log(" \"wsc:ok\" - Devices with Windows Security Center ok.");
404 + console.log(" \"wsc:noav\" - Devices with Windows Security Center with anti-virus problem.");
405 + console.log(" \"wsc:noupdate\" - Devices with Windows Security Center with update problem.");
406 + console.log(" \"wsc:nofirewall\" - Devices with Windows Security Center with firewall problem.");
407 + console.log(" \"wsc:any\" - Devices with Windows Security Center with any problem.");
408 + console.log(" \"a and b\" - Match both conditions with precedence over OR. For example: \"lab and g:home\".");
409 + console.log(" \"a or b\" - Math one of the conditions, for example: \"lab or g:home\".");
410 console.log(" --filterid [id,id...] - Show only results for devices with included id.");
411 console.log(" --details - Show all device details.");
412 break;
@@ -1960,6 +1977,14 @@ function serverConnect() {
1977 }
1978 }
1979
1980 + // Filter devices based on filter string
1981 + if (args.filter != null) {
1982 + for (var meshid in data.nodes) {
1983 + for (var d in data.nodes[meshid]) { data.nodes[meshid][d].meshid = meshid; }
1984 + data.nodes[meshid] = parseSearchOrInput(data.nodes[meshid], args.filter.toLowerCase());
1985 + }
1986 + }
1987 +
1988 if (args.csv) {
1989 // Return a flat list
1990 var nodecount = 0;
@@ -1996,12 +2021,14 @@ function serverConnect() {
2021 var nodecount = 0;
2022 for (var i in data.nodes) {
2023 var devicesInMesh = data.nodes[i];
1999 - if (settings.xmeshes) { console.log('\r\nDevice group: \"' + settings.xmeshes[i].name.split('\"').join('') + '\"'); }
2000 - console.log('id, name, icon, conn, pwr\r\n-------------------------');
2001 - for (var j in devicesInMesh) {
2002 - var n = devicesInMesh[j];
2003 - nodecount++;
2004 - console.log('\"' + n._id.split('/')[2] + '\", \"' + n.name.split('\"').join('') + '\", ' + (n.icon ? n.icon : 0) + ', ' + (n.conn ? n.conn : 0) + ', ' + (n.pwr ? n.pwr : 0));
2024 + if (devicesInMesh.length > 0) {
2025 + if (settings.xmeshes) { console.log('\r\nDevice group: \"' + settings.xmeshes[i].name.split('\"').join('') + '\"'); }
2026 + console.log('id, name, icon, conn, pwr\r\n-------------------------');
2027 + for (var j in devicesInMesh) {
2028 + var n = devicesInMesh[j];
2029 + nodecount++;
2030 + console.log('\"' + n._id.split('/')[2] + '\", \"' + n.name.split('\"').join('') + '\", ' + (n.icon ? n.icon : 0) + ', ' + (n.conn ? n.conn : 0) + ', ' + (n.pwr ? n.pwr : 0));
2031 + }
2032 }
2033 }
2034 if (nodecount == 0) { console.log('None'); }
@@ -2135,6 +2162,106 @@ function padString(str, pad) {
2162 if (str.length >= pad) return str; return str + xpad.substring(0, pad - str.length)
2163 }
2164
2165 +function parseSearchAndInput(nodes, x) {
2166 + var s = x.split(' ' + "and" + ' '), r = null;
2167 + for (var i in s) {
2168 + var r2 = getDevicesThatMatchFilter(nodes, s[i]);
2169 + if (r == null) { r = r2; } else { var r3 = []; for (var j in r2) { if (r.indexOf(r2[j]) >= 0) { r3.push(r2[j]); } } r = r3; }
2170 + }
2171 + return r;
2172 +}
2173 +
2174 +function parseSearchOrInput(nodes, x) {
2175 + var s = x.split(' ' + "or" + ' '), r = null;
2176 + for (var i in s) { var r2 = parseSearchAndInput(nodes, s[i]); if (r == null) { r = r2; } else { for (var j in r2) { if (r.indexOf(r2[j] >= 0)) { r.push(r2[j]); } } } }
2177 + return r;
2178 +}
2179 +
2180 +function getDevicesThatMatchFilter(nodes, x) {
2181 + var r = [];
2182 + var userSearch = null, ipSearch = null, groupSearch = null, tagSearch = null, agentTagSearch = null, wscSearch = null, osSearch = null, amtSearch = null, descSearch = null;
2183 + if (x.startsWith("user:".toLowerCase())) { userSearch = x.substring("user:".length); }
2184 + else if (x.startsWith("u:".toLowerCase())) { userSearch = x.substring("u:".length); }
2185 + else if (x.startsWith("ip:".toLowerCase())) { ipSearch = x.substring("ip:".length); }
2186 + else if (x.startsWith("group:".toLowerCase())) { groupSearch = x.substring("group:".length); }
2187 + else if (x.startsWith("g:".toLowerCase())) { groupSearch = x.substring("g:".length); }
2188 + else if (x.startsWith("tag:".toLowerCase())) { tagSearch = x.substring("tag:".length); }
2189 + else if (x.startsWith("t:".toLowerCase())) { tagSearch = x.substring("t:".length); }
2190 + else if (x.startsWith("atag:".toLowerCase())) { agentTagSearch = x.substring("atag:".length); }
2191 + else if (x.startsWith("a:".toLowerCase())) { agentTagSearch = x.substring("a:".length); }
2192 + else if (x.startsWith("os:".toLowerCase())) { osSearch = x.substring("os:".length); }
2193 + else if (x.startsWith("amt:".toLowerCase())) { amtSearch = x.substring("amt:".length); }
2194 + else if (x.startsWith("desc:".toLowerCase())) { descSearch = x.substring("desc:".length); }
2195 + else if (x == 'wsc:ok') { wscSearch = 1; }
2196 + else if (x == 'wsc:noav') { wscSearch = 2; }
2197 + else if (x == 'wsc:noupdate') { wscSearch = 3; }
2198 + else if (x == 'wsc:nofirewall') { wscSearch = 4; }
2199 + else if (x == 'wsc:any') { wscSearch = 5; }
2200 +
2201 + if (x == '') {
2202 + // No search
2203 + for (var d in nodes) { r.push(nodes[d]); }
2204 + } else if (ipSearch != null) {
2205 + // IP address search
2206 + for (var d in nodes) { if ((nodes[d].ip != null) && (nodes[d].ip.indexOf(ipSearch) >= 0)) { r.push(nodes[d]); } }
2207 + } else if (groupSearch != null) {
2208 + // Group filter
2209 + if (settings.xmeshes) { for (var d in nodes) { if (settings.xmeshes[nodes[d].meshid].name.toLowerCase().indexOf(groupSearch) >= 0) { r.push(nodes[d]); } } }
2210 + } else if (tagSearch != null) {
2211 + // Tag filter
2212 + for (var d in nodes) {
2213 + if (((nodes[d].tags == null) && (tagSearch == '')) || ((nodes[d].tags != null) && (nodes[d].tags.indexOf(tagSearch) >= 0))) { r.push(nodes[d]); }
2214 + }
2215 + } else if (agentTagSearch != null) {
2216 + // Agent Tag filter
2217 + for (var d in nodes) {
2218 + if ((((nodes[d].agent != null) && (nodes[d].agent.tag == null)) && (agentTagSearch == '')) || ((nodes[d].agent != null) && (nodes[d].agent.tag != null) && (nodes[d].agent.tag.toLowerCase().indexOf(agentTagSearch) >= 0))) { r.push(nodes[d]); };
2219 + }
2220 + } else if (userSearch != null) {
2221 + // User search
2222 + for (var d in nodes) {
2223 + if (nodes[d].users && nodes[d].users.length > 0) { for (var i in nodes[d].users) { if (nodes[d].users[i].toLowerCase().indexOf(userSearch) >= 0) { r.push(nodes[d]); } } }
2224 + }
2225 + } else if (osSearch != null) {
2226 + // OS search
2227 + for (var d in nodes) { if ((nodes[d].osdesc != null) && (nodes[d].osdesc.toLowerCase().indexOf(osSearch) >= 0)) { r.push(nodes[d]); }; }
2228 + } else if (amtSearch != null) {
2229 + // Intel AMT search
2230 + for (var d in nodes) { if ((nodes[d].intelamt != null) && ((amtSearch == '') || (nodes[d].intelamt.state == amtSearch))) { r.push(nodes[d]); } }
2231 + } else if (descSearch != null) {
2232 + // Device description search
2233 + for (var d in nodes) { if ((nodes[d].desc != null) && (nodes[d].desc != '') && ((descSearch == '') || (nodes[d].desc.toLowerCase().indexOf(descSearch) >= 0))) { r.push(nodes[d]); } }
2234 + } else if (wscSearch != null) {
2235 + // Windows Security Center
2236 + for (var d in nodes) {
2237 + if (nodes[d].wsc) {
2238 + if ((wscSearch == 1) && (nodes[d].wsc.antiVirus == 'OK') && (nodes[d].wsc.autoUpdate == 'OK') && (nodes[d].wsc.firewall == 'OK')) { r.push(nodes[d]); }
2239 + else if (((wscSearch == 2) || (wscSearch == 5)) && (nodes[d].wsc.antiVirus != 'OK')) { r.push(nodes[d]); }
2240 + else if (((wscSearch == 3) || (wscSearch == 5)) && (nodes[d].wsc.autoUpdate != 'OK')) { r.push(nodes[d]); }
2241 + else if (((wscSearch == 4) || (wscSearch == 5)) && (nodes[d].wsc.firewall != 'OK')) { r.push(nodes[d]); }
2242 + }
2243 + }
2244 + } else if (x == '*') {
2245 + // Star filter
2246 + for (var d in nodes) { if (stars[nodes[d]._id] == 1) { r.push(nodes[d]); } }
2247 + } else {
2248 + // Device name search
2249 + try {
2250 + var rs = x.split(/\s+/).join('|'), rx = new RegExp(rs); // In some cases (like +), this can throw an exception.
2251 + for (var d in nodes) {
2252 + //if (showRealNames) {
2253 + //if (nodes[d].rnamel != null && rx.test(nodes[d].rnamel.toLowerCase())) { r.push(nodes[d]); }
2254 + //} else {
2255 + if (rx.test(nodes[d].name.toLowerCase())) { r.push(nodes[d]); }
2256 + //}
2257 + }
2258 + } catch (ex) { for (var d in nodes) { r.push(nodes[d]); } }
2259 + }
2260 +
2261 + return r;
2262 +}
2263 +
2264 +
2265 // Connect tunnel to a remote agent
2266 function connectTunnel(url) {
2267 // Setup WebSocket options