Set Location based on IP

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 08:41 AM
Hi,
We are performing a Disocvery using IP Ranges and would like to use the IP Address to populate the lcoation of the discovered devices
This could be based on the first three octets of the IP For eg. if the IP is 10.2.8 - Site A, if the IP is 10.2.9 - Site B, etc
What is the best possible approach to this? We can create a mapping table to store the IP to Location mappings.
Thanks,
Ayman
- Labels:
-
Discovery
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 09:47 AM
There are some undocumented API's that discovery use for IP ranges that you can utilize. I have seen them in various business rules and script includes.
Here is some code that will check if an IP address is within a range. Thinking you can create a mapping table or add an IP range to your locations table and do a lookup. My concern is the number of lookups based upon the number of discovered items so you may need to batch update them. Hopefully this code will get you going. Since it is undocumented I cannot provide a ton of detail but this script did work via background script:
var start = new SncIPAddressV4("10.2.8.1");
var end = new SncIPAddressV4("10.2.8.254");
var range = new SncIPRangeV4;
range = range.getIPRangeV4Instance(start+"-"+end); // Expects a string value of StartIP-EndIP
function isInRange(ipAddress) {
var ipAddressLong = ipAddress.getAddressAsLong();
var rangeStart = range.getBeginAddress().getAddressAsLong();
var rangeEnd = range.getEndAddress().getAddressAsLong();
if (ipAddressLong >= rangeStart && ipAddressLong <= rangeEnd) {
return true;
} else {
return false;
}
}
var ipAddress1 = new SncIPAddressV4("10.2.8.2");
var ipAddress2 = new SncIPAddressV4("10.2.9.2");
gs.print(ipAddress1 + " is within range: " + isInRange(ipAddress1))
gs.print(ipAddress2 + " is within range: " + isInRange(ipAddress2))
Then the output of the script was:
[0:00:00.002] Script completed in scope global: script
*** Script: 10.2.8.2 is within range: true
*** Script: 10.2.9.2 is within range: false

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 11:38 AM
Thanks Michael, that is quite useful. Thinking about it again, I can do the following as well to get Location from the Mappings table
- Create an after Business Rule on the CMDB CI Table that runs on Insert when Discovery Source is ServiceNow
- The BR calls a GlideRecord query to the Mappings table with the IP Address of the CI and grab the location it returns
- Use the Returned Location to set to the Location of the CI
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 12:41 PM
Hi Ayman,
You could create a schedule for each location and use the out of the box functionality to set the location on the CIs discovered using that schedule:
https://docs.servicenow.com/bundle/kingston-it-operations-management/page/product/discovery/task/t_CreateADiscoverySchedule.html
From the docs page...
Location: Choose a location to assign to the CIs that the schedule discovers. If this field is blank, then no location is assigned.
Then you would need to create one or more range sets for each location and add the range sets to the correct schedule.
Regards,
Dave