Skip to content

Instantly share code, notes, and snippets.

@Apsu
Last active April 10, 2023 01:48
Show Gist options
  • Save Apsu/8799432 to your computer and use it in GitHub Desktop.
Save Apsu/8799432 to your computer and use it in GitHub Desktop.
/etc/network/interfaces parser
# Comment lo
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 162.242.220.169
gateway 162.242.220.1
netmask 255.255.255.0
iface eth0 inet6 static
address 2001:4802:7801:0102:adbb:e94c:ff20:3cbe
dns-nameservers 69.20.0.164 69.20.0.196
gateway fe80::def
netmask 64
auto eth1
iface eth1 inet static
# Comment nested
address 10.176.129.214
dns-nameservers 69.20.0.164 69.20.0.196
netmask 255.255.224.0
post-up route add -net 10.208.0.0 netmask 255.240.0.0 gw 10.176.128.1 || true
pre-down route del -net 10.208.0.0 netmask 255.240.0.0 gw 10.176.128.1 || true
post-up route add -net 10.176.0.0 netmask 255.240.0.0 gw 10.176.128.1 || true
pre-down route del -net 10.176.0.0 netmask 255.240.0.0 gw 10.176.128.1 || true
auto eth2
iface eth2 inet static
address 172.20.0.3
netmask 255.255.255.0
auto eth3
iface eth3 inet manual
up ip link set $IFACE up
down ip link set $IFACE down
# Comment lo
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 162.242.220.169
gateway 162.242.220.1
netmask 255.255.255.0
iface eth0 inet6 static
address 2001:4802:7801:0102:adbb:e94c:ff20:3cbe
dns-nameservers 69.20.0.164 69.20.0.196
gateway fe80::def
netmask 64
auto eth1
iface eth1 inet static
# Comment nested
address 10.176.129.214
dns-nameservers 69.20.0.164 69.20.0.196
netmask 255.255.224.0
post-up route add -net 10.208.0.0 netmask 255.240.0.0 gw 10.176.128.1 || true
pre-down route del -net 10.208.0.0 netmask 255.240.0.0 gw 10.176.128.1 || true
post-up route add -net 10.176.0.0 netmask 255.240.0.0 gw 10.176.128.1 || true
pre-down route del -net 10.176.0.0 netmask 255.240.0.0 gw 10.176.128.1 || true
auto eth2
iface eth2 inet static
address 172.20.0.3
netmask 255.255.255.0
auto lxb-mgmt
iface lxb-mgmt inet manual
up ip link set $IFACE up
down ip link set $IFACE down
pre-up ip link add name phy-lxb-mgmt type veth peer name ovs-lxb-mgmt || true
bridge_ports eth3 phy-lxb-mgmt
#!/usr/bin/env python
from __future__ import print_function
class Interfaces():
"Manage /etc/network/interfaces file"
def __init__(self):
# Open file
fd = open("interfaces")
# Make iterator of contents
self.iterator = iter(
[
line.strip()
for line in fd.read().splitlines()
if line.strip()
]
)
# Close file
fd.close()
# Parsed directives
self.directives = []
# Do the needful
self.parse()
def parse(self):
"Parse super- and sub-directives, and return nested results"
# For each directive
for directive in self.iterator:
# If we're on a super, start sub loop
while directive.startswith(("iface", "mapping")):
sup = None # Clear super-directive
subs = [] # Clear sub-directives
# For each sub-directive
for sub in self.iterator:
# If sub is actually a super
if sub.startswith(
(
"auto",
"allow-",
"iface",
"mapping",
"source"
)
):
sup = sub # Set new super
break # Exit sub loop
# Else it's just a sub, so add it
else:
subs.append(sub)
# If we found subs, store them
if subs:
self.directives.append([directive, subs])
# Else just store directive
else:
self.directives.append([directive])
# If we didn't find a super, return
if not sup:
return
directive = sup # Store super for next inner loop check
# Not a super here so just add directive
self.directives.append([directive])
# End of iterator, return
return
# TODO: -> save
def prettyPrint(self):
"Pretty-print interface directives"
for directive in self.directives:
# Print directive
print(directive[0])
# If has subs
if len(directive) > 1:
# Print indented subs
for sub in directive[1]:
print(" {}".format(sub))
# If super, add a blank line for spacing
if directive[0].startswith(("iface", "mapping")):
print()
def swapDirective(self, one, two):
"Swap one directive with another"
for index, directive in enumerate(self.directives):
if one in directive[0]:
self.directives[index][0] = directive[0].replace(one, two)
def addSubs(self, sup, subs):
"Add sub-directives to super-directive"
for index, directive in enumerate(self.directives):
if sup == directive[0] and len(directive) > 1:
self.directives[index][1].extend(subs)
interfaces = Interfaces()
interfaces.swapDirective("eth3", "lxb-mgmt")
interfaces.addSubs(
"iface lxb-mgmt inet manual",
[
"pre-up ip link add name phy-lxb-mgmt "
"type veth peer name ovs-lxb-mgmt || true",
"bridge_ports eth3 phy-lxb-mgmt"
]
)
interfaces.prettyPrint()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment