diff --git a/iptables/iptables.go b/iptables/iptables.go index e95929c..6eed58e 100644 --- a/iptables/iptables.go +++ b/iptables/iptables.go @@ -241,6 +241,24 @@ func (ipt *IPTables) DeleteIfExists(table, chain string, rulespec ...string) err return err } +// DeleteById deletes the rule with the specified ID in the given table and chain. +func (ipt *IPTables) DeleteById(table, chain string, id int) (string, error) { + rules, err := ipt.List(table, chain) + if err != nil { + return "", err + } + + for _, rule := range rules { + args := []string{"-t", table, "-D", chain, strconv.Itoa(id)} + _, err := ipt.executeList(args) + if err != nil { + return "", fmt.Errorf("rule %s with ID %d not found: %v", rule, id, err) + } + } + + return "Rule deleted successfully", nil +} + // List rules in specified table/chain func (ipt *IPTables) ListById(table, chain string, id int) (string, error) { args := []string{"-t", table, "-S", chain, strconv.Itoa(id)}