分享一个自制的计算子网划分的小工具

使用 javascirpt 写的,因此可以使用浏览器浏览即可

code:

<meta charset="utf-8">

输入划分网段的数量:
<input id="inp_netCount" />
<input type="button" onclick="createElem()" value="start"><hr>
<div id="div_showNet" style="border:1px solid #c3c3c3;width:500px;height:300px;overflow:scroll">
</div><br/><br>

输入被划分的IP地址:
<input id="inp_startIP" />
<input type="button" onclick="beginInit()" value="start"><hr>
<div id="">
  <table class="table_showResult" id="table_showResult" border=1>
    <tr>
       <td>网络地址</td>
       <td>子网掩码</td>
       <td>第一地址</td>
       <td>最后地址</td>
       <td>广播地址</td>
    </tr>
  </table>
</div>

<script>
 /*************************
  *        GOLBAL         *
  *************************/
  var __pointer = 0;
  var netcount = 0;
  var hostbit = new Array();
  var beginIP = new Array();
  var netmask = new Array();

 
 /*************************
  *      CREATE INP       *
  *************************/
  function createElem(){
    var str = "";
    netcount = document.getElementById("inp_netCount").value;
    for(var i=1; i<=netcount; i++){
      str += "第" + (i<10?i="0"+i:i) + "网段需要";
      str += "<input id=a"+(i-0)+" size='10' value='"+i*100+"'/>台&nbsp;";
      if(i%2==0)str+="<br/>"
    }
    document.getElementById("div_showNet").innerHTML = str
  }
 

 /*************************
  *      INIT MAIN        *
  *************************/
  function beginInit(){

    calcHostBit();
    calcBeginIP();
    calcNetMask();

    //alert(hostbit.join("\n"));
    //alert(beginIP.join("\n"));
    //alert(netmask.join("\n"));
    
    for(var i=0; i<netcount; i++)
      calcTheMain(i);
  }
 

  function calcHostBit(){
    var b = 0;
    var hostcount;

    //clearHostbit
    for(i=0; i<netcount; i++)
      hostbit[i] = 0;
    
    //setHostbit
    for(i=0; i<netcount; i++){
      hostcount = document.getElementById("a"+(i+1)).value - 0 + 2;

      while( (1<<b) <  hostcount )b++;
      hostbit[i] = b;
      b=0;

    }



    //sortHostbit
    for(i=0; i<netcount; i++)
      hostbit[i]-=0;

    //for(i=0; i<netcount; i++)
     // alert(hostbit[i])

    hostbit.sort(function s(a,b){return b-a});


 
  }


  function calcBeginIP(){

    __pointer = 0;

    var increment = 0;
    var carry_num = 0;
    var startIP_String = "";
    var startIP_Array  = new Array();

    beginIP[0] = document.getElementById("inp_startIP").value;
    
    for(var i=1; i<=netcount; i++){

      startIP_Array = beginIP[i-1].split(".");

      for(var x in startIP_Array)
        startIP_Array[x]-=0;

      increment = 1<<hostbit[__pointer];
      startIP_Array[3] += increment;

      for(var j=3; j>0; j--){
        carry_num = Math.floor(startIP_Array[j] / 256);
        startIP_Array[j] %= 256;
        startIP_Array[j-1] += carry_num;
      }

      __pointer++;
      beginIP[i] = startIP_Array.join(".");
    }

  }
 

  function  calcNetMask(){
     
     var str="";

     for(var i=0; i<netcount; i++){
        var n = hostbit[i]-0;

        if( n==32 )str = "0.0.0.0";
        else if( n>24 )str = changeToNum(n-24) + ".0.0.0";
        else if( n>16 )str = "255."            + changeToNum(n-16) + ".0.0";
        else if( n>8  )str = "255.255."        + changeToNum(n-8)  + ".0"
        else           str = "255.255.255."    + changeToNum(n)

        netmask[i] = str
     }

  }



  function  calcTheMain(i){

     var calcresult = "";
     
     var network_string   = beginIP[i]      //获取网络号
     var netmask_string   = netmask[i]      //获取子网号
     var broadcast_string = beginIP[i+1]    //获取下个网络号
     
     var network_array   = network_string.split(".")
     var netmask_array   = netmask_string.split(".")
     var broadcast_array = broadcast_string.split(".")  

     for(var j=3; j>0; j--){        //计算广播地址
        if(--broadcast_array[j] < 0)          //如果不够减先前借一位
           broadcast_array[j]=255;
        else
           break;            //如果够减了,退出循环
     }


     if( broadcast_array[3]%2 == 0){    //如果最后一位是偶数
          
        alert("计算错误,广播地址意外为偶数!");
        return ;

     }
        
     

     var broadcast_string = broadcast_array.join(".")


     network_array[3]-=0;            //计算首个地址
     network_array[3]+=1;
     var firstaddr_string = network_array.join(".")

     
     broadcast_array[3]-=0;            //计算最后地址
     broadcast_array[3]-=1;
     var lastaddr_string = broadcast_array.join(".")


     calcresult += "<tr>";
     calcresult += "<td>"+ network_string   +"</td>";
     calcresult += "<td>"+ netmask_string   +"</td>";
     calcresult += "<td>"+ firstaddr_string +"</td>";
     calcresult += "<td>"+ lastaddr_string  +"</td>";
     calcresult += "<td>"+ broadcast_string  +"</td>";   
     calcresult += "</tr>";
    
     document.getElementById("table_showResult").innerHTML += calcresult;
     
  }



  function  changeToNum(n){
     if(n==0)return 255;
     if(n==1)return 254;
     if(n==2)return 252;
     if(n==3)return 248;
     if(n==4)return 240;
     if(n==5)return 224;
     if(n==6)return 192;
     if(n==7)return 128;
     if(n==8)return 0;   
  }



     //for(var i=0; i<4; i++)            //计算广播地址
     //   broadcast_array[i] = network_array[i] & (~netmask_array[i]+256)
     //var broadcast_string = broadcast_array.join(".")


</script>

 

 

posted @ 2017-11-27 10:47  Khazix  阅读(859)  评论(0编辑  收藏  举报