js IP效验,IP+掩码计算网络地址、广播地址、可用地址范围

js IP效验,IP+掩码计算网络地址、广播地址、可用地址范围

修复:二进制转换没有逆排序的BUG

  1. IP效验
    let iputils = {
    };
    
    iputils.checkIp = function(ip) {
    	var exp = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
    	var reg = ip.match(exp);
    	return reg != null;
    };
    

  2. 根据掩码获取掩码地址
    iputils.getSubnetMask = function(mask) {
    	 var result='';
    	 var sum = 32;
    	 var m = Number(mask);
    	 sum = sum-m;
    	 var str = "";
    	 for(var i=0;i<m;i++) {
    	      str = str+"1";
    	 }
    	 for(var j=0;j<sum;j++) {
    	      str = str+"0";
    	 }
    	 var arr = ['','','',''];
    	 for(var l=0;l<arr.length;l++) {
    	      arr[l]=str.substring(l*8,(l+1)*8);
    	      if(""==result)
    	         result = result+iputils.add(arr[l]);
    	      else result = result+"."+iputils.add(arr[l]);
    	 }
    		
    	 return result;
    }
    iputils.add = function(s) {
    	  //声明两个变量
    	 var str='';
    	 var count=0;
    	 var temp = s.toString();
    	 for (var i = 0; i < temp.length; i++) {
    	   str=temp.substring(temp.length-i-1,temp.length-i);//字符截取
    	   if(Number(str)==0)//转换成Internet类型
    	        count+=0;
    	   else if(i==0)
    	   	count+=1;
    	   else
    	   	count+=iputils.addc(i);
    	  }
    	  return count;//返回结果
    }
      //计算数字的次方
    iputils.addc = function(i) { 
    	 var v=1;
    	 for (var j = 0; j < i; j++) {
    	   v=v*2
    	 }
    	 return v;
    }
    

  3. 根据IP与掩码地址获取网络地址
    iputils.getNetAddress = function(ip, subnetMask) {
           var ip_str = '';
           var ip_arr = ip.split(".");
           var mask_arr = subnetMask.split(".");
    
           var ip2 = Number(ip_arr[2]);
           var ip3 = Number(ip_arr[3]);
           var mask2 = Number(mask_arr[2]);
           var mask3 = Number(mask_arr[3]);
    	
           ip_str=(ip2&mask2)+"."+(ip3&mask3);
           ip_str = ip_arr[0]+"."+ip_arr[1]+"."+ip_str;
           return ip_str;
    }
    

  4. 根据网络地址与主机位0的个数获取广播地址
    //获取广播地址
    iputils.getBroadcastAddress = function(netAddress, localNumber){
    	var netArr = netAddress.split(".");
    	var length = netArr.length;
    	var more = localNumber%8;
    	var except = parseInt(localNumber/8);
    	if(more!=0)
    		++except;
    	except = length - except;
    	for(var i=except;i<length;i++) {
    		var netPart = Number(netArr[i]);
    		var temp = iputils.convertBinary(netPart);
    		if(""==temp) {
    			temp="00000000";
    		}
    		if(i==except) {
    			if(more==0) {
    				temp="11111111";
    			}else {
    				temp = temp.substring(0,temp.length-more)+iputils.getNumber(more,"1");
    			}
    			var result = Number(temp);
    			result = iputils.binaryToDecimal(result);
    			netArr[i]=result+"";
    		}else {
    			netArr[i]="255";
    		}
    	}
    	return netArr.join(".");
    }
    
    //获取主机位0的个数
    iputils.getLocalNumber = function(subnetMask){
        var maskArr = subnetMask.split(".");
        var length = maskArr.length;
        for(var i=0;i<length;i++) {
            var maskPart = Number(maskArr[i]);
            var temp = iputils.convertBinary(maskPart);
            if("11111111"!=temp) {
                var index = temp.lastIndexOf("1")+1;
                var result = (8-index)+(length-i-1)*8;
    	    return result;
    	}
        }
        return 0;
    }
    //转成二进制
    iputils.convertBinary = function(sum){
        var binary = "";
        while (sum != 0 && sum != 1) {
    	    binary=(sum % 2)+binary
            sum = parseInt(sum / 2);
            if (sum == 0 || sum == 1) {
            	binary=(sum % 2)+binary
            }
        }
        if(binary.length<8){
            binary=iputils.getNumber((8-binary.length),"0")+binary;
        }
        return binary;
    }
    //获取0/1的字符串
    iputils.getNumber = function(number,str){
    	var result = "";
    	for(var i=0;i<number;i++) {
    		result=str+result;
    	}
    	return result;
    }
    
    //二进制转十进制
    iputils.binaryToDecimal = function(binaryNumber){
        var decimal = 0;
        var p = 0;
        while(true){
          if(binaryNumber == 0){
    	    break;
          }else {
    	    var temp = binaryNumber%10;
    	    decimal += temp*Math.pow(2, p);
    	    binaryNumber = parseInt(binaryNumber/10);
    	    p++;
           }
        }
        return decimal;
    }
    

  5. 根据IP与掩码地址获取网络地址
    //根据IP与掩码获取IP范围
    iputils.getIpRange = function(ip, mask) {
    	var subnetMask = iputils.getSubnetMask(mask);
    	var localNumber = iputils.getLocalNumber(subnetMask)
    	var netAddress = iputils.getNetAddress(ip, subnetMask)
    	var broadcastAddress = iputils.getBroadcastAddress(netAddress, localNumber)
    	var result = iputils.getIpRangez(netAddress, broadcastAddress);
    	console.log(subnetMask)
    	console.log(netAddress)
    	console.log(localNumber)
    	console.log(broadcastAddress)
    	console.log(iputils.getIpRangez(netAddress, broadcastAddress))
    	return result;
    };
    
    //获取Ip范围
    iputils.getIpRangez = function(netAddress, broadcastAddress){
    	var netArr = netAddress.split(".");
    	var number = Number(netArr[3])+1;
    	netArr[3] = number+"";
    	var broadcastArr = broadcastAddress.split(".");
    	number = Number(broadcastArr[3])-1;
    	broadcastArr[3] = number+"";
    	return netArr.join(".")+"-"+broadcastArr.join(".");
    }
    
    

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值