restart individual network interface on change

+ use prefix 24 instead of 31
This commit is contained in:
2021-12-14 09:29:37 +01:00
parent e2acd8030d
commit 1deb91dbbb

View File

@ -203,18 +203,22 @@ def create_if(name, cfg, mac, dhcp_server_cfg):
result['BOOTPROTO']='dhcp'
# default: all <= 192.0.0.0
# others have to be added explicitly
dhcp_server_cfg.append((('0.0.0.0','128.0.0.0'), []))
dhcp_server_cfg.append((('128.0.0.0','192.0.0.0'), []))
dhcp_server_cfg.append(('0.0.0.0/128.0.0.0', []))
dhcp_server_cfg.append(('128.0.0.0/192.0.0.0', []))
for nw in cfg.split(',')[1:]:
nw = IPv4Interface(cfg).network
dhcp_server_cfg.append((nw.with_netmask.split('/'), []))
else:
cfgip = IPv4Interface(cfg)
network = cfgip.network
if network.prefixlen == 32:
otherip = IPv4Interface('%s/31' % cfgip.ip)
if network.prefixlen == 32: # or no prefix specified
otherip = IPv4Interface('%s/24' % cfgip.ip)
network = otherip.network
cfgip = network.network_address + (1 - int(otherip) % 2)
if str(cfgip.ip).endswith('.1'):
cfgip = network.network_address + 2
else:
cfgip = network.network_address + 1
print(name, network.network_address, cfgip, otherip.ip)
dhcp_server_cfg.append((network.with_netmask, [(str(otherip.ip), str(otherip.ip))]))
result['IPADDR'] = str(cfgip)
else: # subnet with multiple adresses -> static adresses only. dhcp range not yet implemented
@ -344,13 +348,13 @@ def handle_config():
show.dirty = True
if cfgfile is None:
return False
to_start = {}
ifname = ''
parser = ConfigParser()
dh = []
dhcp_server_cfg = []
try:
parser.read(cfgfile)
network = dict(parser['NETWORK'])
dhcp_server_cfg = []
for ifname in IFNAMES:
content = create_if(ifname, network.pop(ifname, 'off'), netaddr_dict[ifname], dhcp_server_cfg)
content = '\n'.join('%s=%s' % kv for kv in content.items())
@ -358,19 +362,25 @@ def handle_config():
if todo and more_info == 'NONE':
print('change', ifname)
show.dirty = True
if dh:
to_start[ifname] = 'if_restart'
if dhcp_server_cfg:
content = [DHCP_HEADER]
for subnet, rangelist in dhcp_server_cfg:
dh.append('subnet %s netmask %s {\n' % subnet)
dh.append(' option netmask %s;\n' % subnet[1])
adr, mask = subnet.split('/')
content.append('subnet %s netmask %s {\n' % (adr, mask))
#content.append(' option netmask %s;\n' % mask)
for rng in rangelist:
dh.append(' range %s %s;\n' % rng)
dh.append('}\n')
content.append(' range %s %s;\n' % rng)
content.append('}\n')
content = ''.join(content)
todo = write_when_new('/etc/dhcp/dhcpd.conf', content)
if todo:
print('change ', ifname)
print('change dhcpd.conf')
to_start['dhcpd'] = 'restart'
show.dirty = True
elif doit:
unix_cmd('systemctl stop dhcpd')
unix_cmd('systemctl disable dhcpd')
content = []
dirty = False
for section in parser.sections():
@ -389,7 +399,6 @@ def handle_config():
raise
return False
to_start = {}
reload_systemd = False
for service, template_func in SERVICES.items():
section = service.upper()
@ -428,9 +437,13 @@ def handle_config():
unix_cmd('systemctl daemon-reload')
for service, action in to_start.items():
show.dirty = True
if action == 'restart':
unix_cmd('systemctl restart %s' % service)
unix_cmd('systemctl enable %s' % service)
if action == 'if_restart':
unix_cmd('ifdown %s' % service)
unix_cmd('ifup %s' % service)
else:
if action == 'restart':
unix_cmd('systemctl restart %s' % service)
unix_cmd('systemctl enable %s' % service)
return True