Last active
January 4, 2025 22:05
-
-
Save aojea/8a811e11a060fa638aa2dd8165960d5a to your computer and use it in GitHub Desktop.
golang nftables dump go objects
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
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "os" | |
| "github.com/google/nftables" | |
| ) | |
| func main() { | |
| args := os.Args[1:] | |
| if len(args) != 2 { | |
| log.Fatalf("need to specify the table and chain to list") | |
| } | |
| c, err := nftables.New() | |
| if err != nil { | |
| log.Fatalf("nftables.New() failed: %v", err) | |
| } | |
| table, err := c.ListTableOfFamily(args[0], nftables.TableFamilyINet) | |
| if err != nil { | |
| log.Fatalf("ListTableOfFamily failed: %v", err) | |
| } | |
| chain, err := c.ListChain(table, args[1]) | |
| if err != nil { | |
| log.Fatalf("ListChain failed: %v", err) | |
| } | |
| rules, err := c.GetRules(table, chain) | |
| if err != nil { | |
| log.Fatalf("GetRules failed: %v", err) | |
| } | |
| for _, rule := range rules { | |
| log.Printf("rule position %d", rule.Position) | |
| for _, exp := range rule.Exprs { | |
| fmt.Printf("%#v\n", exp) | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This makes easy to migrate from
nfttogoogle/nftablescreating the rules withnftand dumping them we can obtain the necessary elements for the rules without having to parse the bytecode