def _configure_interface_address(self, interface, address, default_route=None):
net = ipaddress.ip_interface(address)
if default_route:
try:
next_hop = ipaddress.ip_address(default_route)
except ValueError:
self.logger.error("next-hop address {} could not be parsed".format(default_route))
sys.exit(1)
if default_route and next_hop not in net.network:
self.logger.error("next-hop address {} not in network {}".format(next_hop, net))
sys.exit(1)
subprocess.check_call(["ip", "-{}".format(net.version), "address", "add", str(net.ip) + "/" + str(net.network.prefixlen), "dev", interface])
if next_hop:
try:
subprocess.check_call(["ip", "-{}".format(net.version), "route", "del", "default"])
except:
pass
subprocess.check_call(["ip", "-{}".format(net.version), "route", "add", "default", "dev", interface, "via", str(next_hop)])
评论列表
文章目录