@gibme/mikrotik 2.0.4 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +50 -15
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -697,27 +697,62 @@ class Mikrotik extends ssh_1.default {
|
|
|
697
697
|
terse(command) {
|
|
698
698
|
return __awaiter(this, void 0, void 0, function* () {
|
|
699
699
|
const results = [];
|
|
700
|
-
|
|
701
|
-
.toString()
|
|
700
|
+
(yield this.exec(command)).toString()
|
|
702
701
|
.split('\r\n')
|
|
703
702
|
.map(line => line.trim())
|
|
704
|
-
.filter(line => line.split(/\s+/).length !== 0 && line.length !== 0)
|
|
705
|
-
|
|
706
|
-
const result = {
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
if (
|
|
711
|
-
result.
|
|
703
|
+
.filter(line => line.split(/\s+/).length !== 0 && line.length !== 0)
|
|
704
|
+
.forEach(line => {
|
|
705
|
+
const result = (() => {
|
|
706
|
+
const [, flags] = line.split(/\s+/).map(part => part.trim());
|
|
707
|
+
const result = {};
|
|
708
|
+
// get the id or the number
|
|
709
|
+
if (line.startsWith('*')) {
|
|
710
|
+
result.id = line.split(/\s/).shift();
|
|
711
|
+
}
|
|
712
|
+
else if (!isNaN(parseInt(line))) {
|
|
713
|
+
result.idx = parseInt(line);
|
|
712
714
|
}
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
715
|
+
result.flags = flags.includes('=') ? '' : flags;
|
|
716
|
+
return result;
|
|
717
|
+
})();
|
|
718
|
+
/**
|
|
719
|
+
* This looks like a mess; however, RouterOS will return values with spaced
|
|
720
|
+
* which causes havoc if a simple regex matcher is applied to the key-value pairs
|
|
721
|
+
*/
|
|
722
|
+
do {
|
|
723
|
+
const middle = line.indexOf('=') + 1;
|
|
724
|
+
const start = (() => {
|
|
725
|
+
const start = line.substring(0, middle).lastIndexOf(' ');
|
|
726
|
+
return start === -1 ? 0 : start;
|
|
727
|
+
})();
|
|
728
|
+
const next = (() => {
|
|
729
|
+
const next = line.substring(middle).indexOf('=');
|
|
730
|
+
return next === -1 ? -1 : next + middle;
|
|
731
|
+
})();
|
|
732
|
+
const end = (() => {
|
|
733
|
+
if (next === -1)
|
|
734
|
+
return -1;
|
|
735
|
+
const end = line.substring(middle, next).lastIndexOf(' ');
|
|
736
|
+
return end === -1 ? -1 : end + middle;
|
|
737
|
+
})();
|
|
738
|
+
const [key, value] = line.substring(start, end !== -1 ? end : undefined)
|
|
739
|
+
.trim().split(/=/);
|
|
740
|
+
if (parseFloat(value).toString() === value) {
|
|
741
|
+
result[key] = parseFloat(value);
|
|
742
|
+
}
|
|
743
|
+
else if (parseInt(value).toString() === value) {
|
|
744
|
+
result[key] = parseInt(value);
|
|
716
745
|
}
|
|
717
746
|
else {
|
|
718
|
-
result[
|
|
747
|
+
result[key] = value;
|
|
719
748
|
}
|
|
720
|
-
|
|
749
|
+
if (end > 0) {
|
|
750
|
+
line = line.substring(end);
|
|
751
|
+
}
|
|
752
|
+
else {
|
|
753
|
+
line = '';
|
|
754
|
+
}
|
|
755
|
+
} while (line.length !== 0);
|
|
721
756
|
if (Object.keys(result).length !== 0)
|
|
722
757
|
results.push(result);
|
|
723
758
|
});
|