diff --git a/cloudinit/distros/bsd.py b/cloudinit/distros/bsd.py index 4664762487a..60490c8a794 100644 --- a/cloudinit/distros/bsd.py +++ b/cloudinit/distros/bsd.py @@ -39,7 +39,7 @@ def __init__(self, name, cfg, paths): cfg["ssh_svcname"] = "sshd" cfg["rsyslog_svcname"] = "rsyslogd" self.osfamily = platform.system().lower() - self.net_ops = bsd_netops + self.net_ops = bsd_netops.BsdNetOps def _read_system_hostname(self): sys_hostname = self._read_hostname(self.hostname_conf_fn) diff --git a/cloudinit/net/netops/bsd_netops.py b/cloudinit/net/netops/bsd_netops.py index e87ee1e8444..fd9ea8ca573 100644 --- a/cloudinit/net/netops/bsd_netops.py +++ b/cloudinit/net/netops/bsd_netops.py @@ -1,16 +1,17 @@ from typing import Optional import cloudinit.net.netops as netops +from cloudinit import subp class BsdNetOps(netops.NetOps): @staticmethod def link_up(interface: str): - pass + subp.subp(["ifconfig", interface, "up"]) @staticmethod def link_down(interface: str): - pass + subp.subp(["ifconfig", interface, "down"]) @staticmethod def add_route( @@ -20,11 +21,15 @@ def add_route( gateway: Optional[str] = None, source_address: Optional[str] = None ): - pass + subp.subp(["route", "add", route, "-interface", interface]) + if gateway and gateway != "0.0.0.0": + subp.subp( + ["route", "change", route, gateway], + ) @staticmethod def append_route(interface: str, address: str, gateway: str): - pass + return BsdNetOps.add_route(interface, route=address, gateway=gateway) @staticmethod def del_route( @@ -34,16 +39,36 @@ def del_route( gateway: Optional[str] = None, source_address: Optional[str] = None ): - pass + subp.subp( + ["route", "del", address] + + ([gateway] if gateway and gateway != "0.0.0.0" else []), + ) @staticmethod def get_default_route() -> str: - pass + std, _ = subp.subp(["route", "-nv", "get", "0.0.0.0/0"]) + return std.splitlines()[-1].strip() @staticmethod def add_addr(interface: str, address: str, broadcast: str): - pass + subp.subp( + [ + "ifconfig", + interface, + address, + "broadcast", + broadcast, + "alias", + ], + ) @staticmethod def del_addr(interface: str, address: str): - pass + subp.subp( + [ + "ifconfig", + interface, + address, + "-alias", + ], + )