This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathwatchIPList.js
73 lines (66 loc) · 1.79 KB
/
watchIPList.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// src iplist
var MikroNode = require('../lib/index.js');
var c1 = MikroNode.getConnection(process.argv[2], process.argv[3], process.argv[4], {
timeout : 0
});
var ipList = {};
function ipOutput(p) {
console.log(JSON.stringify(p));
var v = MikroNode.parseItems(p)[0]; // Returns a row of items, but we only need one.
var t = ipList[v['.id']];
console.log(JSON.stringify(v));
if (v['.dead']) { // if it was removed...
if (!t) {
t = {
name : "Unknown"
}; // we don't have this ID in our list.
}
console.log('IP: ' + t.address + ' deleted');
delete ipList[v['.id']];
} else {
var c = [];
if (!t) {
ipList[v['.id']] = v;
} else {
Object.keys(v).forEach(function(k) {
if (v[k] !== t[k]) {
if (c.length === 0) {
c.push('changed ');
}
c.push(" (" + k + ')' + t[k] + ' to ' + v[k] + ' ');
ipList[v['.id']][k] = v[k];
}
});
}
console.log('IP: address ' + v.address + c.join("\n"));
}
return true;
}
function connCallback(connection) {
var o = connection.openChannel();
o.addListener('trap', function(e) {
console.log('There was an error: ' + e);
});
o.write('/ip/address/print', function(channel) {
channel.once('done', function(p) {
console.log(p);
var d = MikroNode.parseItems(p);
d.forEach(function(p) {
console.log('Loaded: (' + p['.id'] + ')' + p.address);
ipList[p['.id']] = p;
});
});
});
o.write('/ip/address/listen', function(channel) {
console.log('Listening to ip changes.');
console.log('Press CTRL-C to stop listening.');
channel.on('done', function() {
console.log('ip listen done');
});
channel.addListener('read', ipOutput); // report when an IP is being addeed.
});
}
c1.addListener('trap', function(e) {
console.log('Connection caught a trap: ' + e.message);
});
c1.connect(connCallback);