Post

Subnetrechner per API nutzen

Um einfach mal eine bestimmte IP-Adresse einem Subnet zuzuordnen, bedienen wir uns einfach einer frei verfügbaren API im Internet. Einfach die IP-Adresse in CIDR Notation z.B. per curl an die API schicken.

1
curl https://networkcalc.com/api/ip/10.0.1.15/27

Das Ergebnis ist dann eine Antwort in JSON:

1
{"status":"OK","meta":{"permalink":"https://networkcalc.com/subnet-calculator/10.0.1.15/27","next_address":"https://networkcalc.com/api/ip/10.0.1.16/27"},"address":{"cidr_notation":"10.0.1.15/27","subnet_bits":27,"subnet_mask":"255.255.255.224","wildcard_mask":"0.0.0.31","network_address":"10.0.1.0","broadcast_address":"10.0.1.31","assignable_hosts":30,"first_assignable_host":"10.0.1.1","last_assignable_host":"10.0.1.30"}}

Wer das etwas lesbarer haben möchte kann die Ausgabe auch in eine Datei umleiten:

1
curl https://networkcalc.com/api/ip/10.0.1.15/27 > ip.txt

Und danach mit dem Tool jq auswerten (auf macOS vorher z.B. mit Homebrew und dem Kommando `brew install jq’ installieren):

1
2
3
4
5
6
7
8
cat ip.txt | jq '.address.cidr_notation'  
"10.1.101.0/28"
cat ip.txt | jq '.address.subnet_mask'                     
"255.255.255.240"
cat ip.txt | jq '.address.first_assignable_host'
"10.1.101.1"
cat ip.txt | jq '.address.last_assignable_host'
"10.1.101.14"

Da JSON wie XML hierarchisch auf gebaut ist gehen wir mit .address in den dritten Abschnitt (1. = .status, 2. = .meta, 3. = .address) und holen uns dort das Objekt .cidr_notation. Da wir das ja bereits kennen, weil wir das ja beim curl erfasst haben, wäre zum Beispiel die Subnetmaske interessant (die Bit kennen wir bereits).

Das ganze geht natürlich auch mit PowerShell:

1
(Invoke-RestMethod https://networkcalc.com/api/ip/10.0.1.15/27).address

Ausgabe der API:

1
2
3
4
5
6
7
8
9
10
11
PS /Users/thomas> (Invoke-RestMethod https://networkcalc.com/api/ip/10.0.1.15/27).address

cidr_notation         : 10.0.1.15/27
subnet_bits           : 27
subnet_mask           : 255.255.255.224
wildcard_mask         : 0.0.0.31
network_address       : 10.0.1.0
broadcast_address     : 10.0.1.31
assignable_hosts      : 30
first_assignable_host : 10.0.1.1
last_assignable_host  : 10.0.1.30
This post is licensed under CC BY 4.0 by the author.