File Coverage

lib/Rex/Commands/Network.pm
Criterion Covered Total %
statement 20 32 62.5
branch 0 4 0.0
condition n/a
subroutine 7 10 70.0
pod 3 3 100.0
total 30 49 61.2


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             =head1 NAME
6              
7             Rex::Commands::Network - Network Module
8              
9             =head1 DESCRIPTION
10              
11             With this module you can get information of the routing table, current network connections, open ports, ...
12              
13             =head1 SYNOPSIS
14              
15             use Rex::Commands::Network;
16              
17             my @routes = route;
18             print Dumper(\@routes);
19              
20             my $default_gw = default_gateway;
21             default_gateway "192.168.2.1";
22              
23             my @netstat = netstat;
24             my @tcp_connections = grep { $_->{"proto"} eq "tcp" } netstat;
25              
26             =head1 EXPORTED FUNCTIONS
27              
28             =cut
29              
30             package Rex::Commands::Network;
31              
32 1     1   14 use v5.12.5;
  1         8  
33 1     1   11 use warnings;
  1         2  
  1         47  
34              
35             our $VERSION = '1.14.2.3'; # TRIAL VERSION
36              
37             require Rex::Exporter;
38 1     1   6 use Rex::Commands::Gather;
  1         3  
  1         7  
39 1     1   8 use Rex::Hardware::Network;
  1         2  
  1         13  
40 1     1   54 use Data::Dumper;
  1         2  
  1         58  
41              
42 1     1   8 use vars qw(@EXPORT);
  1         2  
  1         62  
43 1     1   10 use base qw(Rex::Exporter);
  1         2  
  1         309  
44              
45             @EXPORT = qw(route default_gateway netstat);
46              
47             =head2 route
48              
49             Get routing information
50              
51             =cut
52              
53             sub route {
54 0     0 1   return Rex::Hardware::Network::route();
55             }
56              
57             =head2 default_gateway([$default_gw])
58              
59             Get or set the default gateway.
60              
61             =cut
62              
63             sub default_gateway {
64 0     0 1   my $gw = shift;
65              
66 0 0         if ($gw) {
67             Rex::get_current_connection()->{reporter}
68 0           ->report_resource_start( type => "default_gateway", name => $gw );
69              
70 0           my $cur_gw = Rex::Hardware::Network::default_gateway();
71              
72 0           Rex::Hardware::Network::default_gateway($gw);
73              
74 0           my $new_gw = Rex::Hardware::Network::default_gateway();
75              
76 0 0         if ( $cur_gw ne $new_gw ) {
77             Rex::get_current_connection()->{reporter}
78 0           ->report( changed => 1, message => "New default gateway $gw set." );
79             }
80              
81             Rex::get_current_connection()->{reporter}
82 0           ->report_resource_end( type => "default_gateway", name => $gw );
83             }
84              
85 0           return Rex::Hardware::Network::default_gateway();
86             }
87              
88             =head2 netstat
89              
90             Get network connection information
91              
92             =cut
93              
94             sub netstat {
95 0     0 1   return Rex::Hardware::Network::netstat();
96             }
97              
98             1;