forked from hopglass/node-respondd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bat-list-neighbours
executable file
·36 lines (32 loc) · 1.05 KB
/
bat-list-neighbours
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env node
const fs = require('fs')
const netIfaces = require('os').networkInterfaces()
const exec = require('child_process').execSync
var batIfaces = ["bat0"]
try {
var c = JSON.parse(fs.readFileSync("./config.json")).bat_iface
if (typeof c === "string") c = [c]
if (c) batIfaces = c.map(b => b.replace(/[^A-Za-z0-9\-_\.]/g, ''))
} catch (ignored) {
}
var res = {}
for(const batIface of batIfaces) {
var origs = exec("batctl meshif " + batIface + " originators").toString().trim()
if (origs) {
origs = origs.split("\n")
origs.forEach((orig) => {
var mac = orig.substring(3,20)
if (orig.substring(39,56) != mac) return
var lastSeen = parseFloat(orig.substring(21,29).trim())
var tq = parseInt(orig.substring(34,37).trim())
var iface = orig.split('[')[1].split(']')[0].trim()
var mac2 = netIfaces[iface][0].mac
if (!(mac2 in res)) res[mac2] = {"neighbours": {}}
res[mac2].neighbours[mac] = {
"tq": tq,
"lastseen": lastSeen
}
})
}
}
console.log(JSON.stringify(res))