How to get "Index" in Virtual Parameter?
Michael Ducharme
mducharme at gmail.com
Fri Sep 15 15:26:58 EDT 2017
Hello,
I am playing with virtual parameters for the first time and am trying to
get the IP address for the interface named 'loopback'. Unfortunately,
different devices will have a different 'index number', if that is the
correct term, for this interface. I have found the IP addresses table
contains this address in Device.IP.Interface.5.IPv4Address.1.IPAddress,
however it does not contain the name of the interface, which is
'loopback'. Instead, it contains a reference in
Device.IP.Interface.5.LowerLayers, which has the value
Device.X_MIKROTIK_Interface.Generic.3, and
Device.X_MIKROTIK_Interface.Generic.3.Name is 'loopback'.
Based on this I have worked out a specific algorithm that would
determine the IP address:
1. Iterate through Device.X_MIKROTIK_Interface.Generic.*.Name until
finding one where the name is the string 'loopback'
2. Once found, get the 'index number', i.e. the asterisk above, so that
if loopback is found in Device.X_MIKROTIK_Interface.Generic.5.Name,
it would return 5.
3. Iterate through Device.IP.Interface.*.LowerLayers to find one where
its value is Device.X_MIKROTIK_Interface.Generic.5.Name
4. Once found, get the 'index number' for the IP interface, example, '8'
5. Use this index number to get the IP address for the interface, ex.
Device.IP.Interface.8.IPv4Address.1.IPAddress
The algorithm I believe will work, but the thing I cannot figure out is
how to get this index number as it iterates through. The script in
progress is shown below. Initially I am just trying to fetch this 'index
number' for the first part and return it instead, just to make sure I
have part of the script working. Once I have that figured out I think
the rest will fall into place.
// Example: Fetch index for loopback interface
let m = 'Unknown'
let intnames = declare(
"Device.X_MIKROTIK_Interface.Generic.*.Name",
{value: Date.now()});
if (intnames.size) {
for (let myitem of intnames) {
if (myitem.value[0]=='loopback') {
m = myitem; // NOTE: This is the part I don't know, I want m to
be the index number where value was found
break;
}
}
}
return {writable: false, value: [m, "xsd:string"]};
Any ideas? I have done programming in quite a few languages before but
have done very little JavaScript.
Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.genieacs.com/pipermail/users/attachments/20170915/26282cc1/attachment.html>
More information about the Users
mailing list