Spaces:
Running
Running
Download server/faults/ospf.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 4.55 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/faults/ospf.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/server/faults/ospf.py
-
curl -L -o ospf.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/server/faults/ospf.py
4.55 kB
| """OSPF faults (F12-F16): cost change, timer mismatch, MTU mismatch, area mismatch, daemon kill.""" | |
| from __future__ import annotations | |
| import os | |
| import time | |
| from server.constants import GroundTruth | |
| def inject_ospf_cost_change(injector, router: str, peer: str, cost: int = 1000) -> GroundTruth: | |
| """F12 — Change OSPF cost on an interface to shift traffic.""" | |
| intf = injector._get_link_intf(router, peer) | |
| node = injector._nm.get_router(router) | |
| node.cmd(f'vtysh -c "conf t" -c "interface {intf.name}" -c "ip ospf cost {cost}" -c "end"') | |
| def undo(): | |
| node.cmd(f'vtysh -c "conf t" -c "interface {intf.name}" -c "ip ospf cost 10" -c "end"') | |
| return injector._register(GroundTruth( | |
| fault_type="ospf_cost_change", component=f"{router}-{peer}", | |
| details={"router": router, "peer": peer, | |
| "interface": intf.name, "cost": cost, "original_cost": 10}, | |
| ), undo) | |
| def inject_ospf_timer_mismatch(injector, router: str, peer: str, | |
| hello: int = 40, dead: int = 160) -> GroundTruth: | |
| """F13 — Change OSPF hello/dead timers on one side to break adjacency.""" | |
| intf = injector._get_link_intf(router, peer) | |
| node = injector._nm.get_router(router) | |
| node.cmd( | |
| f'vtysh -c "conf t" -c "interface {intf.name}" ' | |
| f'-c "ip ospf hello-interval {hello}" ' | |
| f'-c "ip ospf dead-interval {dead}" -c "end"' | |
| ) | |
| def undo(): | |
| node.cmd( | |
| f'vtysh -c "conf t" -c "interface {intf.name}" ' | |
| f'-c "no ip ospf hello-interval" ' | |
| f'-c "no ip ospf dead-interval" -c "end"' | |
| ) | |
| pid = injector._find_daemon_pid(node, "ospfd") | |
| if pid: | |
| os.kill(pid, 9) | |
| time.sleep(2) | |
| node.cmd("/usr/lib/frr/ospfd -d") | |
| return injector._register(GroundTruth( | |
| fault_type="ospf_timer_mismatch", component=f"{router}-{peer}", | |
| details={"router": router, "peer": peer, "interface": intf.name, | |
| "hello": hello, "dead": dead}, | |
| ), undo) | |
| def inject_ospf_mtu_mismatch(injector, router: str, peer: str, mtu: int = 1400) -> GroundTruth: | |
| """F14 — Change MTU on one side to break OSPF DBD exchange.""" | |
| intf = injector._get_link_intf(router, peer) | |
| node = injector._nm.get_router(router) | |
| node.cmd(f"ip link set dev {intf.name} mtu {mtu}") | |
| def undo(): | |
| node.cmd(f"ip link set dev {intf.name} mtu 1500") | |
| return injector._register(GroundTruth( | |
| fault_type="ospf_mtu_mismatch", component=f"{router}-{peer}", | |
| details={"router": router, "peer": peer, | |
| "interface": intf.name, "mtu": mtu}, | |
| ), undo) | |
| def inject_ospf_area_mismatch(injector, router: str, peer: str, | |
| area: str = "0.0.0.1") -> GroundTruth: | |
| """F15 — Change OSPF area on one side to break adjacency.""" | |
| intf = injector._get_link_intf(router, peer) | |
| node = injector._nm.get_router(router) | |
| ip_addr = intf.IP() | |
| node.cmd( | |
| f'vtysh -c "conf t" -c "router ospf" ' | |
| f'-c "network {ip_addr}/32 area {area}" -c "end"' | |
| ) | |
| def undo(): | |
| node.cmd( | |
| f'vtysh -c "conf t" -c "router ospf" ' | |
| f'-c "no network {ip_addr}/32 area {area}" -c "end"' | |
| ) | |
| return injector._register(GroundTruth( | |
| fault_type="ospf_area_mismatch", component=f"{router}-{peer}", | |
| details={"router": router, "peer": peer, | |
| "interface": intf.name, "area": area}, | |
| ), undo) | |
| def inject_ospf_daemon_kill(injector, router: str) -> GroundTruth: | |
| """F16 — Kill ospfd process on a router.""" | |
| node = injector._nm.get_router(router) | |
| pid = injector._find_daemon_pid(node, "ospfd") | |
| if pid: | |
| os.kill(pid, 9) | |
| def undo(): | |
| node.cmd("/usr/lib/frr/ospfd -d -A 127.0.0.1") | |
| return injector._register(GroundTruth( | |
| fault_type="ospf_daemon_kill", component=router, | |
| details={"router": router, "daemon": "ospfd", "killed_pid": pid}, | |
| ), undo) | |
| FAULTS = { | |
| "ospf_cost_change": {"fn": inject_ospf_cost_change, "difficulty": "medium", "needs": "link"}, | |
| "ospf_timer_mismatch": {"fn": inject_ospf_timer_mismatch, "difficulty": "medium", "needs": "link"}, | |
| "ospf_mtu_mismatch": {"fn": inject_ospf_mtu_mismatch, "difficulty": "medium", "needs": "link"}, | |
| "ospf_area_mismatch": {"fn": inject_ospf_area_mismatch, "difficulty": "medium", "needs": "link"}, | |
| "ospf_daemon_kill": {"fn": inject_ospf_daemon_kill, "difficulty": "medium", "needs": "router"}, | |
| } | |