-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
72 lines (60 loc) · 2.49 KB
/
script.js
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
$(document).ready(function () {
$("#calculate").click(function () {
let ipAddress = $("#ipAddress").val().trim();
let subnetMask = $("#subnetMask").val().trim();
// Classify IP address and update analysis results
classifyIPAddress(ipAddress);
// Update the subnet mask in binary format
$(".subnetMaskBinary").text(toBinary(subnetMask));
$("#ipInBinary").text(toBinary(ipAddress));
// Get the network bits from the span
let networkBits = parseInt($("#NetworkBits").text());
// Calculate and update additional analysis information
calculateAdditionalInfo(subnetMask, networkBits);
});
function classifyIPAddress(ipAddress) {
let firstOctet = parseInt(ipAddress.split(".")[0]);
let addressRange = "";
let ipClass = "";
let NetworkBits = "";
if (firstOctet >= 1 && firstOctet <= 126) {
addressRange = "1-126";
ipClass = "A";
NetworkBits = "8";
} else if (firstOctet >= 128 && firstOctet <= 191) {
addressRange = "128-191";
ipClass = "B";
NetworkBits = "16";
} else if (firstOctet >= 192 && firstOctet <= 223) {
addressRange = "192-223";
ipClass = "C";
NetworkBits = "24";
} else {
addressRange = "Not in the defined ranges";
ipClass = "N/A";
NetworkBits = "N/A";
}
$("#addressRange").text(addressRange);
$("#ipClass").text(ipClass);
$("#NetworkBits").text(NetworkBits);
}
function toBinary(subnetMask) {
// Convert each octet of the subnet mask to binary and join them
let binaryMask = subnetMask.split(".")
.map(octet => (parseInt(octet).toString(2)).padStart(8, '0'))
.join(".");
return binaryMask;
}
function calculateAdditionalInfo(subnetMask, networkBits) {
let prefixBits = subnetMask.split(".").map(octet => parseInt(octet).toString(2).padStart(8, '0')).join("").indexOf("0");
let subnetBits = prefixBits - networkBits;
let hostBits = 32 - prefixBits;
let possibleSubnetsNo = Math.pow(2, subnetBits);
let possibleHostsNo = Math.pow(2, hostBits) - 2;
$("#prefixBits").text(prefixBits);
$("#subnetBits").text(subnetBits);
$("#hostBits").text(hostBits);
$("#possibleSubnetsNo").text(possibleSubnetsNo);
$("#possibleHostsNo").text(possibleHostsNo);
}
});