===== DHCP-/DNS-Admin VLAN Dateien erstellen ===== Erweiterung mit ipaddress-Modul möglich: [[https://docs.python.org/3/howto/ipaddress.html]] Iterating through the “usable” addresses on a network: >>> net4 = ipaddress.ip_network('192.0.2.0/24') >>> for x in net4.hosts(): ... print(x) 192.0.2.1 192.0.2.2 192.0.2.3 192.0.2.4 ... 192.0.2.252 192.0.2.253 192.0.2.254 #!/usr/bin/env python3 # -*- coding: utf-8 -*- # # dda_vlan_macher.py print (""" === Erstellen einer neuen VLAN Datei === \n\n """) while True: vlan = input("Welches VLAN soll erstellt werden? Bitte Nr. eingeben: ") try: i_vlan = int(vlan) except ValueError: print("Keine Nummer! Bitte nur Zahlen eingeben ...") # Alles in Ordnung: else: vlan = "{:04d}".format(i_vlan) # Eingabe: 200 -> 0200 break devices = input("Präfix für Geräte. Bitte die ersten zwei Stellen eingeben: ") ## VLAN Konfigurations Datei zum schreiben öffnen with open('vlans/vlan{}.in'.format(vlan), 'w') as vlanconf: # "0200" -> 200 s_vlan = str(int(vlan)) if len(s_vlan) > 3: # wenn 1400 -> 14 s_vlan = s_vlan[:2] else: s_vlan = s_vlan[0] for zaehler in range(1, 50): s_devices = devices + "{:03d}".format(zaehler) ip = "172.16.{}.{}".format(s_vlan, zaehler) mac = "{:02d}:22:33:44:55:66".format(zaehler) vlanconf.writelines("{} {} {}\n".format(s_devices, mac, ip))