Rex::C...ptables(3pm)

🌸 Rex::Commands::Iptables πŸ’…

okay so bestie, welcome to the Rex::Commands::Iptables page from your User Contributed Perl Documentation era. this is the Iptable Management Commands module and she eats. fr fr. ✨


✨ NAME ✨

Rex::Commands::Iptables β€” Iptable Management Commands πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

so like, with this Module you can manage basic Iptables rules. that's the whole vibe. πŸ‘‘

real talk though: Version <= 1.0: All these functions will not be reported. so if you're on the old-old era, don't come crying to me about the report, because there is no report. πŸ’€

also important, and I need you to hear me on this one: only open_port and close_port are idempotent. the rest of them will just do the thing every single time you call them, no chill. 🫢


🌈 SYNOPSIS 🌈

here's the whole fantasy, in code, exactly as the ancestors wrote it:

use Rex::Commands::Iptables;

task "firewall", sub {
  iptables_clear;

  open_port 22;
  open_port [22, 80] => {
    dev => "eth0",
  };

  close_port 22 => {
    dev => "eth0",
  };
  close_port "all";

  redirect_port 80 => 10080;
  redirect_port 80 => {
    dev => "eth0",
    to  => 10080,
  };

  default_state_rule;
  default_state_rule dev => "eth0";

  is_nat_gateway;

  iptables t => "nat",
        A => "POSTROUTING",
        o => "eth0",
        j => "MASQUERADE";

  # The 'iptables' function also accepts long options,
  # however, options with dashes need to be quoted
  iptables table => "nat",
        accept          => "POSTROUTING",
        "out-interface" => "eth0",
        jump            => "MASQUERADE";

  # Version of IP can be specified in the first argument
  # of any function: -4 or -6 (defaults to -4)
  iptables_clear -6;

  open_port -6, [22, 80];
  close_port -6, "all";
  redirect_port -6, 80 => 10080;
  default_state_rule -6;

  iptables -6, "flush";
  iptables -6,
        t     => "filter",
        A     => "INPUT",
        i     => "eth0",
        m     => "state",
        state => "RELATED,ESTABLISHED",
        j     => "ACCEPT";
};

like yes, that's a lot, but she's worth it. πŸ“


πŸ’– EXPORTED FUNCTIONS πŸ’–

🌸 open_port($port, $option)

Open a port for inbound connections. bestie inbound. people coming to you. main character energy only. πŸ‘‘

task "firewall", sub {
  open_port 22;
  open_port [22, 80];
  open_port [22, 80],
    dev => "eth1";
};

task "firewall", sub {
 open_port 22,
   dev    => "eth1",
   only_if => "test -f /etc/firewall.managed";
} ;

πŸŽ€ close_port($port, $option)

Close a port for inbound connections. goodbye, we're booked and busy. πŸ™ˆ

task "firewall", sub {
  close_port 22;
  close_port [22, 80];
  close_port [22, 80],
    dev    => "eth0",
    only_if => "test -f /etc/firewall.managed";
};

πŸ¦‹ redirect_port($in_port, $option)

Redirect $in_port to another local port. sneaky little reroute, love that for you. πŸ’œ

task "redirects", sub {
  redirect_port 80 => 10080;
  redirect_port 80 => {
    to  => 10080,
    dev => "eth0",
  };
};

⭐ iptables(@params)

Write standard iptable comands. ✨ (yes, "comands", the manpage said what it said, I don't make the rules, I just vibe with them.)

okay so listen, there's a little shortcut situation here and it's lowkey iconic: Note that there is a short form for the iptables "--flush" option; when you pass the option of "-F|"flush"" as the only argument, the command iptables -F is run on the connected host. With the two argument form of "flush" shown in the examples below, the second argument is table you want to flush. 🫢

task "firewall", sub {
  iptables t => "nat", A => "POSTROUTING", o => "eth0", j => "MASQUERADE";
  iptables t => "filter", i => "eth0", m => "state", state => "RELATED,ESTABLISHED", j => "ACCEPT";

  # automatically flushes all tables; equivalent to 'iptables -F'
  iptables "flush";
  iptables -F;

  # flush only the "filter" table
  iptables flush => "filter";
  iptables -F => "filter";
};
# Note: options with dashes "-" need to be quoted to escape them from Perl
task "long_form_firewall", sub {
  iptables table => "nat",
       append          => "POSTROUTING",
       "out-interface" => "eth0",
       jump            => "MASQUERADE";
  iptables table => "filter",
       "in-interface" => "eth0",
       match          => "state",
       state          => "RELATED,ESTABLISHED",
       jump           => "ACCEPT";
};
πŸ’€ not me reminding you again: options with dashes - need to be quoted, because Perl will see them and simply start drama. quote them. always.

πŸ’… is_nat_gateway

This function creates a NAT gateway for the device the default route points to. she's the gateway. she's literally the gateway. 🌈

task "make-gateway", sub {
  is_nat_gateway;
  is_nat_gateway -6;
};

😭 default_state_rule(%option)

Set the default state rules for the given device. setting the rules, setting the tone. ✨

task "firewall", sub {
  default_state_rule(dev => "eth0");
};

πŸ‘‘ iptables_list

List all iptables rules. receipts. 🧾✨

task "list-iptables", sub {
  print Dumper iptables_list;
  print Dumper iptables_list -6;
};

🧚 iptables_clear

Remove all iptables rules. scorched earth. clean slate. no cap. πŸ”₯

task "no-firewall", sub {
  iptables_clear;
};

and that's the whole page bestie, you ate that 🫢 this was perl v5.40.1 documentation, dated 2025-10-03, filed under Rex::C...ptables(3pm) in the User Contributed Perl Documentation collection. go forth and manage your iptables, iykyk. πŸ’–πŸ’…πŸŽ€

πŸ“¦ where this page comes from