Getting a device's IP address with the Javascript API

Solved
OM
Oleg Mazur
millenniumtecfreez

Is there any way to get a device's IP address using SOTI's Javascript API? When I call the below function from within the API, I get back "undefined", but getting the same info using the %HOSTNAME% macro using legacy scripting returns an IP, is there anything I'm doing wrong here?

var ip = mobicontrol.network.IpAddress.hostName;
mobicontrol.log.info(""+ip);
The log was just to test what value gets returned from the variable above.
a year ago
Android Scripting
ANSWERS
R
Robert
a year ago

Hi,

Which agent version are you using? The ipAddress class is available since API level 3.

Otherwise the script looks ok or try with hostAddress instead of hostName, but both return ip if no hostname on device.

I did the same test here with same result in our environment on latest agent version.

Maybe this is not supported on any device. Or the call has to be different as other network scripts work which require API level 3 as well.

OM
Oleg Mazur
a year ago

I tried it with both hostAddress and hostName, same result, returns (undefined). I'm using agent 2024.0.3.1194 on all the devices I tried this on, they are all android devices with Android 11.

R
Robert
a year ago

Hi,
Were able to test it out now on a device, add the activeNetwork property to get the ip. It will return an array with both ipv4 and ipv6 address. 

Try this for ipv4 address:

var activeIP = mobicontrol.network.activeNetwork.ipAddresses;
mobicontrol.log.info("IPv4 Address: " +activeIP[1].hostAddress);

Solution
OM
Oleg Mazur
a year ago

Is it possible to get a substring of this output?

OM
Oleg Mazur
a year ago

Would this work? Based on what I was reading from SOTI's own docs, their ECMA implementation is limited and not all usual Javascript functions work

R
Robert
a year ago

Yes it will work, SOTI docs say support for ECMA edition 3 and limited for ECMA edition 4 and higher.
In the link in Rafaels post you can see split() is from ECMA edition 1.

You can also test it out on an test device to see the result to be sure it works on your device and agent.

OM
Oleg Mazur
a year ago

I ended up going with a much simpler solution for my problem, my original idea was to rename devices based on which of our warehouses they're in using the subnet address the devices are on, I just created an enumerator custom attribute with "warehouse IDs" that I applied per warehouse location folder in the console to achieve the same result.

C
CKMOD@SOTI
a year ago

Hi Oleg,
 
Thanks for posting on SOTI Pulse, Thanks Rafael and Robert
for responding to the post, your expertise and willingness to help are greatly appreciated!
 
Have you had an opportunity to test the suggested solutions by Rafael and Robert and has it successfully addressed your query?
 
If not, or If you have any additional questions or concerns, please don't hesitate to reach out. We're dedicated to providing assistance and support.