Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Description: A function to check if a specified IP is a member of one of the IP Networks in the cmdb_ci_ip_network table.
- var su = new SubnetUtil();
- su.<doSomething>
- */
- var SubnetUtil = Class.create();
- SubnetUtil.prototype = {
- initialize : function() {
- },
- 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]);
- }
- return null;
- },
- IPmask: function(maskSize) {
- return -1<<(32-maskSize);
- },
- inSubnet: function(IPaddress){
- var matchedNetwork = '';
- var networks = new GlideRecord('cmdb_ci_ip_network');
- networks.query();
- while(networks.next()) {
- if((this.IPnumber(IPaddress) & this.IPmask(networks.subnet.split('/')[1])) == this.IPnumber(networks.subnet.split('/')[0])){
- matchedNetwork = networks.sys_id + '';
- }
- }
- if(matchedNetwork) {
- return matchedNetwork;
- }else{
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment