-
Notifications
You must be signed in to change notification settings - Fork 220
Check if an iptables chain exists #1186
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,7 +104,7 @@ use Rex::Logger; | |
|
|
||
| @EXPORT = qw(iptables is_nat_gateway iptables_list iptables_clear | ||
| open_port close_port redirect_port | ||
| default_state_rule); | ||
| default_state_rule chain_exists); | ||
|
|
||
| sub iptables; | ||
|
|
||
|
|
@@ -485,6 +485,49 @@ sub _iptables_list { | |
| return $ret; | ||
| } | ||
|
|
||
| =head2 chain_exists | ||
|
|
||
| Returns true if a chain exists in a table, false otherwise. | ||
|
|
||
| In this example we create a chain unless it already exists. | ||
|
|
||
| task "create_chain", sub { | ||
| iptables(t => 'filter', N => 'FOO') | ||
| unless chain_exists('filter','FOO'); | ||
| }; | ||
|
|
||
|
|
||
| =cut | ||
|
|
||
| sub chain_exists { | ||
| my ( $table, $chain, @params ) = @_; | ||
| my $iptables = _get_executable( \@params ); | ||
| my @lines = run "$iptables-save"; | ||
|
|
||
| return _chain_exists( $table, $chain, @lines ); | ||
|
||
| } | ||
|
|
||
| sub _chain_exists { | ||
| my ( $table, $chain, @lines ) = @_; | ||
| my ($current_table); | ||
| for my $line (@lines) { | ||
| chomp $line; | ||
|
|
||
| next if ( $line eq "COMMIT" ); | ||
| next if ( $line =~ m/^#/ ); | ||
| if ( $line =~ m/^:(\w+)/ ) { | ||
| return 1 if $current_table eq $table && $chain eq $1; | ||
| next; | ||
| } | ||
|
|
||
| if ( $line =~ m/^\*([a-z]+)$/ ) { | ||
| $current_table = $1; | ||
| next; | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| =head2 iptables_clear | ||
|
|
||
| Remove all iptables rules. | ||
|
|
||
There was a problem hiding this comment.
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
-6as 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