Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var readline = require('readline-sync');
  2. var os =require('os');
  3. var iface= os.networkInterfaces();
  4. var fs = require('fs');
  5. var sys = require('sys');
  6. var exec = require('child_process').exec;
  7.  
  8. var ipFromUser = process.argv[2];
  9. var ipTab=[];
  10. var ipTabBinary=[];
  11. var ip;
  12.  
  13.  
  14.  function getIPAddress() {
  15.     var interfaces = require('os').networkInterfaces();
  16.     for (var devName in interfaces) {
  17.         var iface = interfaces[devName];
  18.  
  19.         for (var i = 0; i < iface.length; i++) {
  20.             var alias = iface[i];
  21.             if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
  22.                 return alias.cidr;
  23.         }
  24.     }
  25.  
  26.     return '0.0.0.0';
  27. }
  28. function checkIpIfOk(){
  29.      if(ipTab.size>4){
  30.          return false;
  31.      }
  32.      if(!maskIndex){
  33.          return false}
  34.  
  35.     if(maskIndex[0]>32){
  36.         return false;
  37.     }
  38.     for(let i=0;i<5;i++)
  39.         if(ipTab[i]>255||ipTab<255){
  40.             return false;
  41.         }
  42.     return true;
  43. }
  44. function makeMaskBinary(maskNumber){
  45.     var tempString="";
  46.     for(let i =0;i<maskNumber;i++){
  47.         tempString+="1";
  48.     }
  49.     var buff = 32-maskNumber;
  50.     for(let i=0;i<buff;i++)
  51.     {
  52.         tempString+="0";
  53.  
  54.     }var  maskBinaryString = tempString.match(/.{1,8}/g);
  55.  
  56.  
  57.     return maskBinaryString;
  58. }
  59.  
  60.  
  61. function networkAdress(ip=[],mask=[]){
  62.  
  63.      var ipString=ip.join("");
  64.  
  65.      var maskString=mask.join("");
  66.  
  67.      var networkAdress="";
  68.  
  69.     for (let i=0;i<32;i++){
  70.         if(ipString[i]=="1"&&maskString[i]=="1"){
  71.             networkAdress+="1";
  72.         }
  73.         else{
  74.             networkAdress+="0";
  75.         }
  76.     }
  77.  
  78.     var networkAddresArray=networkAdress.match(/.{1,8}/g);
  79.     return networkAddresArray;
  80.  
  81. }
  82.  
  83.  
  84.  
  85. function broadcastAdress(maskBin=[], ip=[]){
  86.      var tempString = maskBin.join("");
  87.      var broadcastString="";
  88.  
  89.  
  90.      for(let i=0;i<32;i++) {
  91.          if (tempString[i] == "1") {
  92.              broadcastString += "0";
  93.          } else if (tempString[i] == "0") {
  94.              broadcastString += "1";
  95.          }
  96.      }
  97.  
  98.     var broadcastAdressArray=broadcastString.match(/.{1,8}/g);
  99.  
  100.  
  101.    var decArray=[4];
  102.    var decIp=[4];
  103.  
  104.    decArray[0] =parseInt(broadcastAdressArray[0],2);
  105.    decArray[1]=parseInt(broadcastAdressArray[1],2);
  106.    decArray[2]=parseInt(broadcastAdressArray[2],2);
  107.    decArray[3]=parseInt(broadcastAdressArray[3],2);
  108.  
  109.    decIp[0]=parseInt(ip[0],2);
  110.    decIp[1]=parseInt(ip[1],2);
  111.    decIp[2]=parseInt(ip[2],2);
  112.    decIp[3]=parseInt(ip[3],2);
  113.  
  114.  
  115.  
  116.    for(let i=0;i<4;i++){
  117.        decArray[i]+=decIp[i];
  118.    }
  119.  
  120.     for (let i = 0; i < 4; i++) {
  121.         broadcastAdressArray[i] = ("00000000" + decArray[i].toString(2)).substr(-8);
  122.     }
  123.  
  124.  
  125.     return broadcastAdressArray;
  126. }
  127.  
  128.  
  129. function binaryToDecimal(temp=[]){
  130.      var tempString= "";
  131.  
  132.      for(let i=0;i<4;i++){
  133.          tempString+=String(parseInt(temp[i],2));
  134.          if(i<3){
  135.              tempString+=".";
  136.          }
  137.      }
  138.      return tempString;
  139. }
  140.  
  141.  
  142. function ipClass(ip=[]){
  143.      if(parseInt(ip[0],2)<127){
  144.          console.log("Class A address");
  145.          return "A";
  146.      }
  147.      else if(parseInt(ip[0],2)>127&&parseInt(ip[0],2)<192&&parseInt(ip[1],2)<255){
  148.          console.log("Class B address");
  149.          return "B"
  150.      }
  151.      else if(parseInt(ip[0],2)>191&&parseInt(ip[1],2)<224&&parseInt(ip[2],2)<255){
  152.          console.log("Class C address");
  153.          return "C";
  154.      }
  155.      else if(parseInt(ip[0],2)>223&&parseInt(ip[0],2)<240&&parseInt(ip[3],2)<255){
  156.          console.log("Class D address");
  157.          return "D";
  158.      }
  159.      else{
  160.          console.log("Class E address");
  161.          return "E";
  162.      }
  163. }
  164.  
  165.  
  166. function isPrivate(ip=[]){
  167.      if(parseInt(ip[0],2)==10&&parseInt(ip[3],2)<256){
  168.          console.log("Private");
  169.          return true;
  170.      }
  171.      else if(parseInt(ip[0],2)==172&&parseInt(ip[1],2)>15&&parseInt(ip[1],2)<32){
  172.          console.log("Private");
  173.          return true;
  174.      }
  175.      else if(parseInt(ip[0],2)==192&&parseInt(ip[1],2)==168){
  176.          console.log("Private");
  177.          return true;
  178.      }
  179.  
  180.      else if(parseInt(ip[0],2)==127){
  181.          console.log("Local host");
  182.          return true;
  183.      }
  184.     else if(parseInt(ip[0],2)==255&&parseInt(ip[1],2)==255&&parseInt(ip[2],2)==255&&parseInt[ip[3],2]==255){
  185.          console.log("Broadcast");
  186.          return true;
  187.      }
  188.     else{
  189.      console.log("Public");
  190.      return false;
  191.     }
  192. }
  193.  
  194.  
  195. function firstHost(ip=[]){
  196.      var ipDec=[4];
  197.      ipDec[0]=parseInt(ip[0],2);
  198.      ipDec[1]=parseInt(ip[1],2);
  199.      ipDec[2]=parseInt(ip[2],2);
  200.      ipDec[3]=parseInt(ip[3],2);
  201.  
  202.      if(ipDec[3]<255){
  203.          ipDec[3]++;
  204.      }
  205.  
  206.      var hostArray=[4];
  207.     for (let i = 0; i < 4; i++) {
  208.         hostArray[i] = ("00000000" + ipDec[i].toString(2)).substr(-8);
  209.     }
  210.      return hostArray;
  211. }
  212.  
  213. function lastHost(ip=[]){
  214.     var ipDec=[4];
  215.     ipDec[0]=parseInt(ip[0],2);
  216.     ipDec[1]=parseInt(ip[1],2);
  217.     ipDec[2]=parseInt(ip[2],2);
  218.     ipDec[3]=parseInt(ip[3],2);
  219.  
  220.     if(ipDec[3]>0){
  221.         ipDec[3]--;
  222.     }
  223.  
  224.     var lastHostArray=[4];
  225.  
  226.     for (let i = 0; i < 4; i++) {
  227.         lastHostArray[i] = ("00000000" + ipDec[i].toString(2)).substr(-8);
  228.     }
  229.  
  230.     return lastHostArray;
  231.  
  232. }
  233.  
  234.  
  235. function maxHostsNumber(maskInd){
  236.      var pow = 32-maskInd;
  237.      return (Math.pow(2,pow))-2;
  238. }
  239.  
  240.  
  241. function isHostAdress(firstHost=[],lastHost=[],ip=[]){
  242.  
  243.      var firstHostDec=parseInt(firstHost[3],2);
  244.      var lastHostDec=parseInt(lastHost[3],2);
  245.      var ipDec=parseInt(ip[3],2);
  246.  
  247.      if(ipDec>firstHostDec&&ipDec<lastHostDec){
  248.          return true;
  249.      }
  250.      else{
  251.          return false;
  252.      }
  253.  
  254. }
  255.  
  256.  
  257.  
  258. //getting localIP  if not given and adding to array
  259. if(!ipFromUser){
  260.     var ipFromLocal=getIPAddress();
  261.     var ipAndMaskFromLocal=ipFromLocal.split("/");
  262.     xs2=ipAndMaskFromLocal[0];
  263.     maskIndex=ipAndMaskFromLocal[1];
  264.     ipTab=xs2.split(".");
  265.     ipTab.push(maskIndex);
  266. }
  267. //adding ip from user to array
  268. else{
  269.  
  270. for(let i=0;i<4;i++) {
  271.     var ipAndMask=ipFromUser.split("/");
  272.     xs2=ipAndMask[0];
  273.     maskIndex=ipAndMask[1];
  274.     ipTab=xs2.split(".");
  275.     ipTab.push(maskIndex);
  276.  
  277. }
  278. }
  279. //Float parsing
  280. for(let i=0;i<5;i++){
  281.     ipTab[i]=parseFloat(ipTab[i]);
  282. }
  283.  
  284.  
  285. if(checkIpIfOk()===false){
  286.     console.log("Podales zle IP, koniec programu");
  287.     return;
  288. }
  289. else {
  290.     // Changing IP to binary
  291.     for (let i = 0; i < 4; i++) {
  292.         ipTabBinary[i] = ("00000000" + ipTab[i].toString(2)).substr(-8);
  293.     }
  294.  
  295.  
  296.     console.log("Ip na ktorym pracuje");
  297.     if(ipFromUser){
  298.         console.log(ipFromUser);
  299.          ip=ipFromUser;
  300.     }
  301.     else {
  302.         console.log(ipFromLocal);
  303.          ip=ipFromLocal
  304.     }
  305.  
  306.  
  307.  
  308.     var ipDecimal=binaryToDecimal(ipTabBinary);
  309.  
  310.     console.log("Ip binary");
  311.     console.log(ipTabBinary);
  312.  
  313.     console.log("Mask binary");
  314.     var maskBinary = makeMaskBinary(parseInt(maskIndex))
  315.     console.log(maskBinary);
  316.  
  317.  
  318.     console.log("Mask decimal");
  319.     var maskDec=binaryToDecimal(makeMaskBinary(parseInt(maskIndex)));
  320.     console.log(maskDec);
  321.  
  322. var networkAddres=networkAdress(ipTabBinary,makeMaskBinary(parseInt(maskIndex)));
  323.  
  324. console.log("Network adres")
  325. console.log(networkAddres);
  326.  
  327. var networkAddresDec=binaryToDecimal(networkAddres);
  328.  
  329. console.log("Network adress dec");
  330. console.log(networkAddresDec);
  331.  
  332. var broadcastAddres = broadcastAdress(maskBinary,networkAddres);
  333. console.log("Broadcast adrress binary");
  334. console.log(broadcastAddres);
  335.  
  336. var broadcastAdressDec = binaryToDecimal(broadcastAddres);
  337. console.log("Broadcast adress decimal");
  338. console.log(broadcastAdressDec);
  339.  
  340. console.log("Ip class");
  341. var ipClassDec=ipClass(networkAddres);
  342.  
  343. var private = isPrivate(networkAddres);
  344.  
  345. var maxHosts=maxHostsNumber(maskIndex);
  346.  
  347.  
  348. var firstHost = firstHost(networkAddres);
  349. var firstHostDec=binaryToDecimal(firstHost);
  350.  
  351. var lastHost = lastHost(broadcastAddres);
  352. var lastHostDec=binaryToDecimal(lastHost);
  353.  
  354. console.log("First host");
  355. console.log(firstHost);
  356. console.log(firstHostDec);
  357.  
  358. console.log("Last host");
  359. console.log(lastHost);
  360. console.log(lastHostDec);
  361.  
  362.     console.log("Max host number");
  363.     console.log(maxHostsNumber(maskIndex));
  364.  
  365.  
  366. fs.writeFileSync("result.txt",ip +'\r\n'+ "Ip Binary"+'\r\n'+ ipTabBinary +'\r\n'+ "Mask binary" +'\r\n'+maskBinary+'\r\n'+"Mask Decimal"+'\r\n'+maskDec+'\r\n'+"Network address"+'\r\n'+networkAddres+'\r\n'+"Network address decimal" +'\r\n'+networkAddresDec +'\r\n'+"Broadcast address"+'\r\n'+broadcastAddres+'\r\n'+"Broadcast address decimal"+'\r\n'+broadcastAdressDec+'\r\n'+ "Address class"+'\r\n'+ipClassDec+'\r\n'+"Is private?"+'\r\n'+private+'\r\n'+"First host"+'\r\n'+firstHostDec+'\r\n'+"Last host"+'\r\n'+lastHostDec+'\r\n'+"Max Hosts number"+'\r\n'+maxHosts);
  367.  
  368.  
  369. if(isHostAdress(firstHost,lastHost,ipTabBinary)){
  370.     var answear = readline.question("Do you want to ping address");
  371.  
  372.     if(answear=="Y"){
  373.         function puts(error, stdout, stderr) { sys.puts(stdout) }
  374.         exec("ping "+ipDecimal, puts);
  375.     }
  376. }
  377.  
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement