it-swarm.com.de

Wie kann ich die Netzwerk- und Broadcast-Adresse anhand der IP-Adresse und der Subnetzmaske ermitteln?

Beispielsweise:

  • IP Adresse: 130.45.34.36
  • Maske: 255.255.240.0

Was wäre die Net ID/Subnet Address und die Broadcast Address?

16
Apprentice

Lassen Sie uns beide in Binärform schreiben:

130.45.34.36 = 10000010.00101101.00100010.00100100

255.255.240.0 = 11111111.11111111.11110000.00000000

Ein bitweises [~ # ~] und [~ # ~] zwischen den beiden würde uns die Netzwerkadresse geben:

10000010.00101101.00100010.00100100   (ip address)
AND
11111111.11111111.11110000.00000000   (subnet mask)
=
10000010.00101101.00100000.00000000 = 130.45.32.0 (the resulting network address)

Eine bitweise [~ # ~] oder [~ # ~] zwischen der Netzwerkadresse und der invertierten Subnetzmaske würde uns geben die Broadcast-Adresse:

10000010.00101101.00100000.00000000   (netadress)
OR
00000000.00000000.00001111.11111111   (inverted subnet mask)
=
10000010.00101101.00101111.11111111 = 130.45.47.255 (broadcast address)
48
Malt
var network = calculateNetworkIP("192.168.0.101", "255.255.255.0");

var broadcast = calculateBroadcastIP("192.168.0.101", "255.255.255.0");

function calculateNetworkIP(ipAddress, maskIP){

    var binaryIP = convertIPToBinaryIP(ipAddress);
    var maskBinaryIP = convertIPToBinaryIP(maskIP);

    var binaryNetwork = [];
    for (var j = 0; j < maskBinaryIP.length; j++) {
        binaryNetwork.Push(bitwiseAND(binaryIP[j], maskBinaryIP[j]));
    }

    var NetworkIPArr = convertBinaryIPToDecIP(binaryNetwork);

    var NetworkIPStr = "";
    for (var k = 0; k < NetworkIPArr.length; k++) {
        NetworkIPStr += NetworkIPArr[k]+".";
    }
    return NetworkIPStr.slice(0, -1);
}

function calculateBroadcastIP(ipAddress, maskIP){

    var binaryIP = convertIPToBinaryIP(ipAddress);
    var maskBinaryIP = convertIPToBinaryIP(maskIP);
    var invertedMark = [];
    for (var i = 0; i < maskBinaryIP.length; i++) {
        invertedMark.Push(invertedBinary(maskBinaryIP[i]));
    }

    var binaryBroadcast = [];
    for (var j = 0; j < maskBinaryIP.length; j++) {
        binaryBroadcast.Push(bitwiseOR(binaryIP[j], invertedMark[j]));
    }

    var broadcastIPArr = convertBinaryIPToDecIP(binaryBroadcast);

    var broadcastIPStr = "";
    for (var k = 0; k < broadcastIPArr.length; k++) {
        broadcastIPStr += broadcastIPArr[k]+".";
    }
    return broadcastIPStr.slice(0, -1);
}

function invertedBinary(number){

    var no = number+"";
    var noArr = no.split("");
    var newNo = "";
    for(var i = 0; i < noArr.length; i++){
        if(noArr[i] == "0"){
            newNo += "1";
        }else{
            newNo += "0";
        }
    }
    return newNo;
}

function bitwiseAND(firstBinary, secondBinary){

    var firstArr = [];
    var secondArr = [];
    firstArr = firstBinary.split("");
    secondArr = secondBinary.split("");
    var newAdded = "";
    for(var i = 0; i < firstArr.length; i++){
        if(firstArr[i]+"+"+secondArr[i] == "1+0"){
            newAdded += "0";
        }else if(firstArr[i]+"+"+secondArr[i] == "0+1"){
            newAdded += "0";
        }else if(firstArr[i]+"+"+secondArr[i] == "1+1"){
            newAdded += "1";
        }else if(firstArr[i]+"+"+secondArr[i] == "0+0"){
            newAdded += "0";
        }
    }
    return newAdded;
}

function bitwiseOR(firstBinary, secondBinary){

    var firstArr = [];
    var secondArr = [];
    firstArr = firstBinary.split("");
    secondArr = secondBinary.split("");
    var newAdded = "";
    for(var i = 0; i < firstArr.length; i++){
        if(firstArr[i]+"+"+secondArr[i] == "1+0"){
            newAdded += "1";
        }else if(firstArr[i]+"+"+secondArr[i] == "0+1"){
            newAdded += "1";
        }else if(firstArr[i]+"+"+secondArr[i] == "1+1"){
            newAdded += "1";
        }else if(firstArr[i]+"+"+secondArr[i] == "0+0"){
            newAdded += "0";
        }
    }
    return newAdded;
}

function convertBinaryIPToDecIP(binaryIPArr){

    var broadcastIP = [];
    for (var i = 0; i < binaryIPArr.length; i++) {
        broadcastIP.Push(parseInt(parseInt(binaryIPArr[i]), 2));
    }
    return broadcastIP;
}

function convertIPToBinaryIP(ipAddress) {

    var ipArr = ipAddress.split(".");
    var binaryIP = [];
    for (var i = 0; i < ipArr.length; i++) {
        var binaryNo = parseInt(ipArr[i]).toString(2);
        if(binaryNo.length == 8){
            binaryIP.Push(binaryNo);
        }else{
            var diffNo = 8 - binaryNo.length;
            var createBinary = '';
            for (var j = 0; j < diffNo; j++) {
               createBinary += '0';
            }
            createBinary += binaryNo;
            binaryIP.Push(createBinary);
        }
    }
   return binaryIP; 
}
2

Eine weitere Abkürzung für die Berechnung der Broadcast-Adresse nach dem Abrufen der Netzwerkadresse lautet:

  1. berechne die Gesamtzahl der Hosts (in diesem Fall ist es 2 ^ 12 = 4096)

  2. Teilen Sie es durch 256 (in diesem Fall ist es 16) und addieren Sie das Ergebnis - 1 (in diesem Fall 15) in * das entsprechende Oktett (in diesem Fall das zweite Oktett, d. H. 32 + 15 = 47) und machen Sie ein anderes Oktett 255

* Wir können das entsprechende Oktett erhalten, indem wir uns die Anzahl der Hosts ansehen. Wenn beispielsweise die Anzahl der Hosts größer als 256 ist, müssen wir sie zum 2. Oktett der Netzwerkadresse hinzufügen und so weiter

Codebeispiel basierend auf Malt's Antwort:

const
    ipadr = '130.45.34.36',
    subnet = '255.255.240.0',
    ipadrs = ipadr.split('.'),
    subnets = subnet.split('.');

let networks = [],
    broadcasts = [];

for (let i in ipadrs) {
    networks[i] = ipadrs[i] & subnets[i];
}

console.log('netaddress: ', networks.join('.')) // netaddress:  130.45.32.0

for (let i in networks) {
    broadcasts[i] = networks[i] | ~subnets[i] + 256;
}

console.log('broadcast address: ', broadcasts.join('.')) // broadcast address:  130.45.47.255
0
Ben