sidetrak

SubnetUtil

May 1st, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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.
  3. var su = new SubnetUtil();
  4. su.<doSomething>
  5. */
  6. var SubnetUtil = Class.create();
  7.  
  8. SubnetUtil.prototype = {
  9.   initialize : function() {
  10.   },
  11.  
  12.   IPnumber: function(IPaddress) {
  13.       var ip = IPaddress.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/);
  14.       if(ip) {
  15.          return (+ip[1]<<24) + (+ip[2]<<16) + (+ip[3]<<8) + (+ip[4]);
  16.       }
  17.       return null;
  18.    },
  19.    
  20.    IPmask: function(maskSize) {
  21.       return -1<<(32-maskSize);
  22.    },
  23.    
  24.    inSubnet: function(IPaddress){
  25.       var matchedNetwork = '';
  26.       var networks = new GlideRecord('cmdb_ci_ip_network');
  27.       networks.query();
  28.       while(networks.next()) {
  29.          if((this.IPnumber(IPaddress) & this.IPmask(networks.subnet.split('/')[1])) == this.IPnumber(networks.subnet.split('/')[0])){
  30.             matchedNetwork = networks.sys_id + '';
  31.          }
  32.       }
  33.       if(matchedNetwork) {
  34.          return matchedNetwork;
  35.       }else{
  36.          return null;
  37.       }
  38.    }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment