- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2016 07:52 AM
Is there an existing script class I can use to determine if an IP address is in a given CIDR range?
Basically I want to make a call like: isInCIDR(ip_addr, cidr) and it returns true or false. I'd like to do this without first converting CIDR to range and then using SncIPRangeV4 for example.
Or as a corollary, is there a function I can call where given an IP address, it will return the IP Network (cmdb_ci_ip_network) that IP belongs to, regardless if I have that IP address mapped in CMDB.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2016 05:23 PM
Hi Jason,
Thanks for the inquiry. As far as I know, there's nothing OOB. It sounded like a fun thing to build on a weekend so... here's a script include you can use.
var CidrUtil = Class.create();
CidrUtil.prototype = {
initialize: function() {
},
ipIsInCidr : function(ip, cidr) {
var cidrIp = cidr.split('/')[0];
var cidrSm = cidr.split('/')[1];
return (this.IPnumber(ip) & this.IPmask(cidrSm)) == this.IPnumber(cidrIp);
},
IPnumber : function (IPaddress) {
var ip = IPaddress.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/);
if(ip) {
return (+ip[1]<<24) + (+ip[2]<<16) + (+ip[3]<<8) + (+ip[4]);
}
// else ... ?
return null;
},
IPmask : function(maskSize) {
return -1<<(32-maskSize);
},
type: 'CidrUtil'
};
Here's a small example how it can be used:
var ip = '192.168.1.4';
var cidr = '192.168.1.0/24';
var cu = new CidrUtil();
gs.log(cu.ipIsInCidr(ip, cidr));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2016 07:02 AM
Thanks Jason. I invite you to open an enhancement request (and link to this post.)
Our product managers are listening!
Enhancement requests: Tell us how you would improve the ServiceNow product
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2017 10:20 AM
I use this function. It's easier (to me) to read. At a test of only 10 Ip's it runs at the same speed.
checkRangeForIp: function(needle, network_summary){
//given a range(network_summary), return true if this ip(needle)
var needle_ip = new SncIPAddressV4(needle).getAddressAsLong();
var nw = new SncIPNetworkV4(network_summary);
var lo = nw.getAddress().getAddressAsLong();
var hi = nw.getBroadcastAddress().getAddressAsLong();
if (lo < needle_ip && needle_ip < hi){
return true;
} else {
return false;
}
},
Hope this helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 08:59 AM
Very interesting Johnny. I sure wish we had some good docs on that. The only reference I show is in the old Packages cross reference to scriptable objects, but no details. I'll send this over to our docs team to see what can be done about the gap.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2017 10:46 PM
Hi Chuck,
do you have any update where we can find some documentation of the class SncIPNetworkV4 or even better of the whole packages com.snc.commons.networks?
Any feedback is highly appreciated.
/Martin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2017 07:00 AM
I don't have any additional information at this time. It frustrates me that we have such a rich set of undocumented classes/methods. I have brought this up to the documentation team (and other similar cases.) I have yet to determine their means of approving/releasing certain APIs. Sorry about that.