// 验证IP的正则 var ip_reg = /^(25[0-5]|2[0-4][0-9]|[01]"", ip_arr = ip.split("."); for (var i = 0; i < 4; i++) { curr_num = ip_arr[i]; number_bin = parseInt(curr_num); number_bin = number_bin.toString(2); count = 8 - number_bin.length; for (var j = 0; j < count; j++) { number_bin = "0" + number_bin; } ip_str += number_bin; } return ip_str; } return ''; } /*** 把二进制格式转换成IP地址* @param string binary 待转换的二进制 */ function binary_to_ip(binary) { if (binary.length == 32) { a = parseInt(binary.substr(0, 8), 2); b = parseInt(binary.substr(8, 8), 2); c = parseInt(binary.substr(16, 8), 2); d = parseInt(binary.slice(-8), 2); return a + '.' + b + '.' + c + '.' + d; } return ''; } /*** 根据子网掩码和网关计算网络地址和广播地址* @param string mask 子网掩码* @param string gateway 网关*/ function get_network_broadcast_addr(mask, gateway) { network_broadcast = []; network_addr = ""; mask_arr = mask.split("."); ip_arr = ip.split("."); // 计算IP的网络地址 与(&)运算 for (var i = 0; i < 4; i++) { number1 = parseInt(mask_arr[i]); number2 = parseInt(ip_arr[i]); network_addr += number1 & number2; if( i < 3 ){ network_addr += "."; } } network_broadcast.push(network_addr); // 计算广播地址 // 子掩码后面有几个0,就去掉IP地址后几位再补1 mask_binary = ip_to_binary(mask); gateway_binary = ip_to_binary(ip); mask_zero = mask_binary.split(0).length - 1; one_number = new Array(mask_zero + 1).join('1'); // IP地址后位补1 gateway_hou_wei_bu_yi = gateway_binary.slice(0, -mask_zero) + one_number; network_broadcast.push(binary_to_ip(gateway_hou_wei_bu_yi)); return network_broadcast; } // 全排列组合算法(两两递归组合) function doExchange(doubleArrays) { var len = doubleArrays.length; if(len >= 2){ var len1 = doubleArrays[0].length; var len2 = doubleArrays[1].length; var newlen = len1 * len2; var temp = new Array(newlen); var index = 0; for(var i = 0; i < len1; i++){ for(var j = 0; j < len2; j++){ temp[index] = doubleArrays[0][i] + '.' + doubleArrays[1][j]; index++; } } var newArray = new Array(len - 1); for(var i = 2; i < len; i++){ newArray[i - 1] = doubleArrays[i]; } newArray[0] = temp; return doExchange(newArray); } else{ return doubleArrays[0]; } } /*** 获取由网络地址和广播址组成的所有IP组合* @param string network_addr 网络地址* @param string broadcast_addr 广播地址* @param string gateway 网关*/ function return_ip(network_addr, broadcast_addr, gateway) { range = []; start = network_addr.split("."); end = broadcast.split("."); // range格式为[[192], [168], [0,1,2...254], [0,1,2...254]] for (var i = 0; i < 4; i++) { if (start[i] == end[i]) { range[i] = [start[i]]; } else { min = Math.min(start[i], end[i]); max = Math.max(start[i], end[i]); temp = []; if (i == 3) min = 199; // 从200起计 for (var j = min; j <= max; j++) { temp.push(j); } range[i] = temp; } } ip_list = doExchange(range); ip_list.shift(); // 去掉网络地址 ip_list.pop(); // 去掉广播地址 gateway_index = -1; // 去掉网关 for (var k = 0; k < ip_list.length; k++) { if (ip_list[k] == gateway) { gateway_index = k; break; } } if (gateway_index > -1) { ip_list.splice(gateway_index, 1); } return ip_list; }
PS:这里再为大家推荐几款相关在线工具供大家参考:
在线网络计算器|TCP/IP子网掩码计算与换算工具:
http://tools.jb51.net/aideddesign/ipcalc
在线IP地址/子网掩码计算与转换工具:
http://tools.jb51.net/aideddesign/ip_net_calc
在线子网掩码换算与网络计算工具:
http://tools.jb51.net/aideddesign/network_calc
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“JS 根据子网掩码,网关计算出所有IP地址范围示例”评论...