Files
2026-04-17 09:01:34 -05:00

61 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
# Funktion zum Konvertieren einer Dezimalzahl in eine Binärzahl
function decimal_to_binary() {
local decimal=$1
printf "%08d" $(echo "obase=2; $decimal" | bc)
}
# Funktion zur Berechnung des Netzanteils und Hostanteils
function calculate_network_and_host() {
local ip_address=$1
local subnet_mask=$2
# IP-Adresse und Subnetzmaske in ihre Teile zerlegen
IFS='.' read -r -a ip_parts <<< "$ip_address"
IFS='.' read -r -a mask_parts <<< "$subnet_mask"
# Arrays für Netz- und Hostanteil initialisieren
local network_parts=()
local host_parts=()
# Netz- und Hostanteil berechnen
for i in {0..3}; do
network_parts[i]=$((${ip_parts[i]} & ${mask_parts[i]}))
host_parts[i]=$((${ip_parts[i]} & (~${mask_parts[i]} & 255)))
done
# Ausgabe der Ergebnisse
echo "IP-Adresse: $ip_address"
echo "Subnetzmaske: $subnet_mask"
echo -n "Netzanteil: "
printf "%d." "${network_parts[@]}"
echo
echo -n "Netzanteil (Binär): "
for part in "${network_parts[@]}"; do
echo -n "$(decimal_to_binary $part) "
done
echo
echo -n "Hostanteil: "
printf "%d." "${host_parts[@]}"
echo
echo -n "Hostanteil (Binär): "
for part in "${host_parts[@]}"; do
echo -n "$(decimal_to_binary $part) "
done
echo
}
# Überprüfen, ob die notwendigen Argumente übergeben wurden
if [ $# -ne 2 ]; then
echo "Verwendung: $0 <IP-Adresse> <Subnetzmaske>"
exit 1
fi
# Hauptfunktion aufrufen
calculate_network_and_host "$1" "$2"