Last active
July 16, 2026 05:10
-
-
Save paigeadelethompson/d99e6bb0aab4ae2fa6a066217831840c to your computer and use it in GitHub Desktop.
Dump FreeBSD route state to `route add' commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| for x in $(ifconfig | grep fib | awk '{print $NF}' | sort | uniq); do | |
| netstat -nr -F $x --libxo json | jq -r --arg fib "$x" ' | |
| .statistics."route-information"."route-table"."rt-family"[]? as $fam | |
| | $fam."rt-entry"[]? as $entry | |
| # Grab the layout flags as a single string to safely compare | |
| | ($entry.flags_pretty | join(",")) as $flags | |
| # Filter for explicitly routed entries | |
| | select(($flags | contains("gateway")) or ($flags | contains("blackhole")) or ($flags | contains("reject"))) | |
| # Determine the IP framework (-inet or -inet6) | |
| | (if $fam."address-family" == "Internet6" then "-inet6" else "-inet" end) as $proto | |
| # Handle the action modifier variant logic | |
| | (if $flags | contains("blackhole") then | |
| "-blackhole " | |
| elif $flags | contains("reject") then | |
| "-reject " | |
| else | |
| "" | |
| end) as $modifier | |
| # Build target string layout | |
| | "route add \($proto) -fib \($fib) \($modifier)-net \($entry.destination)" + (if $modifier == "" then " \($entry.gateway)" else "" end) | |
| ' | |
| done |
paigeadelethompson
commented
Jul 16, 2026
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment