blib/lib/Suricata/Ping.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Suricata::Ping;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use Regexp::IPv4 qw($IPv4_re);
8 use YAML::XS qw(Load);
9 use File::Slurp qw(read_file);
10 use Hash::Merge ();
11  
12 =head1 NAME
13  
14 Suricata::Ping - Reads in a suricata config and sends a ping to the specifid interface.
15  
16 =head1 VERSION
17  
18 Version 0.0.1
19  
20 =cut
21  
22 our $VERSION = '0.0.1';
23  
24 =head1 SYNOPSIS
25  
26 Quick summary of what the module does.
27  
28 Perhaps a little code snippet.
29  
30 use Suricata::Ping;
31  
32 my $suricata_ping;
33 eval {
34 $suricata_ping=Suricata::Ping->new(
35 config_file=>'/usr/local/etc/suricata/config.yaml'
36 );
37 };
38 if ($@) {
39 print $@."\n";
40 exit 1;
41 }
42 $suricata_ping->ping;
43  
44 =head1 SUBROUTINES/METHODS
45  
46 =head2 new
47  
48 Initiates the object.
49  
50 - config_file :: The Suricata config to read in.
51 - default :: undef
52  
53 - pattern :: Pattern to use with the ping.
54 - default :: e034o31qwe9034oldlAd31qdgf3
55  
56 - ip :: The IP to ping.
57 - default :: 8.8.8.8
58  
59 - section :: The config section of the config to use.
60 - default, Linux :: af_packet
61 - default, other :: pcap
62  
63 - count :: Number of packets to send.
64 - default :: 5
65  
66 =cut
67  
68 sub new {
69 my ( $empty, %opts ) = @_;
70  
71 # make sure the value passed for the config file looks sane
72 if ( !defined( $opts{config_file} ) ) {
73 die('$opts{config_file} is undef');
74 } elsif ( !-f $opts{config_file} ) {
75 die( '$opts{config_file}, "' . $opts{config_file} . '", is not a file' );
76 } elsif ( !-r $opts{config_file} ) {
77 die( '$opts{config_file}, "' . $opts{config_file} . '", is readable' );
78 }
79  
80 # make sure we have a IP specified for IP or use the default
81 if ( defined( $opts{ip} ) ) {
82 if ( ref( $opts{ip} ) ne '' ) {
83 die( '$opts{ip} has a ref of "' . ref( $opts{ip} ) . '" instead of ""' );
84 } elsif ( $opts{ip} !~ /^$IPv4_re$/
85 && $opts{ip} !~ /^$IPv6_re$/ )
86 {
87 die( '$opts{ip}, "' . $opts{ip} . '", does not appear to IPv4 or 6' );
88 }
89 } else {
90 $opts{ip} = '8.8.8.8';
91 }
92  
93 # make sure we have a count specified for count or use the default
94 if ( defined( $opts{count} ) ) {
95 if ( ref( $opts{count} ) ne '' ) {
96 die( '$opts{count} has a ref of "' . ref( $opts{count} ) . '" instead of ""' );
97 } elsif ( $opts{count} !~ /^[0-9]+$/ ) {
98 die( '$opts{count}, "' . $opts{count} . '", does not appear to appear to be a integer' );
99 } elsif ( $opts{count} < 1 ) {
100 die( '$opts{count}, "' . $opts{count} . '", may not be less than 1' );
101 }
102 } else {
103 $opts{count} = 5;
104 }
105  
106 # make sure we have a sane value for pattern or use the default
107 if ( defined( $opts{pattern} ) ) {
108 if ( ref( $opts{pattern} ) ne '' ) {
109 die( '$opts{pattern} has a ref of "' . ref( $opts{pattern} ) . '" instead of ""' );
110 }
111 } else {
112 $opts{pattern} = 'e034o31qwe9034oldlAd31qdgf3';
113 }
114  
115 # make sure we have something sane for the section or use the default
116 if ( defined( $opts{section} ) ) {
117 if ( ref( $opts{section} ) ne '' ) {
118 die( '$opts{section} has a ref of "' . ref( $opts{section} ) . '" instead of ""' );
119 }
120 } else {
121 if ( $^O eq 'linux' ) {
122 $opts{section} = 'af-packet';
123 } else {
124 $opts{section} = 'pcap';
125 }
126 }
127  
128 # read in the base config
129 my $raw_config;
130 eval { $raw_config = read_file( $opts{config_file} ); };
131 if ($@) {
132 die( 'Failed to read in "' . $opts{config_file} . '"... ' . $@ );
133 }
134  
135 # parse the base config
136 my $parsed_config;
137 eval { $parsed_config = Load($raw_config); };
138 if ($@) {
139 die( 'Parsing "' . $opts{config_file} . '" failed... ' . $@ );
140 }
141  
142 # read in the includes if needed
143 if ( defined( $parsed_config->{include} ) ) {
144 if ( ref( $parsed_config->{include} ) ne 'ARRAY' ) {
145 die( '.include is defined but is of ref type "'
146 . ref( $parsed_config->{include} )
147 . '" instead of "ARRAY"' );
148 }
149  
150 my $merger = Hash::Merge->new('RIGHT_PRECEDENT');
151  
152 my $include_int = 0;
153 while ( defined( $parsed_config->{include}[$include_int] ) ) {
154 if ( ref( $parsed_config->{include}[$include_int] ) ne '' ) {
155 die( '.include['
156 . $include_int
157 . '] is defined but is of ref type "'
158 . ref( $parsed_config->{include} )
159 . '" instead of ""' );
160 }
161  
162 my $raw_include;
163 eval { $raw_include = read_file( $parsed_config->{include}[$include_int] ); };
164 if ($@) {
165 die( 'Failed to read in include['
166 . $include_int . '], "'
167 . $parsed_config->{include}[$include_int] . '"... '
168 . $@ );
169 }
170  
171 my $parsed_include;
172 eval { $parsed_include = Load($raw_include); };
173 if ($@) {
174 die( 'Parsing .include['
175 . $include_int . '], "'
176 . $parsed_config->{include}[$include_int]
177 . '", failed... '
178 . $@ );
179 }
180  
181 $parsed_config = $merger->merge( $parsed_config, $parsed_include );
182  
183 $include_int++;
184 } ## end while ( defined( $parsed_config->{include}[$include_int...]))
185 } ## end if ( defined( $parsed_config->{include} ) )
186  
187 if ( !defined( $parsed_config->{ $opts{section} } ) ) {
188 die( '.' . $opts{section} . ' not found in the config file ' . $opts{config_file} );
189 }
190 if ( ref( $parsed_config->{ $opts{section} } ) ne 'ARRAY' ) {
191 die( 'section .' . $opts{section} . ' ref is "' . $parsed_config->{ $opts{section} } . '" and not "ARRAY"' );
192 }
193 if ( !defined( $parsed_config->{ $opts{section} }[0] ) ) {
194 die( '.' . $opts{section} . '[0] is undef so this config has no configured interfaces' );
195 }
196  
197 my @interfaces;
198 my $interfaces_int = 0;
199 while ( defined( $parsed_config->{ $opts{section} }[$interfaces_int] ) ) {
200 if ( ( ref( $parsed_config->{ $opts{section} }[$interfaces_int] ) eq 'HASH' )
201 && defined( $parsed_config->{ $opts{section} }[$interfaces_int]{interface} )
202 && ( ref( $parsed_config->{ $opts{section} }[$interfaces_int]{interface} ) eq '' ) )
203 {
204 push( @interfaces, $parsed_config->{ $opts{section} }[$interfaces_int]{interface} );
205 }
206  
207 $interfaces_int++;
208 } ## end while ( defined( $parsed_config->{ $opts{section...}}))
209  
210 if ( !defined( $interfaces[0] ) ) {
211 die( 'No configured interfaces found in the config file "'
212 . $opts{config_file}
213 . '" under the section .'
214 . $opts{section} );
215 }
216  
217 my $self = {
218 config_file => $opts{config_file},
219 pattern => $opts{pattern},
220 ip => $opts{ip},
221 section => $opts{section},
222 interfaces => \@interfaces,
223 count => $opts{count},
224 };
225 bless $self;
226  
227 return $self;
228 } ## end sub new
229  
230 =head2 ping
231  
232 Pings each interface.
233  
234 This does not return any status or the like.
235  
236 This requires ping be in the current path.
237  
238 $suricata_ping->ping;
239  
240 =cut
241  
242 sub ping {
243 my ( $self, %opts ) = @_;
244  
245 foreach my $interface ( @{ $self->{interfaces} } ) {
246 system( 'ping', '-c', $self->{count}, '-p', $self->{pattern}, '-I', $interface, $self->{ip} );
247 }
248 }
249  
250 =head1 AUTHOR
251  
252 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
253  
254 =head1 BUGS
255  
256 Please report any bugs or feature requests to C<bug-suricata-ping at rt.cpan.org>, or through
257 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Suricata-Ping>. I will be notified, and then you'll
258 automatically be notified of progress on your bug as I make changes.
259  
260  
261  
262  
263 =head1 SUPPORT
264  
265 You can find documentation for this module with the perldoc command.
266  
267 perldoc Suricata::Ping
268  
269  
270 You can also look for information at:
271  
272 =over 4
273  
274 =item * RT: CPAN's request tracker (report bugs here)
275  
276 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Suricata-Ping>
277  
278 =item * CPAN Ratings
279  
280 L<https://cpanratings.perl.org/d/Suricata-Ping>
281  
282 =item * Search CPAN
283  
284 L<https://metacpan.org/release/Suricata-Ping>
285  
286 =back
287  
288  
289 =head1 ACKNOWLEDGEMENTS
290  
291  
292 =head1 LICENSE AND COPYRIGHT
293  
294 This software is Copyright (c) 2026 by Zane C. Bowers-Hadley.
295  
296 This is free software, licensed under:
297  
298 The GNU Lesser General Public License, Version 2.1, February 1999
299  
300  
301 =cut
302  
303 1; # End of Suricata::Ping
↑ Summary