Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if an iptables chain exists #1186

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

frankiejol
Copy link

New method to check if a chain exists in a given iptables filter

Copy link
Member

@ferki ferki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @frankiejol for your contribution! I've left some notes to address/discuss in a review before we decide about merging.

=cut

sub chain_exists {
my ( $table, $chain, @params ) = @_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this might not be consistent with the other command's convention to support -6 as first parameter to signal usage of IPv6.

Also how about these as target usage examples:

  • chain_exists 'foo'; # $table defaulting to 'filter'
  • chain_exists 'foo', table => 'filter';
  • chain_exists -6, 'foo'; # IPv6

my $iptables = _get_executable( \@params );
my @lines = run "$iptables-save";

return _chain_exists( $table, $chain, @lines );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be simpler and faster to check for the return code of iptables [--table $table] --list $chain > /dev/null command. If the chain exists it will be true, but otherwise false.

Perhaps even the built-in iptables() sub can be used for that too (but it probably needs to be wrapped in a try-catch block as it die()s in case of non-OK return codes).

@ferki
Copy link
Member

ferki commented Oct 22, 2019

@frankiejol: thanks for the followup! It seems like there are some failing tests. Could you take a look at those, please?

If you add followup commits, please make sure the commit history remains readable (I guess two commits, one for the test and one for the new functionality would suffice here). Feel free to rebase on top of current master and force push your feature branch for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants