1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| const fs = require('fs') let list = []
list = ["173.245.48.0/20","103.21.244.0/22","141.101.64.0/18","108.162.192.0/18","190.93.240.0/20","188.114.96.0/20","198.41.128.0/17","162.158.0.0/15","104.16.0.0/13","104.24.0.0/14","172.64.0.0/13"] let count = 0 let arr1 = [] list.map((v,i)=>{ arr1 = arr1.concat(init(list[i])) }) let arr2 = [] for(v of arr1) { arr2.push(v.join('.')) }
fs.writeFile(`./ips.json`,JSON.stringify(arr2),function(){ console.log("执行完成"); }) function init(ipmask){ let [ip,yanma] = ipmask.split('/') let morenyanma = new Array() for(let i = 0;i < 32;i++){ i < yanma ? morenyanma.push("1") : morenyanma.push("0") } morenyanma.splice(24,0,",") morenyanma.splice(16,0,",") morenyanma.splice(8,0,",") morenyanma = morenyanma.join('').split(',') ip = ip.split('.') ip.map((v,w)=>{ ip[w] = numToHex(v) }) let arr2 = [] for(let i = 0;i < ip.length;i ++){ arr2.push( parseInt(yu(ip[i],morenyanma[i]),2) ) } let output = [] for(let i = 0;i < Math.pow(2,32 - yanma)-2;i++){ arr2 = arr2.concat() digui(arr2,3) output.push(arr2) count ++; console.log("count:",count,"ip:",arr2); } return output }
function yu(a='11111111',b='00000000'){ var arr1 = [] for(let i = 0 ;i < a.length;i++){ arr1.push((a[i] == b[i]) ? a[i] : 0) } return arr1.join('') }
function numToHex(num){ let num1 = (num >>> 0).toString(2) let len = num1.length return len < 8 ? `${"0".repeat(8-len)}${num1}` : num1 }
function digui(ip,num){ if(num <= 1) return ip ip[num] += 1 if(ip[num] >= 256){ ip[num] = 0 digui(ip,num - 1) } return ip }
|