File: Network.pm

package info (click to toggle)
rex 1.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,012 kB
  • sloc: perl: 35,587; xml: 264; sh: 51; makefile: 13
file content (98 lines) | stat: -rw-r--r-- 1,798 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#

=head1 NAME

Rex::Commands::Network - Network Module

=head1 DESCRIPTION

With this module you can get information of the routing table, current network connections, open ports, ...

=head1 SYNOPSIS

 use Rex::Commands::Network;
 
 my @routes = route;
 print Dumper(\@routes);
 
 my $default_gw = default_gateway;
 default_gateway "192.168.2.1";
 
 my @netstat = netstat;
 my @tcp_connections = grep { $_->{"proto"} eq "tcp" } netstat;

=head1 EXPORTED FUNCTIONS

=cut

package Rex::Commands::Network;

use v5.12.5;
use warnings;

our $VERSION = '1.14.1'; # VERSION

require Rex::Exporter;
use Rex::Commands::Gather;
use Rex::Hardware::Network;
use Data::Dumper;

use vars qw(@EXPORT);
use base qw(Rex::Exporter);

@EXPORT = qw(route default_gateway netstat);

=head2 route

Get routing information

=cut

sub route {
  return Rex::Hardware::Network::route();
}

=head2 default_gateway([$default_gw])

Get or set the default gateway.

=cut

sub default_gateway {
  my $gw = shift;

  if ($gw) {
    Rex::get_current_connection()->{reporter}
      ->report_resource_start( type => "default_gateway", name => $gw );

    my $cur_gw = Rex::Hardware::Network::default_gateway();

    Rex::Hardware::Network::default_gateway($gw);

    my $new_gw = Rex::Hardware::Network::default_gateway();

    if ( $cur_gw ne $new_gw ) {
      Rex::get_current_connection()->{reporter}
        ->report( changed => 1, message => "New default gateway $gw set." );
    }

    Rex::get_current_connection()->{reporter}
      ->report_resource_end( type => "default_gateway", name => $gw );
  }

  return Rex::Hardware::Network::default_gateway();
}

=head2 netstat

Get network connection information

=cut

sub netstat {
  return Rex::Hardware::Network::netstat();
}

1;