File: Networks.pm

package info (click to toggle)
ocsinventory-agent 2%3A2.10.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,424 kB
  • sloc: perl: 26,492; xml: 773; objc: 528; sh: 386; ansic: 333; makefile: 12
file content (134 lines) | stat: -rw-r--r-- 4,812 bytes parent folder | download | duplicates (4)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package Ocsinventory::Agent::Backend::OS::Solaris::Networks;


#ce5: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
#        inet 55.37.101.171 netmask fffffc00 broadcast 55.37.103.255
#        ether 0:3:ba:24:9b:bf

#aggr40001:2: flags=201000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4,CoS> mtu 1500 index 3
#        inet 55.37.101.172 netmask ffffff00 broadcast 223.0.146.255


use strict;

sub check {
    my $params = shift;
    my $common = $params->{common};
    $common->can_run("ifconfig") && $common->can_run("netstat") && $common->can_load ("Net::IP qw(:PROC)") 
}

# Initialise the distro entry
sub run {
    my $params = shift;
    my $common = $params->{common};

    my $description;
    my $ipaddress;
    my $ipgateway;
    my $ipmask;
    my $ipsubnet;
    my $macaddr;
    my $status;
    my $type;
    my $zone;

    my @zoneadmcmd;
    my @zacsplitted;
     
    # get the first field of the first line of "zoneadm list -p"
    @zoneadmcmd = `zoneadm list -p`;
    @zacsplitted = split(/:/, $zoneadmcmd[0]);
    $zone = $zacsplitted[0];
      
    foreach (`netstat -rn`){
      $ipgateway=$1 if /^default\s+(\S+)/i;
    }
    #print "Nom :".$zone."*************************\n";

    ### STEP 1: get aliases and zones' interfaces
    foreach (`ifconfig -a`){
        # resets if new interface
        $ipaddress = $description = $macaddr = $status = $type = undef if /^(\S+):/;
        # interface name and other data
        $description = $1.":".$2 if /^(\S+):(\S+):/;
        $ipaddress = $1 if /inet\s+(\S+)/i;
        $ipmask = $1 if /\S*netmask\s+(\S+)/i;  
        if (/ether\s+(\S+)/i) {
            # See
            # https://sourceforge.net/tracker/?func=detail&atid=487492&aid=1819948&group_id=58373
            $macaddr = sprintf "%02x:%02x:%02x:%02x:%02x:%02x" ,
            map hex, split /\:/, $1;
        }
        $status = 1 if /<UP,/;      
        # debug info
        #print "INFO1 : ".$description."_". $ipaddress."_".$ipmask."_".$macaddr."\n";
        if (($description &&  $ipaddress) ){   
            #HEX TO DEC TO BIN TO IP       
            $ipmask = hex($ipmask);
            $ipmask = sprintf("%d", $ipmask);
            $ipmask = unpack("B*", pack("N", $ipmask));
            $ipmask = ip_bintoip($ipmask,4);     
            #print $ipmask."\n";
            my $binip = &ip_iptobin ($ipaddress ,4);
            my $binmask = &ip_iptobin ($ipmask ,4);
            my $binsubnet = $binip & $binmask;
            $ipsubnet = ip_bintoip($binsubnet,4);     
            $common->addNetwork({
                DESCRIPTION => $description,
                IPADDRESS => $ipaddress,      
                IPGATEWAY => $ipgateway,
                IPMASK => $ipmask,
                IPSUBNET => $ipsubnet,
                MACADDR => $macaddr,
                STATUS => $status?"Up":"Down",
                TYPE => $type,
            });
        }
    }    

### STEP 2: get physical interfaces when not a zone

    if (!$zone){  
        foreach (`ifconfig -a`){            
            # resets if new interface
            $ipaddress = $description = $macaddr = $status = $type = undef if /^(\S+):/;
            # interface name and other data
            $description = $1 if /^(\S+): /;
            $ipaddress = $1 if /inet\s+(\S+)/i;
            $ipmask = $1 if /\S*netmask\s+(\S+)/i;  
            if (/ether\s+(\S+)/i) {
                # See
                # https://sourceforge.net/tracker/?func=detail&atid=487492&aid=1819948&group_id=58373
                $macaddr = sprintf "%02x:%02x:%02x:%02x:%02x:%02x" ,
                map hex, split /\:/, $1;
            }
            $status = 1 if /<UP,/;      
            # debug info
            #print "INFO2 : ".$description."_". $ipaddress."_".$ipmask."_".$macaddr."\n";              
            if  (($description && $macaddr)){
                #HEX TO DEC TO BIN TO IP       
                $ipmask = hex($ipmask);
                $ipmask = sprintf("%d", $ipmask);
                $ipmask = unpack("B*", pack("N", $ipmask));
                $ipmask = ip_bintoip($ipmask,4);     
                #print $ipmask."\n";
                my $binip = &ip_iptobin ($ipaddress ,4);
                my $binmask = &ip_iptobin ($ipmask ,4);
                my $binsubnet = $binip & $binmask;
                $ipsubnet = ip_bintoip($binsubnet,4);     
                $common->addNetwork({
                    DESCRIPTION => $description,
                    IPADDRESS => $ipaddress,      
                    IPGATEWAY => $ipgateway,
                    IPMASK => $ipmask,
                    IPSUBNET => $ipsubnet,
                    MACADDR => $macaddr,
                    STATUS => $status?"Up":"Down",
                    TYPE => $type,
                });
            }
        }
    }
}

1;