coreos / go-iptables

Go wrapper around iptables utility
Apache License 2.0
1.11k stars 257 forks source link

Add `GetRuleById()` function #95

Closed alegrey91 closed 2 years ago

alegrey91 commented 2 years ago

I recently used your package to manage iptables forwards (thanks!). I would introduce a new function:

func GetRuleById(table string, chain string, id int) error {
    ...
}

This allow you to retrieve a specific rule by passing its number identifier. The number is retrievable using the following (example) command:

$ sudo iptables -t nat -L PREROUTING -n --line-number
Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    DNAT       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:1242 to:127.0.0.1:29
2    DNAT       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:1242 to:127.0.0.1:22
3    DNAT       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:1242 to:127.0.0.1:25

This is really useful when you need information or need to delete a specific rule.

alegrey91 commented 2 years ago

If you like the idea, I could take care of the issue