export const page = { title: 'Subnetz Rechner', template: () => `

IP Subnetz Rechner

Beispiel-Subnetze (Private Adressbereiche)

Bereich CIDR Subnetzmaske Beschreibung Aktion
192.168.0.0 – 192.168.255.255 /16 (Gesamt) 255.255.0.0 Klasse C (oft als /24 genutzt) Beispiel /24
172.16.0.0 – 172.31.255.255 /12 (Gesamt) 255.240.0.0 Klasse B Beispiel /16
10.0.0.0 – 10.255.255.255 /8 (Gesamt) 255.0.0.0 Klasse A Beispiel /8

Klicken Sie auf "Beispiel", um die Felder auszufüllen und die Berechnung zu starten.

`, init() { const form = document.getElementById('subnet-form'); const ipInput = document.getElementById('ip-address'); const cidrInput = document.getElementById('cidr'); const errorEl = document.getElementById('subnet-error'); const resultsEl = document.getElementById('results'); function showInlineError(msg) { errorEl.textContent = msg; errorEl.classList.toggle('hidden', !msg); } function isValidIP(ip) { return /^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$/.test(ip); } function ipToBinary(ip) { return ip.split('.').map(o => parseInt(o, 10).toString(2).padStart(8, '0')).join(''); } function binaryToIp(b) { const parts = []; for (let i = 0; i < 32; i += 8) parts.push(parseInt(b.slice(i, i + 8), 2)); return parts.join('.'); } function cidrToMask(cidr) { return binaryToIp('1'.repeat(cidr) + '0'.repeat(32 - cidr)); } function maskToCidr(mask) { const b = ipToBinary(mask); if (/^1*0*$/.test(b)) return b.replace(/0+$/, '').length; return null; } function calculate() { showInlineError(null); const ip = ipInput.value.trim(); const cidrRaw = cidrInput.value.trim(); if (!isValidIP(ip)) { showInlineError('Bitte eine gültige IPv4-Adresse eingeben.'); return; } let cidr, mask; if (cidrRaw.includes('.')) { if (!isValidIP(cidrRaw)) { showInlineError('Bitte eine gültige Subnetzmaske eingeben.'); return; } cidr = maskToCidr(cidrRaw); if (cidr === null) { showInlineError('Ungültige Subnetzmaske — muss eine kontinuierliche Folge von Einsen sein (z.B. 255.255.255.0).'); return; } mask = cidrRaw; } else { cidr = parseInt(cidrRaw, 10); if (isNaN(cidr) || cidr < 0 || cidr > 32) { showInlineError('Bitte einen gültigen CIDR-Wert (0–32) eingeben.'); return; } mask = cidrToMask(cidr); } const ipBin = ipToBinary(ip); const maskBin = '1'.repeat(cidr) + '0'.repeat(32 - cidr); let netBin = ''; for (let i = 0; i < 32; i++) netBin += (parseInt(ipBin[i]) & parseInt(maskBin[i])).toString(); const hostBits = 32 - cidr; const bcBin = netBin.slice(0, cidr) + '1'.repeat(hostBits); const netNum = parseInt(netBin, 2); const bcNum = parseInt(bcBin, 2); let hosts, first, last; if (hostBits >= 2) { hosts = Math.pow(2, hostBits) - 2; first = binaryToIp((netNum + 1).toString(2).padStart(32, '0')); last = binaryToIp((bcNum - 1).toString(2).padStart(32, '0')); } else if (cidr === 31) { hosts = 2; first = binaryToIp(netBin); last = binaryToIp(bcBin); } else { hosts = 1; first = binaryToIp(netBin); last = binaryToIp(netBin); } document.getElementById('network-address').textContent = binaryToIp(netBin); document.getElementById('broadcast-address').textContent = binaryToIp(bcBin); document.getElementById('subnet-mask').textContent = mask; document.getElementById('host-count').textContent = hosts.toLocaleString(); document.getElementById('first-host').textContent = first; document.getElementById('last-host').textContent = last; resultsEl.classList.remove('hidden'); } form.addEventListener('submit', e => { e.preventDefault(); calculate(); }); document.querySelectorAll('.example-link').forEach(link => { link.addEventListener('click', () => { ipInput.value = link.dataset.ip; cidrInput.value = link.dataset.cidr; calculate(); window.scrollTo({ top: 0, behavior: 'smooth' }); }); }); } };