[2] | 1 | #!/usr/bin/perl -w |
---|
[32] | 2 | # |
---|
[195] | 3 | # Copyright (C) 2005-2017 Gabriel Moreau |
---|
[32] | 4 | # |
---|
| 5 | # $Id: klask 233 2017-03-11 21:56:58Z g7moreau $ |
---|
[2] | 6 | |
---|
| 7 | use strict; |
---|
[3] | 8 | use warnings; |
---|
[220] | 9 | use version; our $VERSION = qv('0.6.2'); |
---|
[2] | 10 | |
---|
[63] | 11 | use Readonly; |
---|
[64] | 12 | use FileHandle; |
---|
[2] | 13 | use Net::SNMP; |
---|
[44] | 14 | #use YAML; |
---|
| 15 | use YAML::Syck; |
---|
[2] | 16 | use Net::Netmask; |
---|
| 17 | use Net::CIDR::Lite; |
---|
| 18 | use NetAddr::IP; |
---|
[110] | 19 | use Getopt::Long qw(GetOptions); |
---|
[68] | 20 | use Socket; |
---|
[109] | 21 | use List::Util 'shuffle'; |
---|
[2] | 22 | |
---|
[8] | 23 | # apt-get install snmp fping libnet-cidr-lite-perl libnet-netmask-perl libnet-snmp-perl libnetaddr-ip-perl libyaml-perl |
---|
[198] | 24 | # libcrypt-des-perl libcrypt-hcesha-perl libdigest-hmac-perl |
---|
[79] | 25 | # arping net-tools fping bind9-host arpwatch |
---|
[2] | 26 | |
---|
[218] | 27 | ################################################################ |
---|
| 28 | # general initialization |
---|
| 29 | ################################################################ |
---|
| 30 | |
---|
[100] | 31 | my $KLASK_VAR = '/var/lib/klask'; |
---|
[92] | 32 | my $KLASK_CFG_FILE = '/etc/klask/klask.conf'; |
---|
[28] | 33 | my $KLASK_DB_FILE = "$KLASK_VAR/klaskdb"; |
---|
| 34 | my $KLASK_SW_FILE = "$KLASK_VAR/switchdb"; |
---|
[2] | 35 | |
---|
[26] | 36 | test_running_environnement(); |
---|
| 37 | |
---|
[44] | 38 | my $KLASK_CFG = YAML::Syck::LoadFile("$KLASK_CFG_FILE"); |
---|
[2] | 39 | |
---|
[221] | 40 | my %DEFAULT = %{$KLASK_CFG->{'default'}}; |
---|
| 41 | my @SWITCH_LIST = @{$KLASK_CFG->{'switch'}}; |
---|
[2] | 42 | |
---|
[196] | 43 | my %SWITCH_LEVEL = (); |
---|
[63] | 44 | my %SWITCH_DB = (); |
---|
[2] | 45 | LEVEL_OF_EACH_SWITCH: |
---|
[209] | 46 | for my $sw (@SWITCH_LIST) { |
---|
[221] | 47 | $SWITCH_LEVEL{$sw->{hostname}} = $sw->{'level'} || $DEFAULT{'switch_level'} || 2; |
---|
[25] | 48 | $SWITCH_DB{$sw->{hostname}} = $sw; |
---|
[208] | 49 | |
---|
| 50 | # SNMP parameter initialisation |
---|
| 51 | my %session = ( -hostname => $sw->{hostname} ); |
---|
[221] | 52 | $session{-version} = $sw->{'version'} || 1; |
---|
| 53 | $session{-port} = $sw->{'snmpport'} || $DEFAULT{'snmpport'} || 161; |
---|
| 54 | if (exists $sw->{'version'} and $sw->{'version'} eq '3') { |
---|
| 55 | $session{-username} = $sw->{'username'} || 'snmpadmin'; |
---|
[208] | 56 | } |
---|
| 57 | else { |
---|
[221] | 58 | $session{-community} = $sw->{'community'} || $DEFAULT{'community'} || 'public'; |
---|
[208] | 59 | } |
---|
[221] | 60 | $sw->{'snmp_param_session'} = \%session; |
---|
[2] | 61 | } |
---|
[221] | 62 | @SWITCH_LIST = reverse sort { $SWITCH_LEVEL{$a->{hostname}} <=> $SWITCH_LEVEL{$b->{hostname}} } @{$KLASK_CFG->{'switch'}}; |
---|
[2] | 63 | |
---|
[196] | 64 | #my %SWITCH_PORT_COUNT = (); |
---|
[2] | 65 | |
---|
[11] | 66 | my %CMD_DB = ( |
---|
[133] | 67 | 'help' => \&cmd_help, |
---|
| 68 | 'version' => \&cmd_version, |
---|
| 69 | 'exportdb' => \&cmd_exportdb, |
---|
| 70 | 'updatedb' => \&cmd_updatedb, |
---|
| 71 | 'searchdb' => \&cmd_searchdb, |
---|
| 72 | 'removedb' => \&cmd_removedb, |
---|
| 73 | 'cleandb' => \&cmd_cleandb, |
---|
| 74 | 'search' => \&cmd_search, |
---|
| 75 | 'enable' => \&cmd_enable, |
---|
| 76 | 'disable' => \&cmd_disable, |
---|
| 77 | 'status' => \&cmd_status, |
---|
| 78 | 'updatesw' => \&cmd_updatesw, |
---|
| 79 | 'exportsw' => \&cmd_exportsw, |
---|
| 80 | 'iplocation' => \&cmd_ip_location, |
---|
| 81 | 'ip-free' => \&cmd_ip_free, |
---|
[35] | 82 | 'search-mac-on-switch' => \&cmd_search_mac_on_switch, |
---|
[133] | 83 | 'bad-vlan-id' => \&cmd_bad_vlan_id, |
---|
| 84 | 'set-vlan-port' => \&cmd_set_vlan_port, |
---|
| 85 | 'get-vlan-port' => \&cmd_get_vlan_port, |
---|
| 86 | 'set-vlan-name' => \&cmd_set_vlan_name, |
---|
| 87 | 'get-vlan-name' => \&cmd_get_vlan_name, |
---|
[142] | 88 | 'rebootsw' => \&cmd_rebootsw, |
---|
[2] | 89 | ); |
---|
| 90 | |
---|
[218] | 91 | #Readonly my %INTERNAL_PORT_MAP => ( |
---|
| 92 | # 0 => 'A', |
---|
| 93 | # 1 => 'B', |
---|
| 94 | # 2 => 'C', |
---|
| 95 | # 3 => 'D', |
---|
| 96 | # 4 => 'E', |
---|
| 97 | # 5 => 'F', |
---|
| 98 | # 6 => 'G', |
---|
| 99 | # 7 => 'H', |
---|
| 100 | # ); |
---|
| 101 | #Readonly my %INTERNAL_PORT_MAP_REV => reverse %INTERNAL_PORT_MAP; |
---|
[2] | 102 | |
---|
[63] | 103 | Readonly my %SWITCH_KIND => ( |
---|
[128] | 104 | # HP |
---|
[171] | 105 | J3299A => { type => 1, model => 'HP224M', match => 'HP J3299A ProCurve Switch 224M' }, |
---|
| 106 | J4120A => { type => 1, model => 'HP1600M', match => 'HP J4120A ProCurve Switch 1600M' }, |
---|
| 107 | J9029A => { type => 1, model => 'HP1800-8G', match => 'PROCURVE J9029A' }, |
---|
| 108 | J9449A => { type => 1, model => 'HP1810-8G', match => 'HP ProCurve 1810G - 8 GE' }, |
---|
| 109 | J4093A => { type => 1, model => 'HP2424M', match => 'HP J4093A ProCurve Switch 2424M' }, |
---|
| 110 | J9279A => { type => 1, model => 'HP2510G-24', match => 'ProCurve J9279A Switch 2510G-24' }, |
---|
| 111 | J9280A => { type => 1, model => 'HP2510G-48', match => 'ProCurve J9280A Switch 2510G-48' }, |
---|
| 112 | J4813A => { type => 1, model => 'HP2524', match => 'HP J4813A ProCurve Switch 2524' }, |
---|
| 113 | J4900A => { type => 1, model => 'HP2626A', match => 'HP J4900A ProCurve Switch 2626' }, |
---|
| 114 | J4900B => { type => 1, model => 'HP2626B', match => 'J4900B.+?Switch 2626' }, # ProCurve J4900B Switch 2626 # HP J4900B ProCurve Switch 2626 |
---|
| 115 | J4899B => { type => 1, model => 'HP2650', match => 'ProCurve J4899B Switch 2650' }, |
---|
| 116 | J9021A => { type => 1, model => 'HP2810-24G', match => 'ProCurve J9021A Switch 2810-24G' }, |
---|
| 117 | J9022A => { type => 1, model => 'HP2810-48G', match => 'ProCurve J9022A Switch 2810-48G' }, |
---|
| 118 | J8692A => { type => 1, model => 'HP3500-24G', match => 'J8692A Switch 3500yl-24G' }, |
---|
| 119 | J4903A => { type => 1, model => 'HP2824', match => 'J4903A.+?Switch 2824,' }, |
---|
| 120 | J4110A => { type => 1, model => 'HP8000M', match => 'HP J4110A ProCurve Switch 8000M' }, |
---|
| 121 | JE074A => { type => 2, model => 'HP5120-24G', match => 'HP Comware.+?A5120-24G EI Switch' }, |
---|
| 122 | JE069A => { type => 2, model => 'HP5120-48G', match => 'HP Comware.+?A5120-48G EI Switch' }, |
---|
| 123 | JD377A => { type => 2, model => 'HP5500-24G', match => 'HP Comware.+?A5500-24G EI Switch' }, |
---|
| 124 | JD374A => { type => 2, model => 'HP5500-24F', match => 'HP Comware.+?A5500-24G-SFP EI Switch' }, |
---|
[128] | 125 | # BayStack |
---|
[171] | 126 | BS350T => { type => 1, model => 'BS350T', match => 'BayStack 350T HW' }, |
---|
[128] | 127 | # Nexans |
---|
[171] | 128 | N3483G => { type => 2, model => 'NA3483-6G', match => 'GigaSwitch V3 TP SFP-I 48V ES3' }, |
---|
[145] | 129 | # DELL |
---|
[184] | 130 | PC7024 => { type => 2, model => 'DPC7024', match => 'PowerConnect 7024,.+?VxWorks' }, |
---|
[171] | 131 | N2048 => { type => 2, model => 'DN2048', match => 'Dell Networking N2048,' }, |
---|
[183] | 132 | N4032F => { type => 2, model => 'DN4032F', match => 'Dell Networking N4032F,' }, |
---|
[171] | 133 | N4064F => { type => 2, model => 'DN4064F', match => 'Dell Networking N4064F,' }, |
---|
[128] | 134 | # 3COM |
---|
[171] | 135 | 'H3C5500' => { type => 1, model => 'H3C5500', match => 'H3C S5500-SI Series' }, |
---|
| 136 | '3C17203' => { type => 1, model => '3C17203', match => '3Com SuperStack 3 24-Port' }, |
---|
| 137 | '3C17204' => { type => 1, model => '3C17204', match => '3Com SuperStack 3 48-Port' }, |
---|
| 138 | '3CR17562-91' => { type => 1, model => '3CR17562-91', match => '3Com Switch 4500 50-Port' }, |
---|
| 139 | '3CR17255-91' => { type => 1, model => '3CR17255-91', match => '3Com Switch 5500G-EI 48-Port' }, |
---|
| 140 | '3CR17251-91' => { type => 1, model => '3CR17251-91', match => '3Com Switch 5500G-EI 48-Port' }, |
---|
| 141 | '3CR17571-91' => { type => 1, model => '3CR17571-91', match => '3Com Switch 4500 PWR 26-Port' }, |
---|
| 142 | '3CRWX220095A' => { type => 1, model => '3CRWX220095A', match => '3Com Wireless LAN Controller' }, |
---|
| 143 | '3CR17254-91' => { type => 1, model => '3CR17254-91', match => '3Com Switch 5500G-EI 24-Port' }, |
---|
| 144 | '3CRS48G-24S-91' => { type => 1, model => '3CRS48G-24S-91', match => '3Com Switch 4800G 24-Port' }, |
---|
| 145 | '3CRS48G-48S-91' => { type => 1, model => '3CRS48G-48S-91', match => '3Com Switch 4800G 48-Port' }, |
---|
| 146 | '3C17708' => { type => 1, model => '3C17708', match => '3Com Switch 4050' }, |
---|
| 147 | '3C17709' => { type => 1, model => '3C17709', match => '3Com Switch 4060' }, |
---|
| 148 | '3C17707' => { type => 1, model => '3C17707', match => '3Com Switch 4070' }, |
---|
| 149 | '3CR17258-91' => { type => 1, model => '3CR17258-91', match => '3Com Switch 5500G-EI 24-Port SFP' }, |
---|
| 150 | '3CR17181-91' => { type => 1, model => '3CR17181-91', match => '3Com Switch 5500-EI 28-Port FX' }, |
---|
| 151 | '3CR17252-91' => { type => 1, model => '3CR17252-91', match => '3Com Switch 5500G-EI PWR 24-Port' }, |
---|
| 152 | '3CR17253-91' => { type => 1, model => '3CR17253-91', match => '3Com Switch 5500G-EI PWR 48-Port' }, |
---|
| 153 | '3CR17250-91' => { type => 1, model => '3CR17250-91', match => '3Com Switch 5500G-EI 24-Port' }, |
---|
| 154 | '3CR17561-91' => { type => 1, model => '3CR17561-91', match => '3Com Switch 4500 26-Port' }, |
---|
| 155 | '3CR17572-91' => { type => 1, model => '3CR17572-91', match => '3Com Switch 4500 PWR 50-Port' }, |
---|
| 156 | '3C17702-US' => { type => 1, model => '3C17702-US', match => '3Com Switch 4900 SX' }, |
---|
| 157 | '3C17700' => { type => 1, model => '3C17700', match => '3Com Switch 4900' }, |
---|
[18] | 158 | ); |
---|
[63] | 159 | |
---|
| 160 | Readonly my %OID_NUMBER => ( |
---|
[68] | 161 | sysDescription => '1.3.6.1.2.1.1.1.0', |
---|
| 162 | sysName => '1.3.6.1.2.1.1.5.0', |
---|
| 163 | sysContact => '1.3.6.1.2.1.1.4.0', |
---|
| 164 | sysLocation => '1.3.6.1.2.1.1.6.0', |
---|
[127] | 165 | searchPort1 => '1.3.6.1.2.1.17.4.3.1.2', # BRIDGE-MIB (802.1D). |
---|
[144] | 166 | searchPort2 => '1.3.6.1.2.1.17.7.1.2.2.1.2', # Q-BRIDGE-MIB (802.1Q) add 0 if unknown vlan id |
---|
[129] | 167 | vlanPortDefault => '1.3.6.1.2.1.17.7.1.4.5.1.1', # dot1qPvid |
---|
| 168 | vlanStatus => '1.3.6.1.2.1.17.7.1.4.3.1.5', # integer 4 Create, 6 Destroy |
---|
| 169 | vlanName => '1.3.6.1.2.1.17.7.1.4.3.1.1', # string |
---|
[141] | 170 | hpicfReset => '1.3.6.1.4.1.11.2.14.11.1.4.1', # HP reboot switch |
---|
[157] | 171 | ifIndex => '1.3.6.1.2.1.17.1.4.1.2', # dot1dBasePortIfIndex - Interface index redirection |
---|
[146] | 172 | ifName => '1.3.6.1.2.1.31.1.1.1.1', # Interface name (give port number) |
---|
[206] | 173 | portUpDown => '1.3.6.1.2.1.2.2.1.7', # 1.3.6.1.2.1.2.2.1.7.NoPort = 1 (up) = 2 (down) |
---|
[19] | 174 | ); |
---|
[18] | 175 | |
---|
[206] | 176 | Readonly my %PORT_UPDOWN => ( |
---|
| 177 | 1 => 'enable', |
---|
| 178 | 2 => 'disable', |
---|
| 179 | ); |
---|
| 180 | |
---|
[63] | 181 | Readonly my $RE_MAC_ADDRESS => qr{ [0-9,A-Z]{2} : [0-9,A-Z]{2} : [0-9,A-Z]{2} : [0-9,A-Z]{2} : [0-9,A-Z]{2} : [0-9,A-Z]{2} }xms; |
---|
| 182 | Readonly my $RE_IPv4_ADDRESS => qr{ [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} }xms; |
---|
| 183 | |
---|
[223] | 184 | Readonly my $RE_FLOAT_HOSTNAME => $DEFAULT{'float-regex'} || qr{ ^float }xms; |
---|
[63] | 185 | |
---|
[218] | 186 | ################################################################ |
---|
[206] | 187 | # main program |
---|
[218] | 188 | ################################################################ |
---|
[2] | 189 | |
---|
[11] | 190 | my $cmd = shift @ARGV || 'help'; |
---|
| 191 | if (defined $CMD_DB{$cmd}) { |
---|
| 192 | $CMD_DB{$cmd}->(@ARGV); |
---|
[2] | 193 | } |
---|
| 194 | else { |
---|
[63] | 195 | print {*STDERR} "klask: command $cmd not found\n\n"; |
---|
[11] | 196 | $CMD_DB{help}->(); |
---|
[2] | 197 | exit 1; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | exit; |
---|
| 201 | |
---|
[218] | 202 | ################################################################ |
---|
[206] | 203 | # subroutine |
---|
[218] | 204 | ################################################################ |
---|
[206] | 205 | |
---|
[218] | 206 | #--------------------------------------------------------------- |
---|
[26] | 207 | sub test_running_environnement { |
---|
| 208 | die "Configuration file $KLASK_CFG_FILE does not exists. Klask need it !\n" if not -e "$KLASK_CFG_FILE"; |
---|
| 209 | die "Var folder $KLASK_VAR does not exists. Klask need it !\n" if not -d "$KLASK_VAR"; |
---|
[63] | 210 | return; |
---|
[26] | 211 | } |
---|
| 212 | |
---|
[218] | 213 | #--------------------------------------------------------------- |
---|
[34] | 214 | sub test_switchdb_environnement { |
---|
| 215 | die "Switch database $KLASK_SW_FILE does not exists. Launch updatesw before this command !\n" if not -e "$KLASK_SW_FILE"; |
---|
[63] | 216 | return; |
---|
[34] | 217 | } |
---|
| 218 | |
---|
[218] | 219 | #--------------------------------------------------------------- |
---|
[34] | 220 | sub test_maindb_environnement { |
---|
| 221 | die "Main database $KLASK_DB_FILE does not exists. Launch updatedb before this command !\n" if not -e "$KLASK_DB_FILE"; |
---|
[63] | 222 | return; |
---|
[34] | 223 | } |
---|
| 224 | |
---|
[218] | 225 | #--------------------------------------------------------------- |
---|
[2] | 226 | # fast ping dont l'objectif est de remplir la table arp de la machine |
---|
[111] | 227 | sub fast_ping { |
---|
[68] | 228 | # Launch this command without waiting... |
---|
[132] | 229 | system "fping -q -c 1 @_ >/dev/null 2>&1 &"; |
---|
[63] | 230 | return; |
---|
[2] | 231 | } |
---|
| 232 | |
---|
[218] | 233 | #--------------------------------------------------------------- |
---|
[63] | 234 | sub shell_command { |
---|
| 235 | my $cmd = shift; |
---|
| 236 | |
---|
[64] | 237 | my $fh = new FileHandle; |
---|
| 238 | my $result = ''; |
---|
[96] | 239 | open $fh, q{-|}, "LANG=C $cmd" or die "Can't exec $cmd\n"; |
---|
[64] | 240 | $result .= <$fh>; |
---|
[63] | 241 | close $fh; |
---|
| 242 | chomp $result; |
---|
| 243 | return $result; |
---|
| 244 | } |
---|
| 245 | |
---|
[218] | 246 | #--------------------------------------------------------------- |
---|
[2] | 247 | # donne l'@ ip, dns, arp en fonction du dns OU de l'ip |
---|
| 248 | sub resolve_ip_arp_host { |
---|
| 249 | my $param_ip_or_host = shift; |
---|
[63] | 250 | my $interface = shift || q{*}; |
---|
| 251 | my $type = shift || q{fast}; |
---|
[186] | 252 | my $already = shift || q{yes}; |
---|
[2] | 253 | |
---|
| 254 | my %ret = ( |
---|
| 255 | hostname_fq => 'unknow', |
---|
| 256 | ipv4_address => '0.0.0.0', |
---|
| 257 | mac_address => 'unknow', |
---|
| 258 | ); |
---|
| 259 | |
---|
[68] | 260 | # perl -MSocket -E 'say inet_ntoa(scalar gethostbyname("tech7meylan.hmg.inpg.fr"))' |
---|
| 261 | my $packed_ip = scalar gethostbyname($param_ip_or_host); |
---|
| 262 | return %ret if not defined $packed_ip; |
---|
| 263 | $ret{ipv4_address} = inet_ntoa($packed_ip); |
---|
[197] | 264 | #if ($ret{ipv4_address} !~ m/$RE_IPv4_ADDRESS/) { |
---|
| 265 | # print "Error: for computer $param_ip_or_host on interface $interface, IP $ret{ipv4_address} is not valide\n"; |
---|
| 266 | # return %ret; |
---|
| 267 | # } |
---|
[68] | 268 | |
---|
| 269 | # perl -MSocket -E 'say scalar gethostbyaddr(inet_aton("194.254.66.240"), AF_INET)' |
---|
[186] | 270 | my $hostname_fq = scalar gethostbyaddr($packed_ip, AF_INET) if $already eq 'yes'; |
---|
[68] | 271 | $ret{hostname_fq} = $hostname_fq if defined $hostname_fq; |
---|
| 272 | |
---|
| 273 | # my $cmd = q{grep -he '\b} . $param_ip_or_host . q{\b' } . "/var/lib/arpwatch/$interface.dat | sort -rn -k 3,3 | head -1"; |
---|
[144] | 274 | #my $cmd = q{grep -he '\b} . $ret{ipv4_address} . q{\b' } . "/var/lib/arpwatch/$interface.dat | sort -rn -k 3,3 | head -1"; |
---|
| 275 | my $cmd = q{grep -He '\b} . $ret{ipv4_address} . q{\b' } . "/var/lib/arpwatch/$interface.dat" . '| sed -e \'s|^/var/lib/arpwatch/\(.*\)\.dat:|\1 |;\' | sort -rn -k 4,4 | head -1'; |
---|
| 276 | #grep -He 194.254.66.252 /var/lib/arpwatch/*.dat | sed -e 's|^/var/lib/arpwatch/\(.*\)\.dat:|\1\t|;' | sort -rn -k 4,4 | head -1 |
---|
| 277 | |
---|
[62] | 278 | my $cmd_arpwatch = shell_command $cmd; |
---|
[144] | 279 | #my ($arp, $ip, $timestamp, $host) = split m/ \s+ /xms, $cmd_arpwatch; |
---|
| 280 | my ($interface2, $arp, $ip, $timestamp, $host) = split m/ \s+ /xms, $cmd_arpwatch; |
---|
[44] | 281 | |
---|
[144] | 282 | $ret{interface} = $interface2 || $interface; |
---|
[2] | 283 | $ret{mac_address} = $arp if $arp; |
---|
| 284 | $ret{timestamp} = $timestamp if $timestamp; |
---|
| 285 | |
---|
[63] | 286 | my $nowtimestamp = time; |
---|
[3] | 287 | |
---|
[96] | 288 | if ( $type eq 'fast' and ( not defined $timestamp or $timestamp < ( $nowtimestamp - 45 * 60 ) ) ) { # 45 min |
---|
[3] | 289 | $ret{mac_address} = 'unknow'; |
---|
| 290 | return %ret; |
---|
| 291 | } |
---|
| 292 | |
---|
[194] | 293 | # ARP result |
---|
| 294 | # |
---|
| 295 | # LANG=C arp -a 194.254.66.62 -i eth331 |
---|
| 296 | # gw66-62.legi.grenoble-inp.fr (194.254.66.62) at 00:08:7c:bb:0f:c0 [ether] on eth331 |
---|
| 297 | # |
---|
| 298 | # LANG=C ip neigh show to 194.254.66.62 dev eth331 |
---|
| 299 | # 194.254.66.62 lladdr 00:08:7c:bb:0f:c0 REACHABLE |
---|
[197] | 300 | # LANG=C ip neigh show to 194.254.66.62 |
---|
| 301 | # 194.254.66.62 dev eth331 lladdr 00:08:7c:bb:0f:c0 REACHABLE |
---|
[194] | 302 | # my $cmd_arp = shell_command "arp -a $param_ip_or_host -i $ret{interface}"; |
---|
| 303 | # if ( $cmd_arp =~ m{ (\S*) \s \( ( $RE_IPv4_ADDRESS ) \) \s at \s ( $RE_MAC_ADDRESS ) }xms ) { |
---|
| 304 | # ( $ret{hostname_fq}, $ret{ipv4_address}, $ret{mac_address} ) = ($1, $2, $3); |
---|
| 305 | # } |
---|
| 306 | if ($ret{mac_address} eq 'unknow') { |
---|
[196] | 307 | # Last chance to have the mac_address |
---|
[197] | 308 | if ($ret{interface} eq '*') { |
---|
| 309 | my $cmd_arp = shell_command "ip neigh show to $ret{ipv4_address}"; |
---|
| 310 | if ( $cmd_arp =~ m{ ^$RE_IPv4_ADDRESS \s dev \s ([\w\d\.\:]+) \s lladdr \s ( $RE_MAC_ADDRESS ) \s }xms ) { |
---|
| 311 | ($ret{interface}, $ret{mac_address}) = ($1, $2); |
---|
| 312 | } |
---|
[194] | 313 | } |
---|
[197] | 314 | else { |
---|
| 315 | my $cmd_arp = shell_command "ip neigh show to $ret{ipv4_address} dev $ret{interface}"; |
---|
| 316 | if ( $cmd_arp =~ m{ ^$RE_IPv4_ADDRESS \s lladdr \s ( $RE_MAC_ADDRESS ) \s }xms ) { |
---|
| 317 | $ret{mac_address} = $1; |
---|
| 318 | } |
---|
| 319 | } |
---|
[63] | 320 | } |
---|
[2] | 321 | |
---|
[96] | 322 | # Normalize MAC Address |
---|
[63] | 323 | if ($ret{mac_address} ne 'unknow') { |
---|
[2] | 324 | my @paquets = (); |
---|
[165] | 325 | for ( split m/ : /xms, $ret{mac_address} ) { |
---|
[63] | 326 | my @chars = split m//xms, uc "00$_"; |
---|
[2] | 327 | push @paquets, "$chars[-2]$chars[-1]"; |
---|
| 328 | } |
---|
[63] | 329 | $ret{mac_address} = join q{:}, @paquets; |
---|
[2] | 330 | } |
---|
| 331 | |
---|
| 332 | return %ret; |
---|
| 333 | } |
---|
| 334 | |
---|
[20] | 335 | # Find Surname of a switch |
---|
| 336 | sub get_switch_model { |
---|
[22] | 337 | my $sw_snmp_description = shift || 'unknow'; |
---|
[169] | 338 | $sw_snmp_description =~ s/[\n\r]/ /g; |
---|
[63] | 339 | |
---|
[20] | 340 | for my $sw_kind (keys %SWITCH_KIND) { |
---|
[64] | 341 | next if not $sw_snmp_description =~ m/$SWITCH_KIND{$sw_kind}->{match}/ms; # option xms break search, why ? |
---|
[63] | 342 | |
---|
[20] | 343 | return $SWITCH_KIND{$sw_kind}->{model}; |
---|
| 344 | } |
---|
[63] | 345 | |
---|
[22] | 346 | return $sw_snmp_description; |
---|
[20] | 347 | } |
---|
| 348 | |
---|
[218] | 349 | #--------------------------------------------------------------- |
---|
[2] | 350 | # va rechercher le nom des switchs pour savoir qui est qui |
---|
[4] | 351 | sub init_switch_names { |
---|
[173] | 352 | my ($verbose, $verb_description, $check_hostname, $check_location) = @_; |
---|
[63] | 353 | |
---|
[133] | 354 | printf "%-26s %-25s %s\n",'Switch','Description','Type' if $verbose; |
---|
[82] | 355 | print "------------------------------------------------------------------------------\n" if $verbose; |
---|
[2] | 356 | |
---|
| 357 | INIT_EACH_SWITCH: |
---|
[196] | 358 | for my $sw (@SWITCH_LIST) { |
---|
[221] | 359 | my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}} ); |
---|
[14] | 360 | print "$error \n" if $error; |
---|
| 361 | |
---|
[2] | 362 | my $result = $session->get_request( |
---|
[18] | 363 | -varbindlist => [ |
---|
[63] | 364 | $OID_NUMBER{sysDescription}, |
---|
[18] | 365 | $OID_NUMBER{sysName}, |
---|
| 366 | $OID_NUMBER{sysContact}, |
---|
| 367 | $OID_NUMBER{sysLocation}, |
---|
| 368 | ] |
---|
[2] | 369 | ); |
---|
[196] | 370 | if (!defined $result) { |
---|
| 371 | printf {*STDERR} "ERROR: %s.\n", $session->error(); |
---|
| 372 | $session->close(); |
---|
| 373 | # Remove bad switch |
---|
| 374 | @SWITCH_LIST = grep { $_->{hostname} ne $sw->{hostname} } @SWITCH_LIST; |
---|
| 375 | delete $SWITCH_LEVEL{$sw->{hostname}} if exists $SWITCH_LEVEL{$sw->{hostname}}; |
---|
| 376 | delete $SWITCH_DB{$sw->{hostname}} if exists $SWITCH_DB{$sw->{hostname}}; |
---|
| 377 | next INIT_EACH_SWITCH; |
---|
| 378 | } |
---|
| 379 | |
---|
[18] | 380 | $sw->{description} = $result->{$OID_NUMBER{sysName}} || $sw->{hostname}; |
---|
[170] | 381 | $sw->{model} = get_switch_model($result->{$OID_NUMBER{sysDescription}}); |
---|
| 382 | if ($verb_description) { |
---|
| 383 | my $desc = $result->{$OID_NUMBER{sysDescription}}; |
---|
| 384 | $desc =~ s/[\n\r]/ /g; |
---|
| 385 | print " description: $desc\n" |
---|
| 386 | } |
---|
[172] | 387 | if ($check_hostname) { |
---|
| 388 | my ($hostname) = split /\./, $sw->{hostname}, 2; |
---|
[175] | 389 | print " $hostname - error internal hostname: $sw->{hostname}\n" if $result->{$OID_NUMBER{sysName}} ne $hostname; |
---|
[172] | 390 | } |
---|
[173] | 391 | if ($check_location) { |
---|
[174] | 392 | my $location = $result->{$OID_NUMBER{sysLocation}}; |
---|
| 393 | $location =~ s/^"(.+)"$/$1/; |
---|
[175] | 394 | print " $sw->{hostname} - error location: '$location' -> '$sw->{location}'\n" if $location ne $sw->{location}; |
---|
[173] | 395 | } |
---|
[3] | 396 | #$sw->{location} = $result->{"1.3.6.1.2.1.1.6.0"} || $sw->{hostname}; |
---|
| 397 | #$sw->{contact} = $result->{"1.3.6.1.2.1.1.4.0"} || $sw->{hostname}; |
---|
[2] | 398 | $session->close; |
---|
[22] | 399 | |
---|
[63] | 400 | # Ligne à virer car on récupère maintenant le modèle du switch |
---|
[196] | 401 | #my ($desc, $type) = split m/ : /xms, $sw->{description}, 2; |
---|
[197] | 402 | printf "%-26s 0--------->>>> %-25s %s\n", $sw->{hostname}, $sw->{description}, $sw->{model} if $verbose; |
---|
[2] | 403 | } |
---|
| 404 | |
---|
| 405 | print "\n" if $verbose; |
---|
[63] | 406 | return; |
---|
[2] | 407 | } |
---|
| 408 | |
---|
[218] | 409 | #--------------------------------------------------------------- |
---|
[2] | 410 | # convertit l'hexa (uniquement 2 chiffres) en decimal |
---|
[86] | 411 | sub digit_hex_to_dec { |
---|
[2] | 412 | #00:0F:1F:43:E4:2B |
---|
[63] | 413 | my $car = '00' . uc shift; |
---|
[2] | 414 | |
---|
[4] | 415 | return '00' if $car eq '00UNKNOW'; |
---|
[2] | 416 | my %table = ( |
---|
[62] | 417 | '0'=>'0', '1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', |
---|
| 418 | '5'=>'5', '6'=>'6', '7'=>'7', '8'=>'8', '9'=>'9', |
---|
[63] | 419 | 'A'=>'10', 'B'=>'11', 'C'=>'12', 'D'=>'13', 'E'=>'14', 'F'=>'15', |
---|
[2] | 420 | ); |
---|
[63] | 421 | my @chars = split m//xms, $car; |
---|
[2] | 422 | return $table{$chars[-2]}*16 + $table{$chars[-1]}; |
---|
| 423 | } |
---|
| 424 | |
---|
[218] | 425 | #--------------------------------------------------------------- |
---|
[136] | 426 | |
---|
| 427 | sub normalize_mac_address { |
---|
| 428 | my $mac_address = shift; |
---|
| 429 | |
---|
[228] | 430 | # D07E-28D1-7AB8 or D07E.28D1.7AB8 or d07e28-d17ab8 |
---|
| 431 | if ($mac_address =~ m{^ (?: [0-9A-Fa-f]{4} [-\.]){2} [0-9A-Fa-f]{4} $}xms |
---|
| 432 | or $mac_address =~ m{^ [0-9A-Fa-f]{6} - [0-9A-Fa-f]{6} $}xms |
---|
| 433 | ) { |
---|
[229] | 434 | $mac_address =~ s/[-\.]//g; |
---|
[139] | 435 | return join q{:}, unpack('(A2)*', uc($mac_address)); |
---|
| 436 | } |
---|
| 437 | |
---|
[136] | 438 | return join q{:}, map { substr( uc("00$_"), -2) } split m/ [:-] /xms, $mac_address; |
---|
| 439 | } |
---|
| 440 | |
---|
[218] | 441 | #--------------------------------------------------------------- |
---|
[86] | 442 | # convertit l'@ mac en decimal |
---|
| 443 | sub mac_address_hex_to_dec { |
---|
[2] | 444 | #00:0F:1F:43:E4:2B |
---|
[86] | 445 | my $mac_address = shift; |
---|
[2] | 446 | |
---|
[86] | 447 | my @paquets = split m/ : /xms, $mac_address; |
---|
[63] | 448 | my $return = q{}; |
---|
[165] | 449 | for (@paquets) { |
---|
[86] | 450 | $return .= q{.} . digit_hex_to_dec($_); |
---|
[2] | 451 | } |
---|
| 452 | return $return; |
---|
| 453 | } |
---|
| 454 | |
---|
[218] | 455 | #--------------------------------------------------------------- |
---|
[2] | 456 | # va rechercher le port et le switch sur lequel est la machine |
---|
| 457 | sub find_switch_port { |
---|
[86] | 458 | my $mac_address = shift; |
---|
[63] | 459 | my $switch_proposal = shift || q{}; |
---|
[144] | 460 | my $vlan_id = shift || 0; |
---|
[63] | 461 | |
---|
[2] | 462 | my %ret; |
---|
[63] | 463 | $ret{switch_description} = 'unknow'; |
---|
| 464 | $ret{switch_port} = '0'; |
---|
[2] | 465 | |
---|
[86] | 466 | return %ret if $mac_address eq 'unknow';; |
---|
[2] | 467 | |
---|
[196] | 468 | my @switch_search = @SWITCH_LIST; |
---|
[63] | 469 | if ($switch_proposal ne q{}) { |
---|
[196] | 470 | for my $sw (@SWITCH_LIST) { |
---|
[2] | 471 | next if $sw->{hostname} ne $switch_proposal; |
---|
[22] | 472 | unshift @switch_search, $sw; |
---|
[2] | 473 | last; |
---|
| 474 | } |
---|
| 475 | } |
---|
| 476 | |
---|
[221] | 477 | my $research1 = $OID_NUMBER{'searchPort1'} . mac_address_hex_to_dec($mac_address); |
---|
| 478 | my $research2 = $OID_NUMBER{'searchPort2'} .'.'. $vlan_id . mac_address_hex_to_dec($mac_address); |
---|
[63] | 479 | |
---|
[2] | 480 | LOOP_ON_SWITCH: |
---|
[22] | 481 | for my $sw (@switch_search) { |
---|
[221] | 482 | my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}} ); |
---|
[22] | 483 | print "$error \n" if $error; |
---|
| 484 | |
---|
[2] | 485 | my $result = $session->get_request( |
---|
[124] | 486 | -varbindlist => [$research1] |
---|
[2] | 487 | ); |
---|
[124] | 488 | if (not defined $result) { |
---|
| 489 | $result = $session->get_request( |
---|
| 490 | -varbindlist => [$research2] |
---|
| 491 | ); |
---|
| 492 | $result->{$research1} = $result->{$research2} if defined $result; |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | if (not (defined $result and $result->{$research1} ne 'noSuchInstance')) { |
---|
[2] | 496 | $session->close; |
---|
| 497 | next LOOP_ON_SWITCH; |
---|
| 498 | } |
---|
| 499 | |
---|
[167] | 500 | my $swport_num = $result->{$research1}; |
---|
[211] | 501 | my $swport_hr = get_human_readable_port($sw->{model}, snmp_get_switchport_hr($session, $swport_num)); |
---|
[2] | 502 | |
---|
[150] | 503 | $session->close; |
---|
| 504 | |
---|
| 505 | # IMPORTANT !! |
---|
| 506 | # ceci empeche la detection sur certains port ... |
---|
| 507 | # en effet les switch sont relies entre eux par un cable reseau et du coup |
---|
| 508 | # tous les arp de toutes les machines sont presentes sur ces ports (ceux choisis ici sont les miens) |
---|
| 509 | # cette partie est a ameliore, voir a configurer dans l'entete |
---|
| 510 | # 21->24 45->48 |
---|
| 511 | SWITCH_PORT_IGNORE: |
---|
[167] | 512 | for my $portignore (@{$sw->{portignore}}) { |
---|
| 513 | next LOOP_ON_SWITCH if $swport_hr eq $portignore; |
---|
[150] | 514 | } |
---|
| 515 | |
---|
| 516 | $ret{switch_hostname} = $sw->{hostname}; |
---|
| 517 | $ret{switch_description} = $sw->{description}; |
---|
[167] | 518 | $ret{switch_port} = $swport_num; |
---|
[150] | 519 | $ret{switch_port_hr} = $swport_hr; # human readable |
---|
| 520 | |
---|
| 521 | last LOOP_ON_SWITCH; |
---|
[2] | 522 | } |
---|
| 523 | return %ret; |
---|
| 524 | } |
---|
| 525 | |
---|
[218] | 526 | #--------------------------------------------------------------- |
---|
[2] | 527 | # va rechercher les port et les switch sur lequel est la machine |
---|
| 528 | sub find_all_switch_port { |
---|
[86] | 529 | my $mac_address = shift; |
---|
[144] | 530 | my $vlan_id = shift || 0; |
---|
[2] | 531 | |
---|
| 532 | my $ret = {}; |
---|
| 533 | |
---|
[86] | 534 | return $ret if $mac_address eq 'unknow'; |
---|
[2] | 535 | |
---|
[221] | 536 | my $research1 = $OID_NUMBER{'searchPort1'} . mac_address_hex_to_dec($mac_address); |
---|
| 537 | my $research2 = $OID_NUMBER{'searchPort2'} .'.'. $vlan_id . mac_address_hex_to_dec($mac_address); |
---|
[2] | 538 | LOOP_ON_ALL_SWITCH: |
---|
[196] | 539 | for my $sw (@SWITCH_LIST) { |
---|
[221] | 540 | my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}} ); |
---|
[13] | 541 | print "$error \n" if $error; |
---|
| 542 | |
---|
[2] | 543 | my $result = $session->get_request( |
---|
[124] | 544 | -varbindlist => [$research1] |
---|
[2] | 545 | ); |
---|
[124] | 546 | if (not defined $result) { |
---|
| 547 | $result = $session->get_request( |
---|
| 548 | -varbindlist => [$research2] |
---|
| 549 | ); |
---|
| 550 | $result->{$research1} = $result->{$research2} if defined $result; |
---|
| 551 | } |
---|
[13] | 552 | |
---|
[124] | 553 | if (defined $result and $result->{$research1} ne 'noSuchInstance') { |
---|
[167] | 554 | my $swport_num = $result->{$research1}; |
---|
[211] | 555 | my $swport_hr = get_human_readable_port($sw->{model}, snmp_get_switchport_hr($session, $swport_num)); |
---|
[2] | 556 | |
---|
[181] | 557 | SWITCH_PORT_IGNORE: |
---|
| 558 | for my $portignore (@{$sw->{portignore}}) { |
---|
| 559 | if ($swport_hr eq $portignore) { |
---|
| 560 | $session->close; |
---|
| 561 | next LOOP_ON_ALL_SWITCH |
---|
| 562 | } |
---|
| 563 | } |
---|
| 564 | |
---|
[2] | 565 | $ret->{$sw->{hostname}} = {}; |
---|
| 566 | $ret->{$sw->{hostname}}{hostname} = $sw->{hostname}; |
---|
| 567 | $ret->{$sw->{hostname}}{description} = $sw->{description}; |
---|
[167] | 568 | $ret->{$sw->{hostname}}{port} = $swport_num; |
---|
[154] | 569 | $ret->{$sw->{hostname}}{port_hr} = $swport_hr; |
---|
[2] | 570 | |
---|
[86] | 571 | # $SWITCH_PORT_COUNT{$sw->{hostname}}->{$swport}++; |
---|
[2] | 572 | } |
---|
| 573 | |
---|
| 574 | $session->close; |
---|
| 575 | } |
---|
| 576 | return $ret; |
---|
| 577 | } |
---|
| 578 | |
---|
[218] | 579 | #--------------------------------------------------------------- |
---|
[2] | 580 | sub get_list_network { |
---|
| 581 | |
---|
[12] | 582 | return keys %{$KLASK_CFG->{network}}; |
---|
[2] | 583 | } |
---|
| 584 | |
---|
[218] | 585 | #--------------------------------------------------------------- |
---|
[2] | 586 | sub get_current_interface { |
---|
[113] | 587 | my $vlan_name = shift; |
---|
[2] | 588 | |
---|
[113] | 589 | return $KLASK_CFG->{network}{$vlan_name}{interface}; |
---|
[2] | 590 | } |
---|
[63] | 591 | |
---|
[218] | 592 | #--------------------------------------------------------------- |
---|
[144] | 593 | sub get_current_vlan_id { |
---|
| 594 | my $vlan_name = shift; |
---|
| 595 | |
---|
[182] | 596 | return 0 if not exists $KLASK_CFG->{network}{$vlan_name}; |
---|
[144] | 597 | return $KLASK_CFG->{network}{$vlan_name}{'vlan-id'}; |
---|
| 598 | } |
---|
| 599 | |
---|
[218] | 600 | #--------------------------------------------------------------- |
---|
[179] | 601 | sub get_current_scan_mode { |
---|
| 602 | my $vlan_name = shift; |
---|
| 603 | |
---|
| 604 | return $KLASK_CFG->{network}{$vlan_name}{'scan-mode'} || $DEFAULT{'scan-mode'} || 'active'; |
---|
| 605 | } |
---|
| 606 | |
---|
[218] | 607 | #--------------------------------------------------------------- |
---|
[144] | 608 | sub get_current_vlan_name_for_interface { |
---|
| 609 | my $interface = shift; |
---|
| 610 | |
---|
| 611 | for my $vlan_name (keys %{$KLASK_CFG->{network}}) { |
---|
| 612 | next if $KLASK_CFG->{network}{$vlan_name}{interface} ne $interface; |
---|
| 613 | return $vlan_name; |
---|
| 614 | } |
---|
| 615 | } |
---|
| 616 | |
---|
[218] | 617 | #--------------------------------------------------------------- |
---|
[2] | 618 | # liste l'ensemble des adresses ip d'un réseau |
---|
| 619 | sub get_list_ip { |
---|
[113] | 620 | my @vlan_name = @_; |
---|
[2] | 621 | |
---|
| 622 | my $cidrlist = Net::CIDR::Lite->new; |
---|
| 623 | |
---|
[113] | 624 | for my $net (@vlan_name) { |
---|
[12] | 625 | my @line = @{$KLASK_CFG->{network}{$net}{'ip-subnet'}}; |
---|
[2] | 626 | for my $cmd (@line) { |
---|
[209] | 627 | for my $method (keys %{$cmd}) { |
---|
[2] | 628 | $cidrlist->add_any($cmd->{$method}) if $method eq 'add'; |
---|
| 629 | } |
---|
| 630 | } |
---|
| 631 | } |
---|
| 632 | |
---|
[4] | 633 | my @res = (); |
---|
[2] | 634 | |
---|
| 635 | for my $cidr ($cidrlist->list()) { |
---|
| 636 | my $net = new NetAddr::IP $cidr; |
---|
[63] | 637 | for my $ip (@{$net}) { |
---|
| 638 | $ip =~ s{ /32 }{}xms; |
---|
[2] | 639 | push @res, $ip; |
---|
| 640 | } |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | return @res; |
---|
| 644 | } |
---|
| 645 | |
---|
[218] | 646 | #--------------------------------------------------------------- |
---|
[9] | 647 | # liste l'ensemble des routeurs du réseau |
---|
| 648 | sub get_list_main_router { |
---|
[113] | 649 | my @vlan_name = @_; |
---|
[9] | 650 | |
---|
| 651 | my @res = (); |
---|
| 652 | |
---|
[113] | 653 | for my $net (@vlan_name) { |
---|
[12] | 654 | push @res, $KLASK_CFG->{network}{$net}{'main-router'}; |
---|
[9] | 655 | } |
---|
| 656 | |
---|
| 657 | return @res; |
---|
| 658 | } |
---|
| 659 | |
---|
[218] | 660 | #--------------------------------------------------------------- |
---|
[2] | 661 | sub get_human_readable_port { |
---|
[22] | 662 | my $sw_model = shift; |
---|
| 663 | my $sw_port = shift; |
---|
[63] | 664 | |
---|
[167] | 665 | # Not need anymore |
---|
| 666 | # get port name by snmp |
---|
| 667 | return $sw_port; |
---|
[2] | 668 | } |
---|
| 669 | |
---|
[218] | 670 | # if ($sw_model eq 'HP8000M') { |
---|
| 671 | # |
---|
| 672 | # my $reste = (($sw_port - 1) % 8) + 1; |
---|
| 673 | # my $major = int (($sw_port - 1) / 8); |
---|
| 674 | # return "$INTERNAL_PORT_MAP{$major}$reste"; |
---|
| 675 | # } |
---|
| 676 | # |
---|
| 677 | # if ($sw_model eq 'HP2424M') { |
---|
| 678 | # if ($sw_port > 24) { |
---|
| 679 | # |
---|
| 680 | # my $reste = $sw_port - 24; |
---|
| 681 | # return "A$reste"; |
---|
| 682 | # } |
---|
| 683 | # } |
---|
| 684 | # |
---|
| 685 | # if ($sw_model eq 'HP1600M') { |
---|
| 686 | # if ($sw_port > 16) { |
---|
| 687 | # |
---|
| 688 | # my $reste = $sw_port - 16; |
---|
| 689 | # return "A$reste"; |
---|
| 690 | # } |
---|
| 691 | # } |
---|
| 692 | # |
---|
| 693 | # if ($sw_model eq 'HP2810-48G' or $sw_model eq 'HP2810-24G') { |
---|
| 694 | # if ($sw_port > 48) { |
---|
| 695 | # |
---|
| 696 | # my $reste = $sw_port - 48; |
---|
| 697 | # return "Trk$reste"; |
---|
| 698 | # } |
---|
| 699 | # } |
---|
| 700 | # |
---|
| 701 | # if ($sw_model eq 'HP3500-24G') { |
---|
| 702 | # if ($sw_port > 289) { |
---|
| 703 | # |
---|
| 704 | # my $reste = $sw_port - 289; |
---|
| 705 | # return "Trk$reste"; |
---|
| 706 | # } |
---|
| 707 | # } |
---|
| 708 | # |
---|
| 709 | # return $sw_port; |
---|
[63] | 710 | |
---|
[218] | 711 | #sub get_numerical_port { |
---|
| 712 | # my $sw_model = shift; |
---|
| 713 | # my $sw_port = shift; |
---|
| 714 | # |
---|
| 715 | # if ($sw_model eq 'HP8000M') { |
---|
| 716 | # |
---|
| 717 | # my $letter = substr $sw_port, 0, 1; |
---|
| 718 | # my $reste = substr $sw_port, 1; |
---|
| 719 | # |
---|
| 720 | # return $INTERNAL_PORT_MAP_REV{$letter} * 8 + $reste; |
---|
| 721 | # } |
---|
| 722 | # |
---|
| 723 | # if ($sw_model eq 'HP2424M') { |
---|
| 724 | # if ($sw_port =~ m/^A/xms ) { |
---|
| 725 | # |
---|
| 726 | # my $reste = substr $sw_port, 1; |
---|
| 727 | # |
---|
| 728 | # return 24 + $reste; |
---|
| 729 | # } |
---|
| 730 | # } |
---|
| 731 | # |
---|
| 732 | # if ($sw_model eq 'HP1600M') { |
---|
| 733 | # if ($sw_port =~ m/^A/xms ) { |
---|
| 734 | # |
---|
| 735 | # my $reste = substr $sw_port, 1; |
---|
| 736 | # |
---|
| 737 | # return 16 + $reste; |
---|
| 738 | # } |
---|
| 739 | # } |
---|
| 740 | # |
---|
| 741 | # if ($sw_model eq 'HP2810-48G' or $sw_model eq 'HP2810-24G') { |
---|
| 742 | # if ($sw_port =~ m/^Trk/xms ) { |
---|
| 743 | # |
---|
| 744 | # my $reste = substr $sw_port, 3; |
---|
| 745 | # |
---|
| 746 | # return 48 + $reste; |
---|
| 747 | # } |
---|
| 748 | # } |
---|
| 749 | # |
---|
| 750 | # if ($sw_model eq 'HP3500-24G') { |
---|
| 751 | # if ($sw_port =~ m/^Trk/xms ) { |
---|
| 752 | # |
---|
| 753 | # my $reste = substr $sw_port, 3; |
---|
| 754 | # |
---|
| 755 | # return 289 + $reste; |
---|
| 756 | # } |
---|
| 757 | # } |
---|
| 758 | # |
---|
| 759 | # return $sw_port; |
---|
| 760 | # } |
---|
[2] | 761 | |
---|
[218] | 762 | #--------------------------------------------------------------- |
---|
[214] | 763 | sub normalize_port_human_readable { |
---|
[150] | 764 | my $sw_port_hr = shift; |
---|
| 765 | |
---|
[214] | 766 | # Manufacturer abbreviation |
---|
[162] | 767 | $sw_port_hr =~ s/^Bridge-Aggregation/Br/i; |
---|
| 768 | $sw_port_hr =~ s/^Port-Channel/Po/i; |
---|
| 769 | $sw_port_hr =~ s/^Forty-?GigabitEthernet/Fo/i; |
---|
| 770 | $sw_port_hr =~ s/^Ten-?GigabitEthernet/Te/i; |
---|
| 771 | $sw_port_hr =~ s/^GigabitEthernet/Gi/i; |
---|
| 772 | $sw_port_hr =~ s/^FastEthernet/Fa/i; |
---|
[150] | 773 | |
---|
[214] | 774 | # Customer abbreviation |
---|
| 775 | $sw_port_hr =~ s/^Ten/Te/i; |
---|
| 776 | $sw_port_hr =~ s/^Giga/Gi/i; |
---|
| 777 | |
---|
[215] | 778 | return ucfirst $sw_port_hr; |
---|
[150] | 779 | } |
---|
| 780 | |
---|
[218] | 781 | #--------------------------------------------------------------- |
---|
[220] | 782 | sub snmp_get_rwsession { |
---|
| 783 | my ($sw) = @_; |
---|
| 784 | |
---|
[221] | 785 | my %session = %{$sw->{'snmp_param_session'}}; |
---|
| 786 | $session{-community} = $sw->{'community-rw'} || $DEFAULT{'community-rw'} || 'private'; |
---|
[220] | 787 | return %session; |
---|
| 788 | } |
---|
| 789 | |
---|
| 790 | #--------------------------------------------------------------- |
---|
[211] | 791 | sub snmp_get_switchport_hr { |
---|
[154] | 792 | my ($snmp_session, $swport) = @_; |
---|
[198] | 793 | |
---|
[157] | 794 | my $research_index = $OID_NUMBER{ifIndex} .'.'. $swport; |
---|
| 795 | my $result_index = $snmp_session->get_request( |
---|
| 796 | -varbindlist => [$research_index] |
---|
| 797 | ); |
---|
| 798 | my $swifindex = $swport; |
---|
[159] | 799 | $swifindex = $result_index->{$research_index} if defined $result_index; |
---|
[157] | 800 | |
---|
| 801 | my $research_hr = $OID_NUMBER{ifName} .'.'. $swifindex; |
---|
| 802 | my $result_hr = $snmp_session->get_request( |
---|
[154] | 803 | -varbindlist => [$research_hr] |
---|
| 804 | ); |
---|
| 805 | my $swport_hr = $swport; |
---|
[214] | 806 | $swport_hr = normalize_port_human_readable($result_hr->{$research_hr}) if defined $result_hr; |
---|
[154] | 807 | return $swport_hr; |
---|
| 808 | } |
---|
| 809 | |
---|
[218] | 810 | #--------------------------------------------------------------- |
---|
[213] | 811 | # Reverse search port number |
---|
| 812 | sub snmp_get_switchport_num { |
---|
| 813 | my ($snmp_session, $swport_hr, $verbose) = @_; |
---|
| 814 | |
---|
| 815 | my $swport_num = $swport_hr; |
---|
[217] | 816 | return $swport_num if $swport_num =~ m/^\d+$/; # direct return if already numeric (next loop is expensive) |
---|
[213] | 817 | |
---|
| 818 | my $research_index = $OID_NUMBER{ifIndex}; # base OID |
---|
| 819 | my @args = ( -varbindlist => [$research_index]); |
---|
[217] | 820 | LOOP_ON_OID_PORT: |
---|
[213] | 821 | while ( defined $snmp_session->get_next_request(@args) ) { |
---|
| 822 | my ($oid_current) = $snmp_session->var_bind_names; |
---|
[217] | 823 | last LOOP_ON_OID_PORT if not Net::SNMP::oid_base_match($research_index, $oid_current); |
---|
[213] | 824 | |
---|
| 825 | my $port_ifIndex = $snmp_session->var_bind_list->{$oid_current}; |
---|
| 826 | my ($port_index) = reverse split /\./, $oid_current; # last number |
---|
| 827 | printf "PORT1: %s => %s\n", $oid_current, $port_ifIndex if $verbose; |
---|
| 828 | |
---|
[217] | 829 | # prepare next loop item |
---|
[213] | 830 | @args = (-varbindlist => [$oid_current]); |
---|
| 831 | |
---|
| 832 | my $oid_ifName = $OID_NUMBER{ifName} .'.'. $port_ifIndex; |
---|
| 833 | my $result = $snmp_session->get_request(-varbindlist => [$oid_ifName]); |
---|
[217] | 834 | next LOOP_ON_OID_PORT if not defined $result; |
---|
[213] | 835 | |
---|
[214] | 836 | my $current_port_hr = normalize_port_human_readable($result->{$oid_ifName}); |
---|
[213] | 837 | printf "PORT2: $oid_ifName => $current_port_hr\n" if $verbose; |
---|
[214] | 838 | if ($current_port_hr eq $swport_hr) { |
---|
| 839 | print "PORT3: $current_port_hr <-> $port_index\n" if $verbose; |
---|
[217] | 840 | |
---|
| 841 | # return port number ifIndex need by OID portUpDown |
---|
| 842 | $swport_num = $port_ifIndex; # other possible value could be $port_index |
---|
| 843 | last LOOP_ON_OID_PORT; |
---|
[213] | 844 | } |
---|
| 845 | } |
---|
| 846 | return $swport_num; |
---|
| 847 | } |
---|
| 848 | |
---|
[218] | 849 | #--------------------------------------------------------------- |
---|
[151] | 850 | # Load computer database |
---|
[150] | 851 | sub computerdb_load { |
---|
| 852 | my $computerdb = YAML::Syck::LoadFile("$KLASK_DB_FILE"); |
---|
[151] | 853 | |
---|
| 854 | LOOP_ON_IP_ADDRESS: |
---|
| 855 | for my $ip (keys %{$computerdb}) { |
---|
| 856 | |
---|
[185] | 857 | next LOOP_ON_IP_ADDRESS if exists $computerdb->{$ip}{switch_port_hr} and defined $computerdb->{$ip}{switch_port_hr}; |
---|
[198] | 858 | |
---|
[151] | 859 | $computerdb->{$ip}{switch_port_hr} = $computerdb->{$ip}{switch_port}; |
---|
| 860 | } |
---|
| 861 | |
---|
[150] | 862 | return $computerdb; |
---|
| 863 | } |
---|
| 864 | |
---|
[218] | 865 | ################################################################ |
---|
| 866 | # command |
---|
| 867 | ################################################################ |
---|
[2] | 868 | |
---|
[218] | 869 | #--------------------------------------------------------------- |
---|
[2] | 870 | sub cmd_help { |
---|
| 871 | |
---|
[63] | 872 | print <<'END'; |
---|
[224] | 873 | klask - port and search manager for switches, map management |
---|
[2] | 874 | |
---|
[129] | 875 | klask version |
---|
[224] | 876 | klask help |
---|
[129] | 877 | |
---|
[223] | 878 | klask updatedb [--verbose|-v] [--verb-description|-d] [--chk-hostname|-h] [--chk-location|-l] |
---|
[226] | 879 | klask exportdb [--format|-f txt|html] |
---|
[223] | 880 | klask removedb IP* computer* |
---|
| 881 | klask cleandb [--verbose|-v] --day number_of_day --repair-dns |
---|
[2] | 882 | |
---|
[224] | 883 | klask updatesw [--verbose|-v] |
---|
[226] | 884 | klask exportsw [--format|-f txt|dot] |
---|
[45] | 885 | |
---|
[226] | 886 | klask searchdb [--kind|-k host|mac] computer [mac-address] |
---|
[2] | 887 | klask search computer |
---|
[226] | 888 | klask search-mac-on-switch [--verbose|-v] [--vlan|-i vlan-id] switch mac_addr |
---|
[2] | 889 | |
---|
[226] | 890 | klask ip-free [--verbose|-v] [--day|-d days-to-death] [--format|-f txt|html] [vlan_name] |
---|
[69] | 891 | |
---|
[224] | 892 | klask bad-vlan-id [--day|-d days_before_alert] |
---|
[129] | 893 | |
---|
[223] | 894 | klask enable [--verbose|-v] switch port |
---|
| 895 | klask disable [--verbose|-v] switch port |
---|
| 896 | klask status [--verbose|-v] switch port |
---|
[2] | 897 | END |
---|
[63] | 898 | return; |
---|
[2] | 899 | } |
---|
| 900 | |
---|
[218] | 901 | #--------------------------------------------------------------- |
---|
[36] | 902 | sub cmd_version { |
---|
| 903 | |
---|
[63] | 904 | print <<'END'; |
---|
[224] | 905 | klask - port and search manager for switches, map management |
---|
[195] | 906 | Copyright (C) 2005-2017 Gabriel Moreau |
---|
[36] | 907 | |
---|
| 908 | END |
---|
[37] | 909 | print ' $Id: klask 233 2017-03-11 21:56:58Z g7moreau $'."\n"; |
---|
[63] | 910 | return; |
---|
[36] | 911 | } |
---|
| 912 | |
---|
[2] | 913 | sub cmd_search { |
---|
| 914 | my @computer = @_; |
---|
[63] | 915 | |
---|
[4] | 916 | init_switch_names(); #nomme les switchs |
---|
[111] | 917 | fast_ping(@computer); |
---|
[133] | 918 | |
---|
| 919 | LOOP_ON_COMPUTER: |
---|
[2] | 920 | for my $clientname (@computer) { |
---|
| 921 | my %resol_arp = resolve_ip_arp_host($clientname); #resolution arp |
---|
[144] | 922 | my $vlan_name = get_current_vlan_name_for_interface($resol_arp{interface}); |
---|
| 923 | my $vlan_id = get_current_vlan_id($vlan_name); |
---|
| 924 | my %where = find_switch_port($resol_arp{mac_address}, '', $vlan_id); #retrouve l'emplacement |
---|
[133] | 925 | |
---|
| 926 | next LOOP_ON_COMPUTER if $where{switch_description} eq 'unknow' or $resol_arp{hostname_fq} eq 'unknow' or $resol_arp{mac_address} eq 'unknow'; |
---|
| 927 | |
---|
[146] | 928 | printf '%-22s %2s %-30s %-15s %18s', |
---|
[134] | 929 | $where{switch_hostname}, |
---|
[150] | 930 | $where{switch_port_hr}, |
---|
[133] | 931 | $resol_arp{hostname_fq}, |
---|
| 932 | $resol_arp{ipv4_address}, |
---|
| 933 | $resol_arp{mac_address}."\n"; |
---|
[2] | 934 | } |
---|
[63] | 935 | return; |
---|
[2] | 936 | } |
---|
| 937 | |
---|
[218] | 938 | #--------------------------------------------------------------- |
---|
[2] | 939 | sub cmd_searchdb { |
---|
[136] | 940 | my @ARGV = @_; |
---|
| 941 | |
---|
| 942 | my $kind; |
---|
| 943 | |
---|
[138] | 944 | GetOptions( |
---|
[137] | 945 | 'kind=s' => \$kind, |
---|
[136] | 946 | ); |
---|
| 947 | |
---|
| 948 | my %possible_search = ( |
---|
| 949 | host => \&cmd_searchdb_host, |
---|
| 950 | mac => \&cmd_searchdb_mac, |
---|
| 951 | ); |
---|
| 952 | |
---|
[137] | 953 | $kind = 'host' if not defined $possible_search{$kind}; |
---|
[136] | 954 | |
---|
| 955 | $possible_search{$kind}->(@ARGV); |
---|
| 956 | return; |
---|
| 957 | } |
---|
| 958 | |
---|
| 959 | |
---|
[218] | 960 | #--------------------------------------------------------------- |
---|
[136] | 961 | sub cmd_searchdb_host { |
---|
[2] | 962 | my @computer = @_; |
---|
| 963 | |
---|
[111] | 964 | fast_ping(@computer); |
---|
[151] | 965 | my $computerdb = computerdb_load(); |
---|
[63] | 966 | |
---|
[2] | 967 | LOOP_ON_COMPUTER: |
---|
| 968 | for my $clientname (@computer) { |
---|
| 969 | my %resol_arp = resolve_ip_arp_host($clientname); #resolution arp |
---|
| 970 | my $ip = $resol_arp{ipv4_address}; |
---|
[63] | 971 | |
---|
[2] | 972 | next LOOP_ON_COMPUTER unless exists $computerdb->{$ip}; |
---|
[63] | 973 | |
---|
| 974 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{timestamp}; |
---|
[2] | 975 | $year += 1900; |
---|
| 976 | $mon++; |
---|
[63] | 977 | my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; |
---|
[2] | 978 | |
---|
| 979 | printf "%-22s %2s %-30s %-15s %-18s %s\n", |
---|
[134] | 980 | $computerdb->{$ip}{switch_hostname}, |
---|
[150] | 981 | $computerdb->{$ip}{switch_port_hr}, |
---|
[2] | 982 | $computerdb->{$ip}{hostname_fq}, |
---|
| 983 | $ip, |
---|
| 984 | $computerdb->{$ip}{mac_address}, |
---|
| 985 | $date; |
---|
| 986 | } |
---|
[63] | 987 | return; |
---|
[2] | 988 | } |
---|
| 989 | |
---|
[218] | 990 | #--------------------------------------------------------------- |
---|
[136] | 991 | sub cmd_searchdb_mac { |
---|
| 992 | my @mac = map { normalize_mac_address($_) } @_; |
---|
| 993 | |
---|
[151] | 994 | my $computerdb = computerdb_load(); |
---|
[136] | 995 | |
---|
| 996 | LOOP_ON_MAC: |
---|
| 997 | for my $mac (@mac) { |
---|
| 998 | LOOP_ON_COMPUTER: |
---|
| 999 | for my $ip (keys %{$computerdb}) { |
---|
| 1000 | next LOOP_ON_COMPUTER if $mac ne $computerdb->{$ip}{mac_address}; |
---|
[198] | 1001 | |
---|
[136] | 1002 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{timestamp}; |
---|
| 1003 | $year += 1900; |
---|
| 1004 | $mon++; |
---|
| 1005 | my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; |
---|
| 1006 | |
---|
| 1007 | printf "%-22s %2s %-30s %-15s %-18s %s\n", |
---|
| 1008 | $computerdb->{$ip}{switch_hostname}, |
---|
[150] | 1009 | $computerdb->{$ip}{switch_port_hr}, |
---|
[136] | 1010 | $computerdb->{$ip}{hostname_fq}, |
---|
| 1011 | $ip, |
---|
| 1012 | $computerdb->{$ip}{mac_address}, |
---|
| 1013 | $date; |
---|
[138] | 1014 | #next LOOP_ON_MAC; |
---|
[136] | 1015 | } |
---|
| 1016 | |
---|
| 1017 | } |
---|
| 1018 | return; |
---|
| 1019 | } |
---|
| 1020 | |
---|
[218] | 1021 | #--------------------------------------------------------------- |
---|
[2] | 1022 | sub cmd_updatedb { |
---|
[170] | 1023 | @ARGV = @_; |
---|
| 1024 | |
---|
[173] | 1025 | my ($verbose, $verb_description, $check_hostname, $check_location); |
---|
[170] | 1026 | |
---|
| 1027 | GetOptions( |
---|
[172] | 1028 | 'verbose|v' => \$verbose, |
---|
| 1029 | 'verb-description|d' => \$verb_description, |
---|
| 1030 | 'chk-hostname|h' => \$check_hostname, |
---|
[173] | 1031 | 'chk-location|l' => \$check_location, |
---|
[170] | 1032 | ); |
---|
| 1033 | |
---|
| 1034 | my @network = @ARGV; |
---|
[2] | 1035 | @network = get_list_network() if not @network; |
---|
| 1036 | |
---|
[34] | 1037 | test_switchdb_environnement(); |
---|
| 1038 | |
---|
| 1039 | my $computerdb = {}; |
---|
[151] | 1040 | $computerdb = computerdb_load() if -e "$KLASK_DB_FILE"; |
---|
[2] | 1041 | my $timestamp = time; |
---|
[22] | 1042 | |
---|
[2] | 1043 | my %computer_not_detected = (); |
---|
| 1044 | my $timestamp_last_week = $timestamp - (3600 * 24 * 7); |
---|
| 1045 | |
---|
| 1046 | my $number_of_computer = get_list_ip(@network); # + 1; |
---|
[63] | 1047 | my $size_of_database = keys %{$computerdb}; |
---|
[31] | 1048 | $size_of_database = 1 if $size_of_database == 0; |
---|
[2] | 1049 | my $i = 0; |
---|
| 1050 | my $detected_computer = 0; |
---|
[22] | 1051 | |
---|
[173] | 1052 | init_switch_names('yes', $verb_description, $check_hostname, $check_location); #nomme les switchs |
---|
[2] | 1053 | |
---|
[22] | 1054 | { # Remplis le champs portignore des ports d'inter-connection pour chaque switch |
---|
[44] | 1055 | my $switch_connection = YAML::Syck::LoadFile("$KLASK_SW_FILE"); |
---|
[22] | 1056 | my %db_switch_output_port = %{$switch_connection->{output_port}}; |
---|
| 1057 | my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}}; |
---|
| 1058 | my %db_switch_chained_port = (); |
---|
[63] | 1059 | for my $swport (keys %db_switch_connected_on_port) { |
---|
| 1060 | my ($sw_connect,$port_connect) = split m/ : /xms, $swport; |
---|
[22] | 1061 | $db_switch_chained_port{$sw_connect} .= "$port_connect:"; |
---|
| 1062 | } |
---|
[209] | 1063 | for my $sw (@SWITCH_LIST) { |
---|
[22] | 1064 | push @{$sw->{portignore}}, $db_switch_output_port{$sw->{hostname}} if exists $db_switch_output_port{$sw->{hostname}}; |
---|
| 1065 | if ( exists $db_switch_chained_port{$sw->{hostname}} ) { |
---|
| 1066 | chop $db_switch_chained_port{$sw->{hostname}}; |
---|
[63] | 1067 | push @{$sw->{portignore}}, split m/ : /xms, $db_switch_chained_port{$sw->{hostname}}; |
---|
[22] | 1068 | } |
---|
| 1069 | # print "$sw->{hostname} ++ @{$sw->{portignore}}\n"; |
---|
| 1070 | } |
---|
| 1071 | } |
---|
| 1072 | |
---|
[2] | 1073 | my %router_mac_ip = (); |
---|
| 1074 | DETECT_ALL_ROUTER: |
---|
[9] | 1075 | # for my $one_router ('194.254.66.254') { |
---|
| 1076 | for my $one_router ( get_list_main_router(@network) ) { |
---|
[2] | 1077 | my %resol_arp = resolve_ip_arp_host($one_router); |
---|
| 1078 | $router_mac_ip{ $resol_arp{mac_address} } = $resol_arp{ipv4_address}; |
---|
| 1079 | } |
---|
| 1080 | |
---|
| 1081 | ALL_NETWORK: |
---|
[179] | 1082 | for my $current_net (@network) { |
---|
[2] | 1083 | |
---|
[179] | 1084 | my @computer = get_list_ip($current_net); |
---|
| 1085 | my $current_interface = get_current_interface($current_net); |
---|
[2] | 1086 | |
---|
[179] | 1087 | fast_ping(@computer) if get_current_scan_mode($current_net) eq 'active'; |
---|
[2] | 1088 | |
---|
| 1089 | LOOP_ON_COMPUTER: |
---|
| 1090 | for my $one_computer (@computer) { |
---|
| 1091 | $i++; |
---|
[49] | 1092 | |
---|
[63] | 1093 | my $total_percent = int (($i*100)/$number_of_computer); |
---|
| 1094 | |
---|
[2] | 1095 | my $localtime = time - $timestamp; |
---|
[63] | 1096 | my ($sec,$min) = localtime $localtime; |
---|
[2] | 1097 | |
---|
| 1098 | my $time_elapse = 0; |
---|
| 1099 | $time_elapse = $localtime * ( 100 - $total_percent) / $total_percent if $total_percent != 0; |
---|
[63] | 1100 | my ($sec_elapse,$min_elapse) = localtime $time_elapse; |
---|
[2] | 1101 | |
---|
| 1102 | printf "\rComputer scanned: %4i/%i (%2i%%)", $i, $number_of_computer, $total_percent; |
---|
[63] | 1103 | printf ', detected: %4i/%i (%2i%%)', $detected_computer, $size_of_database, int(($detected_computer*100)/$size_of_database); |
---|
| 1104 | printf ' [Time: %02i:%02i / %02i:%02i]', int($localtime/60), $localtime % 60, int($time_elapse/60), $time_elapse % 60; |
---|
[96] | 1105 | printf ' %-8s %-14s', $current_interface, $one_computer; |
---|
[2] | 1106 | |
---|
[186] | 1107 | my $already_exist = exists $computerdb->{$one_computer} ? 'yes' : 'no'; |
---|
| 1108 | my %resol_arp = resolve_ip_arp_host($one_computer, $current_interface, 'fast', $already_exist); |
---|
[63] | 1109 | |
---|
[9] | 1110 | # do not search on router connection (why ?) |
---|
[2] | 1111 | if ( exists $router_mac_ip{$resol_arp{mac_address}}) { |
---|
[179] | 1112 | $computer_not_detected{$one_computer} = $current_net; |
---|
[2] | 1113 | next LOOP_ON_COMPUTER; |
---|
| 1114 | } |
---|
| 1115 | |
---|
[9] | 1116 | # do not search on switch inter-connection |
---|
[196] | 1117 | if (exists $SWITCH_LEVEL{$resol_arp{hostname_fq}}) { |
---|
[179] | 1118 | $computer_not_detected{$one_computer} = $current_net; |
---|
[2] | 1119 | next LOOP_ON_COMPUTER; |
---|
| 1120 | } |
---|
| 1121 | |
---|
[63] | 1122 | my $switch_proposal = q{}; |
---|
[2] | 1123 | if (exists $computerdb->{$resol_arp{ipv4_address}} and exists $computerdb->{$resol_arp{ipv4_address}}{switch_hostname}) { |
---|
| 1124 | $switch_proposal = $computerdb->{$resol_arp{ipv4_address}}{switch_hostname}; |
---|
| 1125 | } |
---|
| 1126 | |
---|
[3] | 1127 | # do not have a mac address |
---|
| 1128 | if ($resol_arp{mac_address} eq 'unknow' or (exists $resol_arp{timestamps} and $resol_arp{timestamps} < ($timestamp - 3 * 3600))) { |
---|
[179] | 1129 | $computer_not_detected{$one_computer} = $current_net; |
---|
[3] | 1130 | next LOOP_ON_COMPUTER; |
---|
| 1131 | } |
---|
| 1132 | |
---|
[147] | 1133 | my $vlan_name = get_current_vlan_name_for_interface($resol_arp{interface}); |
---|
| 1134 | my $vlan_id = get_current_vlan_id($vlan_name); |
---|
| 1135 | my %where = find_switch_port($resol_arp{mac_address},$switch_proposal,$vlan_id); |
---|
[2] | 1136 | |
---|
| 1137 | #192.168.24.156: |
---|
| 1138 | # arp: 00:0B:DB:D5:F6:65 |
---|
| 1139 | # hostname: pcroyon.hmg.priv |
---|
| 1140 | # port: 5 |
---|
| 1141 | # switch: sw-batH-legi:hp2524 |
---|
| 1142 | # timestamp: 1164355525 |
---|
| 1143 | |
---|
| 1144 | # do not have a mac address |
---|
[3] | 1145 | # if ($resol_arp{mac_address} eq 'unknow') { |
---|
| 1146 | # $computer_not_detected{$one_computer} = $current_interface; |
---|
| 1147 | # next LOOP_ON_COMPUTER; |
---|
| 1148 | # } |
---|
[2] | 1149 | |
---|
| 1150 | # detected on a switch |
---|
| 1151 | if ($where{switch_description} ne 'unknow') { |
---|
| 1152 | $detected_computer++; |
---|
| 1153 | $computerdb->{$resol_arp{ipv4_address}} = { |
---|
| 1154 | hostname_fq => $resol_arp{hostname_fq}, |
---|
| 1155 | mac_address => $resol_arp{mac_address}, |
---|
| 1156 | switch_hostname => $where{switch_hostname}, |
---|
| 1157 | switch_description => $where{switch_description}, |
---|
| 1158 | switch_port => $where{switch_port}, |
---|
[150] | 1159 | switch_port_hr => $where{switch_port_hr}, |
---|
[2] | 1160 | timestamp => $timestamp, |
---|
[179] | 1161 | network => $current_net, |
---|
[2] | 1162 | }; |
---|
| 1163 | next LOOP_ON_COMPUTER; |
---|
| 1164 | } |
---|
| 1165 | |
---|
| 1166 | # new in the database but where it is ? |
---|
| 1167 | if (not exists $computerdb->{$resol_arp{ipv4_address}}) { |
---|
| 1168 | $detected_computer++; |
---|
| 1169 | $computerdb->{$resol_arp{ipv4_address}} = { |
---|
| 1170 | hostname_fq => $resol_arp{hostname_fq}, |
---|
| 1171 | mac_address => $resol_arp{mac_address}, |
---|
| 1172 | switch_hostname => $where{switch_hostname}, |
---|
| 1173 | switch_description => $where{switch_description}, |
---|
| 1174 | switch_port => $where{switch_port}, |
---|
[150] | 1175 | switch_port_hr => $where{switch_port_hr}, |
---|
[2] | 1176 | timestamp => $resol_arp{timestamp}, |
---|
[179] | 1177 | network => $current_net, |
---|
[2] | 1178 | }; |
---|
| 1179 | } |
---|
| 1180 | |
---|
| 1181 | # mise a jour du nom de la machine si modification dans le dns |
---|
| 1182 | $computerdb->{$resol_arp{ipv4_address}}{hostname_fq} = $resol_arp{hostname_fq}; |
---|
[63] | 1183 | |
---|
[2] | 1184 | # mise à jour de la date de détection si détection plus récente par arpwatch |
---|
| 1185 | $computerdb->{$resol_arp{ipv4_address}}{timestamp} = $resol_arp{timestamp} if exists $resol_arp{timestamp} and $computerdb->{$resol_arp{ipv4_address}}{timestamp} < $resol_arp{timestamp}; |
---|
| 1186 | |
---|
| 1187 | # relance un arping sur la machine si celle-ci n'a pas été détectée depuis plus d'une semaine |
---|
| 1188 | # push @computer_not_detected, $resol_arp{ipv4_address} if $computerdb->{$resol_arp{ipv4_address}}{timestamp} < $timestamp_last_week; |
---|
[179] | 1189 | $computer_not_detected{$resol_arp{ipv4_address}} = $current_net if $computerdb->{$resol_arp{ipv4_address}}{timestamp} < $timestamp_last_week; |
---|
[63] | 1190 | |
---|
[2] | 1191 | } |
---|
| 1192 | } |
---|
| 1193 | |
---|
| 1194 | # final end of line at the end of the loop |
---|
| 1195 | printf "\n"; |
---|
| 1196 | |
---|
[13] | 1197 | my $dirdb = $KLASK_DB_FILE; |
---|
[63] | 1198 | $dirdb =~ s{ / [^/]* $}{}xms; |
---|
[2] | 1199 | mkdir "$dirdb", 0755 unless -d "$dirdb"; |
---|
[44] | 1200 | YAML::Syck::DumpFile("$KLASK_DB_FILE", $computerdb); |
---|
[2] | 1201 | |
---|
| 1202 | for my $one_computer (keys %computer_not_detected) { |
---|
[179] | 1203 | my $current_net = $computer_not_detected{$one_computer}; |
---|
| 1204 | my $current_interface = get_current_interface($current_net); |
---|
| 1205 | system "arping -c 1 -w 1 -rR -i $current_interface $one_computer &>/dev/null" if get_current_scan_mode($current_net) eq 'active'; |
---|
| 1206 | # print "arping -c 1 -w 1 -rR -i $current_interface $one_computer 2>/dev/null\n"; |
---|
[2] | 1207 | } |
---|
[63] | 1208 | return; |
---|
[2] | 1209 | } |
---|
| 1210 | |
---|
[218] | 1211 | #--------------------------------------------------------------- |
---|
[2] | 1212 | sub cmd_removedb { |
---|
| 1213 | my @computer = @_; |
---|
[34] | 1214 | |
---|
| 1215 | test_maindb_environnement(); |
---|
| 1216 | |
---|
[151] | 1217 | my $computerdb = computerdb_load(); |
---|
[2] | 1218 | |
---|
| 1219 | LOOP_ON_COMPUTER: |
---|
| 1220 | for my $one_computer (@computer) { |
---|
| 1221 | |
---|
[66] | 1222 | if ( $one_computer =~ m/^ $RE_IPv4_ADDRESS $/xms |
---|
| 1223 | and exists $computerdb->{$one_computer} ) { |
---|
| 1224 | delete $computerdb->{$one_computer}; |
---|
| 1225 | next; |
---|
| 1226 | } |
---|
| 1227 | |
---|
[2] | 1228 | my %resol_arp = resolve_ip_arp_host($one_computer); |
---|
| 1229 | |
---|
| 1230 | delete $computerdb->{$resol_arp{ipv4_address}} if exists $computerdb->{$resol_arp{ipv4_address}}; |
---|
| 1231 | } |
---|
| 1232 | |
---|
[13] | 1233 | my $dirdb = $KLASK_DB_FILE; |
---|
[63] | 1234 | $dirdb =~ s{ / [^/]* $}{}xms; |
---|
[2] | 1235 | mkdir "$dirdb", 0755 unless -d "$dirdb"; |
---|
[44] | 1236 | YAML::Syck::DumpFile("$KLASK_DB_FILE", $computerdb); |
---|
[63] | 1237 | return; |
---|
[2] | 1238 | } |
---|
| 1239 | |
---|
[218] | 1240 | #--------------------------------------------------------------- |
---|
[74] | 1241 | sub cmd_cleandb { |
---|
[110] | 1242 | my @ARGV = @_; |
---|
[74] | 1243 | |
---|
| 1244 | my $days_to_clean = 15; |
---|
[188] | 1245 | my $repairdns; |
---|
[74] | 1246 | my $verbose; |
---|
| 1247 | my $database_has_changed; |
---|
| 1248 | |
---|
[138] | 1249 | GetOptions( |
---|
[74] | 1250 | 'day|d=i' => \$days_to_clean, |
---|
| 1251 | 'verbose|v' => \$verbose, |
---|
[190] | 1252 | 'repair-dns|r' => \$repairdns, |
---|
[74] | 1253 | ); |
---|
| 1254 | |
---|
| 1255 | my @vlan_name = get_list_network(); |
---|
| 1256 | |
---|
[188] | 1257 | my $computerdb = computerdb_load(); |
---|
[74] | 1258 | my $timestamp = time; |
---|
| 1259 | |
---|
| 1260 | my $timestamp_barrier = 3600 * 24 * $days_to_clean; |
---|
[106] | 1261 | my $timestamp_3month = 3600 * 24 * 90; |
---|
[74] | 1262 | |
---|
[104] | 1263 | my %mactimedb = (); |
---|
[74] | 1264 | ALL_VLAN: |
---|
[109] | 1265 | for my $vlan (shuffle @vlan_name) { |
---|
[74] | 1266 | |
---|
[109] | 1267 | my @ip_list = shuffle get_list_ip($vlan); |
---|
[198] | 1268 | |
---|
[74] | 1269 | LOOP_ON_IP_ADDRESS: |
---|
| 1270 | for my $ip (@ip_list) { |
---|
| 1271 | |
---|
| 1272 | next LOOP_ON_IP_ADDRESS if |
---|
| 1273 | not exists $computerdb->{$ip}; |
---|
[198] | 1274 | |
---|
[74] | 1275 | #&& $computerdb->{$ip}{timestamp} > $timestamp_barrier; |
---|
[104] | 1276 | my $ip_timestamp = $computerdb->{$ip}{timestamp}; |
---|
| 1277 | my $ip_mac = $computerdb->{$ip}{mac_address}; |
---|
| 1278 | my $ip_hostname_fq = $computerdb->{$ip}{hostname_fq}; |
---|
| 1279 | |
---|
| 1280 | $mactimedb{$ip_mac} ||= { |
---|
| 1281 | ip => $ip, |
---|
| 1282 | timestamp => $ip_timestamp, |
---|
| 1283 | vlan => $vlan, |
---|
| 1284 | hostname_fq => $ip_hostname_fq, |
---|
| 1285 | }; |
---|
[198] | 1286 | |
---|
[108] | 1287 | if ( |
---|
| 1288 | ( $mactimedb{$ip_mac}->{timestamp} - $ip_timestamp > $timestamp_barrier |
---|
| 1289 | or ( |
---|
| 1290 | $mactimedb{$ip_mac}->{timestamp} > $ip_timestamp |
---|
| 1291 | and $timestamp - $mactimedb{$ip_mac}->{timestamp} > $timestamp_3month |
---|
| 1292 | ) |
---|
| 1293 | ) |
---|
[105] | 1294 | and ( |
---|
[118] | 1295 | not $mactimedb{$ip_mac}->{hostname_fq} =~ m/$RE_FLOAT_HOSTNAME/ |
---|
| 1296 | or $ip_hostname_fq =~ m/$RE_FLOAT_HOSTNAME/ |
---|
[105] | 1297 | )) { |
---|
[74] | 1298 | print "remove ip $ip\n" if $verbose; |
---|
| 1299 | delete $computerdb->{$ip}; |
---|
| 1300 | $database_has_changed++; |
---|
| 1301 | } |
---|
| 1302 | |
---|
[108] | 1303 | elsif ( |
---|
| 1304 | ( $ip_timestamp - $mactimedb{$ip_mac}->{timestamp} > $timestamp_barrier |
---|
| 1305 | or ( |
---|
| 1306 | $ip_timestamp > $mactimedb{$ip_mac}->{timestamp} |
---|
| 1307 | and $timestamp - $ip_timestamp > $timestamp_3month |
---|
| 1308 | ) |
---|
| 1309 | ) |
---|
[105] | 1310 | and ( |
---|
[118] | 1311 | not $ip_hostname_fq =~ m/$RE_FLOAT_HOSTNAME/ |
---|
| 1312 | or $mactimedb{$ip_mac}->{hostname_fq} =~ m/$RE_FLOAT_HOSTNAME/ |
---|
[105] | 1313 | )) { |
---|
[104] | 1314 | print "remove ip ".$mactimedb{$ip_mac}->{ip}."\n" if $verbose; |
---|
| 1315 | delete $computerdb->{$mactimedb{$ip_mac}->{ip}}; |
---|
[74] | 1316 | $database_has_changed++; |
---|
| 1317 | } |
---|
| 1318 | |
---|
[104] | 1319 | if ( $ip_timestamp > $mactimedb{$ip_mac}->{timestamp}) { |
---|
| 1320 | $mactimedb{$ip_mac} = { |
---|
| 1321 | ip => $ip, |
---|
| 1322 | timestamp => $ip_timestamp, |
---|
| 1323 | vlan => $vlan, |
---|
| 1324 | hostname_fq => $ip_hostname_fq, |
---|
| 1325 | }; |
---|
[74] | 1326 | } |
---|
| 1327 | } |
---|
| 1328 | } |
---|
| 1329 | |
---|
[188] | 1330 | if ($repairdns) { # Search and update unkown computer in reverse DNS |
---|
[189] | 1331 | LOOP_ON_IP_ADDRESS: |
---|
| 1332 | for my $ip (keys %{$computerdb}) { |
---|
| 1333 | next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} ne 'unknow'; |
---|
[188] | 1334 | |
---|
[189] | 1335 | my $packed_ip = scalar gethostbyname($ip); |
---|
| 1336 | next LOOP_ON_IP_ADDRESS if not defined $packed_ip; |
---|
[188] | 1337 | |
---|
| 1338 | my $hostname_fq = scalar gethostbyaddr($packed_ip, AF_INET); |
---|
[189] | 1339 | next LOOP_ON_IP_ADDRESS if not defined $hostname_fq; |
---|
[188] | 1340 | |
---|
[189] | 1341 | $computerdb->{$ip}{hostname_fq} = $hostname_fq; |
---|
[188] | 1342 | $database_has_changed++; |
---|
| 1343 | } |
---|
| 1344 | } |
---|
| 1345 | |
---|
[74] | 1346 | if ( $database_has_changed ) { |
---|
| 1347 | my $dirdb = $KLASK_DB_FILE; |
---|
| 1348 | $dirdb =~ s{ / [^/]* $}{}xms; |
---|
| 1349 | mkdir "$dirdb", 0755 unless -d "$dirdb"; |
---|
| 1350 | YAML::Syck::DumpFile("$KLASK_DB_FILE", $computerdb); |
---|
| 1351 | } |
---|
| 1352 | return; |
---|
| 1353 | } |
---|
| 1354 | |
---|
[218] | 1355 | #--------------------------------------------------------------- |
---|
[2] | 1356 | sub cmd_exportdb { |
---|
[113] | 1357 | @ARGV = @_; |
---|
[45] | 1358 | |
---|
| 1359 | my $format = 'txt'; |
---|
| 1360 | |
---|
[138] | 1361 | GetOptions( |
---|
[45] | 1362 | 'format|f=s' => \$format, |
---|
| 1363 | ); |
---|
| 1364 | |
---|
| 1365 | my %possible_format = ( |
---|
| 1366 | txt => \&cmd_exportdb_txt, |
---|
| 1367 | html => \&cmd_exportdb_html, |
---|
| 1368 | ); |
---|
| 1369 | |
---|
| 1370 | $format = 'txt' if not defined $possible_format{$format}; |
---|
[63] | 1371 | |
---|
[45] | 1372 | $possible_format{$format}->(@ARGV); |
---|
[63] | 1373 | return; |
---|
[45] | 1374 | } |
---|
| 1375 | |
---|
[218] | 1376 | #--------------------------------------------------------------- |
---|
[45] | 1377 | sub cmd_exportdb_txt { |
---|
[34] | 1378 | test_maindb_environnement(); |
---|
| 1379 | |
---|
[151] | 1380 | my $computerdb = computerdb_load(); |
---|
[2] | 1381 | |
---|
[233] | 1382 | printf "%-28s %8s %-40s %-15s %-18s %-16s %s\n", qw(Switch Port Hostname-FQ IPv4-Address MAC-Address Date VLAN); |
---|
[78] | 1383 | print "--------------------------------------------------------------------------------------------------------------------------------------------\n"; |
---|
[2] | 1384 | |
---|
| 1385 | LOOP_ON_IP_ADDRESS: |
---|
[165] | 1386 | for my $ip (Net::Netmask::sort_by_ip_address(keys %{$computerdb})) { |
---|
[63] | 1387 | |
---|
[2] | 1388 | # to be improve in the future |
---|
| 1389 | next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself ! |
---|
| 1390 | |
---|
[63] | 1391 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{timestamp}; |
---|
[2] | 1392 | $year += 1900; |
---|
| 1393 | $mon++; |
---|
[63] | 1394 | my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; |
---|
[2] | 1395 | |
---|
[177] | 1396 | my $vlan = ''; |
---|
| 1397 | $vlan = $computerdb->{$ip}{network}.'('.get_current_vlan_id($computerdb->{$ip}{network}).')' if $computerdb->{$ip}{network}; |
---|
| 1398 | |
---|
[233] | 1399 | my $arrow ='<-----------'; |
---|
| 1400 | $arrow ='<===========' if $computerdb->{$ip}{switch_port_hr} =~ m/^(Trk|Br|Po)/; |
---|
[232] | 1401 | |
---|
[233] | 1402 | printf "%-28s %8s %12s %-40s %-15s %-18s %-16s %s\n", |
---|
[2] | 1403 | $computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}, |
---|
[150] | 1404 | $computerdb->{$ip}{switch_port_hr}, |
---|
[232] | 1405 | $arrow, |
---|
[2] | 1406 | $computerdb->{$ip}{hostname_fq}, |
---|
| 1407 | $ip, |
---|
| 1408 | $computerdb->{$ip}{mac_address}, |
---|
[70] | 1409 | $date, |
---|
[177] | 1410 | $vlan; |
---|
[2] | 1411 | } |
---|
[63] | 1412 | return; |
---|
[2] | 1413 | } |
---|
| 1414 | |
---|
[218] | 1415 | #--------------------------------------------------------------- |
---|
[45] | 1416 | sub cmd_exportdb_html { |
---|
| 1417 | test_maindb_environnement(); |
---|
| 1418 | |
---|
[151] | 1419 | my $computerdb = computerdb_load(); |
---|
[45] | 1420 | |
---|
| 1421 | #<link rel="stylesheet" type="text/css" href="style-klask.css" /> |
---|
| 1422 | #<script src="sorttable-klask.js"></script> |
---|
| 1423 | |
---|
[63] | 1424 | print <<'END_HTML'; |
---|
[73] | 1425 | <table class="sortable" summary="Klask Host Database"> |
---|
[72] | 1426 | <caption>Klask Host Database</caption> |
---|
[45] | 1427 | <thead> |
---|
| 1428 | <tr> |
---|
[73] | 1429 | <th scope="col" class="klask-header-left">Switch</th> |
---|
[45] | 1430 | <th scope="col" class="sorttable_nosort">Port</th> |
---|
[164] | 1431 | <th scope="col" class="sorttable_nosort" colspan="2">Link</th> |
---|
[73] | 1432 | <th scope="col" class="sorttable_alpha">Hostname-FQ</th> |
---|
[45] | 1433 | <th scope="col" class="hklask-ipv4">IPv4-Address</th> |
---|
[67] | 1434 | <th scope="col" class="sorttable_alpha">MAC-Address</th> |
---|
[71] | 1435 | <th scope="col" class="sorttable_alpha">VLAN</th> |
---|
[73] | 1436 | <th scope="col" class="klask-header-right">Date</th> |
---|
[45] | 1437 | </tr> |
---|
| 1438 | </thead> |
---|
| 1439 | <tfoot> |
---|
| 1440 | <tr> |
---|
[73] | 1441 | <th scope="col" class="klask-footer-left">Switch</th> |
---|
[45] | 1442 | <th scope="col" class="fklask-port">Port</th> |
---|
[163] | 1443 | <th scope="col" class="fklask-link" colspan="2">Link</th> |
---|
[73] | 1444 | <th scope="col" class="fklask-hostname">Hostname-FQ</th> |
---|
[45] | 1445 | <th scope="col" class="fklask-ipv4">IPv4-Address</th> |
---|
| 1446 | <th scope="col" class="fklask-mac">MAC-Address</th> |
---|
[71] | 1447 | <th scope="col" class="fklask-vlan">VLAN</th> |
---|
[73] | 1448 | <th scope="col" class="klask-footer-right">Date</th> |
---|
[45] | 1449 | </tr> |
---|
| 1450 | </tfoot> |
---|
| 1451 | <tbody> |
---|
[63] | 1452 | END_HTML |
---|
[45] | 1453 | |
---|
| 1454 | my %mac_count = (); |
---|
| 1455 | LOOP_ON_IP_ADDRESS: |
---|
[165] | 1456 | for my $ip (keys %{$computerdb}) { |
---|
[63] | 1457 | |
---|
[45] | 1458 | # to be improve in the future |
---|
| 1459 | next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself ! |
---|
[63] | 1460 | |
---|
[45] | 1461 | $mac_count{$computerdb->{$ip}{mac_address}}++; |
---|
| 1462 | } |
---|
| 1463 | |
---|
| 1464 | my $typerow = 'even'; |
---|
| 1465 | |
---|
| 1466 | LOOP_ON_IP_ADDRESS: |
---|
[165] | 1467 | for my $ip (Net::Netmask::sort_by_ip_address(keys %{$computerdb})) { |
---|
[63] | 1468 | |
---|
[45] | 1469 | # to be improve in the future |
---|
| 1470 | next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself ! |
---|
| 1471 | |
---|
[63] | 1472 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{timestamp}; |
---|
[45] | 1473 | $year += 1900; |
---|
| 1474 | $mon++; |
---|
[63] | 1475 | my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; |
---|
[45] | 1476 | |
---|
| 1477 | # $odd_or_even++; |
---|
| 1478 | # my $typerow = $odd_or_even % 2 ? 'odd' : 'even'; |
---|
[63] | 1479 | $typerow = $typerow eq 'even' ? 'odd' : 'even'; |
---|
[45] | 1480 | |
---|
[163] | 1481 | my $arrow ='←'; |
---|
| 1482 | $arrow ='⇐' if $computerdb->{$ip}{switch_port_hr} =~ m/^(Trk|Br|Po)/; |
---|
| 1483 | |
---|
[45] | 1484 | my $switch_hostname = $computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description} || 'unkown'; |
---|
| 1485 | chomp $switch_hostname; |
---|
[150] | 1486 | my $switch_hostname_sort = sprintf '%s %3s' ,$switch_hostname, $computerdb->{$ip}{switch_port_hr}; |
---|
[45] | 1487 | |
---|
[63] | 1488 | my $ip_sort = sprintf '%03i%03i%03i%03i', split m/ \. /xms, $ip; |
---|
[45] | 1489 | |
---|
[63] | 1490 | my $mac_sort = sprintf '%04i-%s', 9999 - $mac_count{$computerdb->{$ip}{mac_address}}, $computerdb->{$ip}{mac_address}; |
---|
[45] | 1491 | |
---|
[63] | 1492 | $computerdb->{$ip}{hostname_fq} = 'unknow' if $computerdb->{$ip}{hostname_fq} =~ m/^ \d+ \. \d+ \. \d+ \. \d+ $/xms; |
---|
| 1493 | my ( $host_short ) = split m/ \. /xms, $computerdb->{$ip}{hostname_fq}; |
---|
[45] | 1494 | |
---|
[178] | 1495 | my $vlan = ''; |
---|
| 1496 | $vlan = $computerdb->{$ip}{network}.' ('.get_current_vlan_id($computerdb->{$ip}{network}).')' if $computerdb->{$ip}{network}; |
---|
[71] | 1497 | |
---|
[63] | 1498 | print <<"END_HTML"; |
---|
[45] | 1499 | <tr class="$typerow"> |
---|
| 1500 | <td sorttable_customkey="$switch_hostname_sort">$switch_hostname</td> |
---|
[150] | 1501 | <td class="bklask-port">$computerdb->{$ip}{switch_port_hr}</td> |
---|
[163] | 1502 | <td colspan="2">$arrow</td> |
---|
[45] | 1503 | <td sorttable_customkey="$host_short">$computerdb->{$ip}{hostname_fq}</td> |
---|
| 1504 | <td sorttable_customkey="$ip_sort">$ip</td> |
---|
| 1505 | <td sorttable_customkey="$mac_sort">$computerdb->{$ip}{mac_address}</td> |
---|
[71] | 1506 | <td>$vlan</td> |
---|
[45] | 1507 | <td>$date</td> |
---|
| 1508 | </tr> |
---|
[63] | 1509 | END_HTML |
---|
[45] | 1510 | } |
---|
| 1511 | |
---|
| 1512 | my $switch_connection = YAML::Syck::LoadFile("$KLASK_SW_FILE"); |
---|
| 1513 | |
---|
| 1514 | my %db_switch_output_port = %{$switch_connection->{output_port}}; |
---|
| 1515 | my %db_switch_parent = %{$switch_connection->{parent}}; |
---|
| 1516 | my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}}; |
---|
| 1517 | my %db_switch = %{$switch_connection->{switch_db}}; |
---|
| 1518 | |
---|
| 1519 | for my $sw (sort keys %db_switch_output_port) { |
---|
| 1520 | |
---|
[63] | 1521 | my $switch_hostname_sort = sprintf '%s %3s' ,$sw, $db_switch_output_port{$sw}; |
---|
[45] | 1522 | |
---|
[63] | 1523 | $typerow = $typerow eq 'even' ? 'odd' : 'even'; |
---|
[45] | 1524 | |
---|
[163] | 1525 | my $arrow ='⇾'; |
---|
| 1526 | $arrow ='⇨' if $db_switch_output_port{$sw} =~ m/^(Trk|Br|Po)/; |
---|
| 1527 | |
---|
[45] | 1528 | if (exists $db_switch_parent{$sw}) { |
---|
[221] | 1529 | my $mac_address = $db_switch{$db_switch_parent{$sw}->{'switch'}}->{mac_address}; |
---|
| 1530 | my $ipv4_address = $db_switch{$db_switch_parent{$sw}->{'switch'}}->{ipv4_address}; |
---|
| 1531 | my $timestamp = $db_switch{$db_switch_parent{$sw}->{'switch'}}->{timestamp}; |
---|
[45] | 1532 | |
---|
[163] | 1533 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $timestamp; |
---|
| 1534 | $year += 1900; |
---|
| 1535 | $mon++; |
---|
| 1536 | my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; |
---|
[45] | 1537 | |
---|
[205] | 1538 | my $ip_sort = sprintf '%03i%03i%03i%03i', split m/ [\.\*] /xms, $ipv4_address; # \* for fake-ip |
---|
[45] | 1539 | |
---|
[163] | 1540 | my $mac_sort = sprintf '%04i-%s', 9999, $mac_address; |
---|
[45] | 1541 | |
---|
[221] | 1542 | my ( $host_short ) = sprintf '%s %3s' , split(m/ \. /xms, $db_switch_parent{$sw}->{'switch'}, 1), $db_switch_parent{$sw}->{port_hr}; |
---|
[45] | 1543 | |
---|
[163] | 1544 | print <<"END_HTML"; |
---|
[45] | 1545 | <tr class="$typerow"> |
---|
| 1546 | <td sorttable_customkey="$switch_hostname_sort">$sw</td> |
---|
[162] | 1547 | <td class="bklask-port">$db_switch_output_port{$sw}</td> |
---|
[164] | 1548 | <td>$arrow</td><td>$db_switch_parent{$sw}->{port_hr}</td> |
---|
[221] | 1549 | <td sorttable_customkey="$host_short">$db_switch_parent{$sw}->{'switch'}</td> |
---|
[45] | 1550 | <td sorttable_customkey="$ip_sort">$ipv4_address</td> |
---|
| 1551 | <td sorttable_customkey="$mac_sort">$mac_address</td> |
---|
[71] | 1552 | <td></td> |
---|
[45] | 1553 | <td>$date</td> |
---|
| 1554 | </tr> |
---|
[63] | 1555 | END_HTML |
---|
[45] | 1556 | } |
---|
| 1557 | else { |
---|
[63] | 1558 | print <<"END_HTML"; |
---|
[45] | 1559 | <tr class="$typerow"> |
---|
| 1560 | <td sorttable_customkey="$switch_hostname_sort">$sw</td> |
---|
[162] | 1561 | <td class="bklask-port">$db_switch_output_port{$sw}</td> |
---|
[164] | 1562 | <td>$arrow</td><td></td> |
---|
[162] | 1563 | <td sorttable_customkey="router">router</td> |
---|
[45] | 1564 | <td sorttable_customkey="999999999999"></td> |
---|
| 1565 | <td sorttable_customkey="99999"></td> |
---|
| 1566 | <td></td> |
---|
[71] | 1567 | <td></td> |
---|
[45] | 1568 | </tr> |
---|
[63] | 1569 | END_HTML |
---|
[45] | 1570 | } |
---|
| 1571 | } |
---|
| 1572 | |
---|
| 1573 | for my $swport (sort keys %db_switch_connected_on_port) { |
---|
[63] | 1574 | my ($sw_connect,$port_connect) = split m/ : /xms, $swport; |
---|
[45] | 1575 | for my $sw (keys %{$db_switch_connected_on_port{$swport}}) { |
---|
| 1576 | |
---|
[63] | 1577 | my $switch_hostname_sort = sprintf '%s %3s' ,$sw_connect, $port_connect; |
---|
[45] | 1578 | |
---|
[163] | 1579 | my $mac_address = $db_switch{$sw}->{mac_address}; |
---|
| 1580 | my $ipv4_address = $db_switch{$sw}->{ipv4_address}; |
---|
| 1581 | my $timestamp = $db_switch{$sw}->{timestamp}; |
---|
[45] | 1582 | |
---|
[163] | 1583 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $timestamp; |
---|
| 1584 | $year += 1900; |
---|
| 1585 | $mon++; |
---|
| 1586 | my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year,$mon,$mday,$hour,$min; |
---|
[45] | 1587 | |
---|
[205] | 1588 | my $ip_sort = sprintf '%03i%03i%03i%03i', split m/ [\.\*] /xms, $ipv4_address; # \* for fake-ip |
---|
[45] | 1589 | |
---|
[163] | 1590 | my $mac_sort = sprintf '%04i-%s', 9999, $mac_address; |
---|
[45] | 1591 | |
---|
[163] | 1592 | $typerow = $typerow eq 'even' ? 'odd' : 'even'; |
---|
[45] | 1593 | |
---|
[163] | 1594 | my $arrow ='⇽'; |
---|
| 1595 | $arrow ='⇦' if $port_connect =~ m/^(Trk|Br|Po)/; |
---|
| 1596 | |
---|
[45] | 1597 | if (exists $db_switch_output_port{$sw}) { |
---|
| 1598 | |
---|
[63] | 1599 | my ( $host_short ) = sprintf '%s %3s' , split( m/\./xms, $sw, 1), $db_switch_output_port{$sw}; |
---|
[45] | 1600 | |
---|
[63] | 1601 | print <<"END_HTML"; |
---|
[45] | 1602 | <tr class="$typerow"> |
---|
| 1603 | <td sorttable_customkey="$switch_hostname_sort">$sw_connect</td> |
---|
[162] | 1604 | <td class="bklask-port">$port_connect</td> |
---|
[163] | 1605 | <td>$arrow</td><td>$db_switch_output_port{$sw}</td> |
---|
[162] | 1606 | <td sorttable_customkey="$host_short">$sw</td> |
---|
[45] | 1607 | <td sorttable_customkey="$ip_sort">$ipv4_address</td> |
---|
| 1608 | <td sorttable_customkey="$mac_sort">$mac_address</td> |
---|
[71] | 1609 | <td></td> |
---|
[45] | 1610 | <td>$date</td> |
---|
| 1611 | </tr> |
---|
[63] | 1612 | END_HTML |
---|
[45] | 1613 | } |
---|
| 1614 | else { |
---|
[63] | 1615 | print <<"END_HTML"; |
---|
[45] | 1616 | <tr class="$typerow"> |
---|
| 1617 | <td sorttable_customkey="$switch_hostname_sort">$sw_connect</td> |
---|
[162] | 1618 | <td class="bklask-port">$port_connect</td> |
---|
[163] | 1619 | <td>$arrow</td><td></td> |
---|
[162] | 1620 | <td sorttable_customkey="$sw">$sw</td> |
---|
[45] | 1621 | <td sorttable_customkey="">$ipv4_address</td> |
---|
| 1622 | <td sorttable_customkey="">$mac_address</td> |
---|
[71] | 1623 | <td></td> |
---|
[45] | 1624 | <td>$date</td> |
---|
| 1625 | </tr> |
---|
[63] | 1626 | END_HTML |
---|
[45] | 1627 | } |
---|
| 1628 | } |
---|
| 1629 | } |
---|
| 1630 | |
---|
[63] | 1631 | print <<'END_HTML'; |
---|
[45] | 1632 | </tbody> |
---|
| 1633 | </table> |
---|
[63] | 1634 | END_HTML |
---|
| 1635 | return; |
---|
[45] | 1636 | } |
---|
| 1637 | |
---|
[218] | 1638 | #--------------------------------------------------------------- |
---|
[133] | 1639 | sub cmd_bad_vlan_id { |
---|
[224] | 1640 | @ARGV = @_; |
---|
| 1641 | |
---|
| 1642 | my $days_before_alert = $DEFAULT{'days-before-alert'} || 15; |
---|
| 1643 | my $verbose; |
---|
| 1644 | |
---|
| 1645 | GetOptions( |
---|
| 1646 | 'day|d=i' => \$days_before_alert, |
---|
| 1647 | ); |
---|
| 1648 | |
---|
[114] | 1649 | test_maindb_environnement(); |
---|
| 1650 | |
---|
[151] | 1651 | my $computerdb = computerdb_load(); |
---|
[114] | 1652 | |
---|
[132] | 1653 | # create a database with the most recent computer by switch port |
---|
[211] | 1654 | my %switchportdb = (); |
---|
[114] | 1655 | LOOP_ON_IP_ADDRESS: |
---|
[165] | 1656 | for my $ip (keys %{$computerdb}) { |
---|
[114] | 1657 | # to be improve in the future |
---|
| 1658 | next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself ! |
---|
[132] | 1659 | next LOOP_ON_IP_ADDRESS if ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}) eq 'unknow'; |
---|
| 1660 | next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{switch_port} eq '0'; |
---|
[114] | 1661 | |
---|
| 1662 | my $ip_timestamp = $computerdb->{$ip}{timestamp}; |
---|
| 1663 | my $ip_mac = $computerdb->{$ip}{mac_address}; |
---|
| 1664 | my $ip_hostname_fq = $computerdb->{$ip}{hostname_fq}; |
---|
| 1665 | |
---|
[115] | 1666 | my $swpt = sprintf "%-28s %2s", |
---|
| 1667 | $computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}, |
---|
[150] | 1668 | $computerdb->{$ip}{switch_port_hr}; |
---|
[211] | 1669 | $switchportdb{$swpt} ||= { |
---|
[114] | 1670 | ip => $ip, |
---|
| 1671 | timestamp => $ip_timestamp, |
---|
| 1672 | vlan => $computerdb->{$ip}{network}, |
---|
| 1673 | hostname_fq => $ip_hostname_fq, |
---|
| 1674 | mac_address => $ip_mac, |
---|
| 1675 | }; |
---|
[117] | 1676 | |
---|
[132] | 1677 | # if float computer, set date 15 day before warning... |
---|
| 1678 | my $ip_timestamp_mod = $ip_timestamp; |
---|
[211] | 1679 | my $ip_timestamp_ref = $switchportdb{$swpt}->{timestamp}; |
---|
[224] | 1680 | $ip_timestamp_mod -= $days_before_alert * 24 * 3600 if $ip_hostname_fq =~ m/$RE_FLOAT_HOSTNAME/; |
---|
| 1681 | $ip_timestamp_ref -= $days_before_alert * 24 * 3600 if $switchportdb{$swpt}->{hostname_fq} =~ m/$RE_FLOAT_HOSTNAME/; |
---|
[198] | 1682 | |
---|
[132] | 1683 | if ($ip_timestamp_mod > $ip_timestamp_ref) { |
---|
[211] | 1684 | $switchportdb{$swpt} = { |
---|
[114] | 1685 | ip => $ip, |
---|
| 1686 | timestamp => $ip_timestamp, |
---|
| 1687 | vlan => $computerdb->{$ip}{network}, |
---|
| 1688 | hostname_fq => $ip_hostname_fq, |
---|
| 1689 | mac_address => $ip_mac, |
---|
| 1690 | }; |
---|
| 1691 | } |
---|
| 1692 | } |
---|
| 1693 | |
---|
[133] | 1694 | LOOP_ON_RECENT_COMPUTER: |
---|
[211] | 1695 | for my $swpt (keys %switchportdb) { |
---|
[133] | 1696 | next LOOP_ON_RECENT_COMPUTER if $swpt =~ m/^\s*0$/; |
---|
[211] | 1697 | next LOOP_ON_RECENT_COMPUTER if $switchportdb{$swpt}->{hostname_fq} !~ m/$RE_FLOAT_HOSTNAME/; |
---|
[117] | 1698 | |
---|
[211] | 1699 | my $src_ip = $switchportdb{$swpt}->{ip}; |
---|
[114] | 1700 | my $src_timestamp = 0; |
---|
[133] | 1701 | LOOP_ON_IP_ADDRESS: |
---|
[165] | 1702 | for my $ip (keys %{$computerdb}) { |
---|
[211] | 1703 | next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{mac_address} ne $switchportdb{$swpt}->{mac_address}; |
---|
[133] | 1704 | next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} =~ m/$RE_FLOAT_HOSTNAME/; |
---|
| 1705 | next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{timestamp} < $src_timestamp; |
---|
[198] | 1706 | |
---|
[114] | 1707 | $src_ip = $ip; |
---|
| 1708 | $src_timestamp = $computerdb->{$ip}{timestamp}; |
---|
| 1709 | } |
---|
[132] | 1710 | |
---|
| 1711 | # keep only if float computer is the most recent |
---|
[133] | 1712 | next LOOP_ON_RECENT_COMPUTER if $src_timestamp == 0; |
---|
[211] | 1713 | next LOOP_ON_RECENT_COMPUTER if $switchportdb{$swpt}->{timestamp} < $src_timestamp; |
---|
[132] | 1714 | |
---|
[211] | 1715 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $switchportdb{$swpt}->{timestamp}; |
---|
[114] | 1716 | $year += 1900; |
---|
| 1717 | $mon++; |
---|
[132] | 1718 | my $date = sprintf '%04i-%02i-%02i/%02i:%02i', $year, $mon, $mday, $hour, $min; |
---|
[114] | 1719 | |
---|
| 1720 | ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$src_ip}{timestamp}; |
---|
| 1721 | $year += 1900; |
---|
| 1722 | $mon++; |
---|
[132] | 1723 | my $src_date = sprintf '%04i-%02i-%02i/%02i:%02i', $year, $mon, $mday, $hour, $min; |
---|
[114] | 1724 | |
---|
[176] | 1725 | my $vlan_id = get_current_vlan_id($computerdb->{$src_ip}{network}); |
---|
| 1726 | |
---|
| 1727 | printf "%s / %-10s +-> %-10s(%i) %s %s %s %s\n", |
---|
[211] | 1728 | $swpt, $switchportdb{$swpt}->{vlan}, $computerdb->{$src_ip}{network}, $vlan_id, |
---|
[114] | 1729 | $date, |
---|
| 1730 | $src_date, |
---|
[132] | 1731 | $computerdb->{$src_ip}{mac_address}, |
---|
[114] | 1732 | $computerdb->{$src_ip}{hostname_fq}; |
---|
| 1733 | } |
---|
| 1734 | } |
---|
| 1735 | |
---|
[218] | 1736 | #--------------------------------------------------------------- |
---|
[220] | 1737 | # not finish - do not use |
---|
[129] | 1738 | sub cmd_set_vlan_port { |
---|
| 1739 | my $switch_name = shift || q{}; |
---|
| 1740 | my $mac_address = shift || q{}; |
---|
| 1741 | |
---|
| 1742 | if ($switch_name eq q{} or $mac_address eq q{}) { |
---|
| 1743 | die "Usage: klask search-mac-on-switch SWITCH_NAME MAC_ADDRESS\n"; |
---|
| 1744 | } |
---|
| 1745 | |
---|
[196] | 1746 | $switch_name = join(',', map {$_->{hostname}} @SWITCH_LIST ) if $switch_name eq q{*}; |
---|
[129] | 1747 | |
---|
| 1748 | for my $sw_name (split /,/, $switch_name) { |
---|
| 1749 | if (not defined $SWITCH_DB{$sw_name}) { |
---|
| 1750 | die "Switch $sw_name must be defined in klask configuration file\n"; |
---|
| 1751 | } |
---|
| 1752 | |
---|
[221] | 1753 | my $research1 = $OID_NUMBER{'searchPort1'} . mac_address_hex_to_dec($mac_address); |
---|
| 1754 | my $research2 = $OID_NUMBER{'searchPort2'} .'.'. 0 . mac_address_hex_to_dec($mac_address); |
---|
[129] | 1755 | print "Klask search OID $research1 on switch $sw_name\n"; |
---|
| 1756 | print "Klask search OID $research2 on switch $sw_name\n"; |
---|
| 1757 | |
---|
[209] | 1758 | my $sw = $SWITCH_DB{$sw_name}; |
---|
[221] | 1759 | my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}} ); |
---|
[129] | 1760 | print "$error \n" if $error; |
---|
| 1761 | |
---|
| 1762 | my $result = $session->get_request( |
---|
| 1763 | -varbindlist => [$research1] |
---|
| 1764 | ); |
---|
| 1765 | if (not defined $result) { |
---|
| 1766 | $result = $session->get_request( |
---|
| 1767 | -varbindlist => [$research2] |
---|
| 1768 | ); |
---|
| 1769 | $result->{$research1} = $result->{$research2} if defined $result; |
---|
| 1770 | } |
---|
| 1771 | |
---|
| 1772 | if (defined $result and $result->{$research1} ne 'noSuchInstance') { |
---|
| 1773 | my $swport = $result->{$research1}; |
---|
| 1774 | print "Klask find MAC $mac_address on switch $sw_name port $swport\n"; |
---|
| 1775 | } |
---|
| 1776 | else { |
---|
| 1777 | print "Klask do not find MAC $mac_address on switch $sw_name\n"; |
---|
| 1778 | } |
---|
| 1779 | |
---|
| 1780 | $session->close; |
---|
| 1781 | } |
---|
| 1782 | return; |
---|
| 1783 | } |
---|
| 1784 | |
---|
[218] | 1785 | #--------------------------------------------------------------- |
---|
[129] | 1786 | sub cmd_get_vlan_port { |
---|
[141] | 1787 | @ARGV = @_; |
---|
| 1788 | |
---|
| 1789 | my $verbose; |
---|
| 1790 | GetOptions( |
---|
| 1791 | 'verbose|v' => \$verbose, |
---|
| 1792 | ); |
---|
| 1793 | |
---|
| 1794 | my $switch_name = shift @ARGV || q{}; |
---|
| 1795 | my $switch_port = shift @ARGV || q{}; |
---|
| 1796 | |
---|
| 1797 | if ($switch_name eq q{} or $switch_port eq q{}) { |
---|
| 1798 | die "Usage: klask get-vlan-port SWITCH_NAME PORT\n"; |
---|
| 1799 | } |
---|
| 1800 | |
---|
| 1801 | for my $sw_name (split /,/, $switch_name) { |
---|
| 1802 | if (not defined $SWITCH_DB{$sw_name}) { |
---|
| 1803 | die "Switch $sw_name must be defined in klask configuration file\n"; |
---|
| 1804 | } |
---|
| 1805 | |
---|
[142] | 1806 | my $search = $OID_NUMBER{'vlanPortDefault'} . ".$switch_port"; |
---|
[141] | 1807 | |
---|
[209] | 1808 | my $sw = $SWITCH_DB{$sw_name}; |
---|
[221] | 1809 | my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}} ); |
---|
[141] | 1810 | print "$error \n" if $error; |
---|
| 1811 | |
---|
| 1812 | my $result = $session->get_request( |
---|
| 1813 | -varbindlist => [$search], |
---|
| 1814 | ); |
---|
| 1815 | |
---|
| 1816 | if (defined $result and $result->{$search} ne 'noSuchInstance') { |
---|
| 1817 | my $vlan_id = $result->{$search} || 'empty'; |
---|
| 1818 | print "Klask VLAN Id $vlan_id on switch $sw_name on port $switch_port\n"; |
---|
| 1819 | } |
---|
| 1820 | else { |
---|
[142] | 1821 | print "Klask do not find VLAN Id on switch $sw_name on port $switch_port\n"; |
---|
[141] | 1822 | } |
---|
| 1823 | |
---|
| 1824 | $session->close; |
---|
| 1825 | } |
---|
| 1826 | return; |
---|
[129] | 1827 | } |
---|
| 1828 | |
---|
[218] | 1829 | #--------------------------------------------------------------- |
---|
[129] | 1830 | sub cmd_set_vlan_name { |
---|
| 1831 | } |
---|
| 1832 | |
---|
[218] | 1833 | #--------------------------------------------------------------- |
---|
[141] | 1834 | # snmpset -v 1 -c public sw1-batG0-legi.hmg.priv "$OID_NUMBER{'hpicfReset'}.0" i 2; |
---|
| 1835 | sub cmd_rebootsw { |
---|
| 1836 | @ARGV = @_; |
---|
| 1837 | |
---|
| 1838 | my $verbose; |
---|
| 1839 | GetOptions( |
---|
| 1840 | 'verbose|v' => \$verbose, |
---|
| 1841 | ); |
---|
| 1842 | |
---|
| 1843 | my $switch_name = shift @ARGV || q{}; |
---|
| 1844 | |
---|
[142] | 1845 | if ($switch_name eq q{}) { |
---|
[141] | 1846 | die "Usage: klask rebootsw SWITCH_NAME\n"; |
---|
| 1847 | } |
---|
| 1848 | |
---|
| 1849 | for my $sw_name (split /,/, $switch_name) { |
---|
| 1850 | if (not defined $SWITCH_DB{$sw_name}) { |
---|
| 1851 | die "Switch $sw_name must be defined in klask configuration file\n"; |
---|
| 1852 | } |
---|
| 1853 | |
---|
| 1854 | my $sw = $SWITCH_DB{$sw_name}; |
---|
[220] | 1855 | my ($session, $error) = Net::SNMP->session(snmp_get_rwsession($sw)); |
---|
[141] | 1856 | print "$error \n" if $error; |
---|
| 1857 | |
---|
| 1858 | my $result = $session->set_request( |
---|
| 1859 | -varbindlist => ["$OID_NUMBER{'hpicfReset'}.0", INTEGER, 2], |
---|
| 1860 | ); |
---|
| 1861 | |
---|
| 1862 | $session->close; |
---|
| 1863 | } |
---|
| 1864 | return; |
---|
| 1865 | } |
---|
| 1866 | |
---|
[218] | 1867 | #--------------------------------------------------------------- |
---|
[129] | 1868 | sub cmd_get_vlan_name { |
---|
| 1869 | my $switch_name = shift || q{}; |
---|
| 1870 | my $vlan_id = shift || q{}; |
---|
| 1871 | |
---|
| 1872 | if ($switch_name eq q{} or $vlan_id eq q{}) { |
---|
| 1873 | die "Usage: klask get-vlan-name SWITCH_NAME VLAN_ID\n"; |
---|
| 1874 | } |
---|
| 1875 | |
---|
[196] | 1876 | $switch_name = join(',', map {$_->{hostname}} @SWITCH_LIST ) if $switch_name eq q{*}; |
---|
[129] | 1877 | |
---|
| 1878 | for my $sw_name (split /,/, $switch_name) { |
---|
| 1879 | if (not defined $SWITCH_DB{$sw_name}) { |
---|
| 1880 | die "Switch $sw_name must be defined in klask configuration file\n"; |
---|
| 1881 | } |
---|
| 1882 | |
---|
[130] | 1883 | my $search_vlan_name = $OID_NUMBER{vlanName} . ".$vlan_id"; |
---|
[129] | 1884 | |
---|
[209] | 1885 | my $sw = $SWITCH_DB{$sw_name}; |
---|
[221] | 1886 | my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}} ); |
---|
[129] | 1887 | print "$error \n" if $error; |
---|
| 1888 | |
---|
| 1889 | my $result = $session->get_request( |
---|
| 1890 | -varbindlist => [$search_vlan_name] |
---|
| 1891 | ); |
---|
| 1892 | |
---|
| 1893 | if (defined $result and $result->{$search_vlan_name} ne 'noSuchInstance') { |
---|
[131] | 1894 | my $vlan_name = $result->{$search_vlan_name} || 'empty'; |
---|
[130] | 1895 | print "Klask find VLAN $vlan_id on switch $sw_name with name $vlan_name\n"; |
---|
[129] | 1896 | } |
---|
| 1897 | else { |
---|
| 1898 | print "Klask do not find VLAN $vlan_id on switch $sw_name\n"; |
---|
| 1899 | } |
---|
| 1900 | |
---|
| 1901 | $session->close; |
---|
| 1902 | } |
---|
| 1903 | return; |
---|
| 1904 | } |
---|
| 1905 | |
---|
[218] | 1906 | #--------------------------------------------------------------- |
---|
[111] | 1907 | sub cmd_ip_location { |
---|
[151] | 1908 | my $computerdb = computerdb_load(); |
---|
[2] | 1909 | |
---|
| 1910 | LOOP_ON_IP_ADDRESS: |
---|
[165] | 1911 | for my $ip (Net::Netmask::sort_by_ip_address(keys %{$computerdb})) { |
---|
[2] | 1912 | |
---|
| 1913 | next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself ! |
---|
| 1914 | |
---|
[63] | 1915 | my $sw_hostname = $computerdb->{$ip}{switch_hostname} || q{}; |
---|
[133] | 1916 | next LOOP_ON_IP_ADDRESS if $sw_hostname eq 'unknow'; |
---|
[63] | 1917 | |
---|
| 1918 | my $sw_location = q{}; |
---|
[133] | 1919 | LOOP_ON_ALL_SWITCH: |
---|
[196] | 1920 | for my $sw (@SWITCH_LIST) { |
---|
[133] | 1921 | next LOOP_ON_ALL_SWITCH if $sw_hostname ne $sw->{hostname}; |
---|
[2] | 1922 | $sw_location = $sw->{location}; |
---|
| 1923 | last; |
---|
| 1924 | } |
---|
| 1925 | |
---|
[63] | 1926 | printf "%s: \"%s\"\n", $ip, $sw_location if not $sw_location eq q{}; |
---|
[2] | 1927 | } |
---|
[63] | 1928 | return; |
---|
[2] | 1929 | } |
---|
| 1930 | |
---|
[218] | 1931 | #--------------------------------------------------------------- |
---|
[69] | 1932 | sub cmd_ip_free { |
---|
[224] | 1933 | @ARGV = @_; |
---|
[69] | 1934 | |
---|
[224] | 1935 | my $days_to_death = $DEFAULT{'days-to-death'} || 365 * 2; |
---|
[69] | 1936 | my $format = 'txt'; |
---|
[97] | 1937 | my $verbose; |
---|
[69] | 1938 | |
---|
[138] | 1939 | GetOptions( |
---|
[224] | 1940 | 'day|d=i' => \$days_to_death, |
---|
[69] | 1941 | 'format|f=s' => \$format, |
---|
[97] | 1942 | 'verbose|v' => \$verbose, |
---|
[69] | 1943 | ); |
---|
| 1944 | |
---|
[72] | 1945 | my %possible_format = ( |
---|
| 1946 | txt => \&cmd_ip_free_txt, |
---|
| 1947 | html => \&cmd_ip_free_html, |
---|
[97] | 1948 | none => sub {}, |
---|
[72] | 1949 | ); |
---|
| 1950 | $format = 'txt' if not defined $possible_format{$format}; |
---|
| 1951 | |
---|
[110] | 1952 | my @vlan_name = @ARGV; |
---|
[69] | 1953 | @vlan_name = get_list_network() if not @vlan_name; |
---|
| 1954 | |
---|
| 1955 | my $computerdb = {}; |
---|
[151] | 1956 | $computerdb = computerdb_load() if -e "$KLASK_DB_FILE"; |
---|
[69] | 1957 | my $timestamp = time; |
---|
| 1958 | |
---|
[224] | 1959 | my $timestamp_barrier = $timestamp - (3600 * 24 * $days_to_death); |
---|
[69] | 1960 | |
---|
[72] | 1961 | my %result_ip = (); |
---|
[69] | 1962 | |
---|
| 1963 | ALL_NETWORK: |
---|
| 1964 | for my $vlan (@vlan_name) { |
---|
| 1965 | |
---|
| 1966 | my @ip_list = get_list_ip($vlan); |
---|
[97] | 1967 | |
---|
[69] | 1968 | LOOP_ON_IP_ADDRESS: |
---|
| 1969 | for my $ip (@ip_list) { |
---|
| 1970 | |
---|
[135] | 1971 | if (exists $computerdb->{$ip}) { |
---|
| 1972 | next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{timestamp} > $timestamp_barrier; |
---|
[198] | 1973 | |
---|
[135] | 1974 | my $mac_address = $computerdb->{$ip}{mac_address}; |
---|
| 1975 | LOOP_ON_DATABASE: |
---|
[165] | 1976 | for my $ip_db (keys %{$computerdb}) { |
---|
[135] | 1977 | next LOOP_ON_DATABASE if $computerdb->{$ip_db}{mac_address} ne $mac_address; |
---|
| 1978 | next LOOP_ON_IP_ADDRESS if $computerdb->{$ip_db}{timestamp} > $timestamp_barrier; |
---|
| 1979 | } |
---|
| 1980 | } |
---|
[69] | 1981 | |
---|
| 1982 | my $ip_date_last_detection = ''; |
---|
| 1983 | if (exists $computerdb->{$ip}) { |
---|
| 1984 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{timestamp}; |
---|
| 1985 | $year += 1900; |
---|
| 1986 | $mon++; |
---|
| 1987 | $ip_date_last_detection = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; |
---|
| 1988 | } |
---|
| 1989 | |
---|
| 1990 | my $packed_ip = scalar gethostbyname($ip); |
---|
| 1991 | my $hostname_fq = 'unknown'; |
---|
[186] | 1992 | $hostname_fq = scalar gethostbyaddr($packed_ip, AF_INET) || 'unknown' if defined $packed_ip and get_current_scan_mode($vlan) eq 'active'; |
---|
[135] | 1993 | |
---|
| 1994 | next LOOP_ON_IP_ADDRESS if $hostname_fq =~ m/$RE_FLOAT_HOSTNAME/; |
---|
| 1995 | |
---|
| 1996 | $result_ip{$ip} ||= {}; |
---|
| 1997 | $result_ip{$ip}->{date_last_detection} = $ip_date_last_detection; |
---|
[69] | 1998 | $result_ip{$ip}->{hostname_fq} = $hostname_fq; |
---|
| 1999 | $result_ip{$ip}->{vlan} = $vlan; |
---|
[97] | 2000 | |
---|
| 2001 | printf "VERBOSE_1: %-15s %-12s %s\n", $ip, $vlan, $hostname_fq if $verbose; |
---|
[69] | 2002 | } |
---|
| 2003 | } |
---|
| 2004 | |
---|
[72] | 2005 | $possible_format{$format}->(%result_ip); |
---|
| 2006 | } |
---|
| 2007 | |
---|
[218] | 2008 | #--------------------------------------------------------------- |
---|
[72] | 2009 | sub cmd_ip_free_txt { |
---|
| 2010 | my %result_ip = @_; |
---|
[198] | 2011 | |
---|
[69] | 2012 | printf "%-15s %-40s %-16s %s\n", qw(IPv4-Address Hostname-FQ Date VLAN); |
---|
| 2013 | print "-------------------------------------------------------------------------------\n"; |
---|
[72] | 2014 | LOOP_ON_IP_ADDRESS: |
---|
[165] | 2015 | for my $ip (Net::Netmask::sort_by_ip_address(keys %result_ip)) { |
---|
[180] | 2016 | my $vlan_nameid = $result_ip{$ip}->{vlan}.'('.get_current_vlan_id($result_ip{$ip}->{vlan}).')'; |
---|
| 2017 | printf "%-15s %-40s %-16s %s\n", $ip, $result_ip{$ip}->{hostname_fq}, $result_ip{$ip}->{date_last_detection}, $vlan_nameid; |
---|
[69] | 2018 | } |
---|
| 2019 | } |
---|
| 2020 | |
---|
[218] | 2021 | #--------------------------------------------------------------- |
---|
[72] | 2022 | sub cmd_ip_free_html { |
---|
| 2023 | my %result_ip = @_; |
---|
| 2024 | |
---|
| 2025 | print <<'END_HTML'; |
---|
[73] | 2026 | <table class="sortable" summary="Klask Free IP Database"> |
---|
| 2027 | <caption>Klask Free IP Database</caption> |
---|
[72] | 2028 | <thead> |
---|
| 2029 | <tr> |
---|
[73] | 2030 | <th scope="col" class="klask-header-left">IPv4-Address</th> |
---|
[72] | 2031 | <th scope="col" class="sorttable_alpha">Hostname-FQ</th> |
---|
| 2032 | <th scope="col" class="sorttable_alpha">VLAN</th> |
---|
[73] | 2033 | <th scope="col" class="klask-header-right">Date</th> |
---|
[72] | 2034 | </tr> |
---|
| 2035 | </thead> |
---|
| 2036 | <tfoot> |
---|
| 2037 | <tr> |
---|
[73] | 2038 | <th scope="col" class="klask-footer-left">IPv4-Address</th> |
---|
| 2039 | <th scope="col" class="fklask-hostname">Hostname-FQ</th> |
---|
[72] | 2040 | <th scope="col" class="fklask-vlan">VLAN</th> |
---|
[73] | 2041 | <th scope="col" class="klask-footer-right">Date</th> |
---|
[72] | 2042 | </tr> |
---|
| 2043 | </tfoot> |
---|
| 2044 | <tbody> |
---|
| 2045 | END_HTML |
---|
| 2046 | |
---|
| 2047 | my $typerow = 'even'; |
---|
| 2048 | |
---|
| 2049 | LOOP_ON_IP_ADDRESS: |
---|
[165] | 2050 | for my $ip (Net::Netmask::sort_by_ip_address(keys %result_ip)) { |
---|
[72] | 2051 | |
---|
| 2052 | $typerow = $typerow eq 'even' ? 'odd' : 'even'; |
---|
| 2053 | |
---|
| 2054 | my $ip_sort = sprintf '%03i%03i%03i%03i', split m/ \. /xms, $ip; |
---|
| 2055 | my ( $host_short ) = split m/ \. /xms, $result_ip{$ip}->{hostname_fq}; |
---|
| 2056 | |
---|
[180] | 2057 | my $vlan_nameid = $result_ip{$ip}->{vlan}.'('.get_current_vlan_id($result_ip{$ip}->{vlan}).')'; |
---|
| 2058 | |
---|
[72] | 2059 | print <<"END_HTML"; |
---|
| 2060 | <tr class="$typerow"> |
---|
| 2061 | <td sorttable_customkey="$ip_sort">$ip</td> |
---|
| 2062 | <td sorttable_customkey="$host_short">$result_ip{$ip}->{hostname_fq}</td> |
---|
[180] | 2063 | <td>$vlan_nameid</td> |
---|
[72] | 2064 | <td>$result_ip{$ip}->{date_last_detection}</td> |
---|
| 2065 | </tr> |
---|
| 2066 | END_HTML |
---|
| 2067 | } |
---|
| 2068 | print <<'END_HTML'; |
---|
| 2069 | </tbody> |
---|
| 2070 | </table> |
---|
| 2071 | END_HTML |
---|
| 2072 | } |
---|
| 2073 | |
---|
[218] | 2074 | #--------------------------------------------------------------- |
---|
[2] | 2075 | sub cmd_enable { |
---|
[210] | 2076 | @ARGV = @_; |
---|
[63] | 2077 | |
---|
[210] | 2078 | my $verbose; |
---|
| 2079 | |
---|
| 2080 | GetOptions( |
---|
| 2081 | 'verbose|v' => \$verbose, |
---|
| 2082 | ); |
---|
| 2083 | |
---|
| 2084 | my $switch_name = shift @ARGV || q{}; |
---|
| 2085 | my $port = shift @ARGV || q{}; |
---|
| 2086 | |
---|
| 2087 | if ($switch_name eq q{} or $port eq q{}) { |
---|
| 2088 | die "Usage: klask disable SWITCH_NAME PORT\n"; |
---|
| 2089 | } |
---|
| 2090 | |
---|
| 2091 | if (not defined $SWITCH_DB{$switch_name}) { |
---|
| 2092 | die "Switch $switch_name must be defined in klask configuration file\n"; |
---|
| 2093 | } |
---|
| 2094 | |
---|
| 2095 | my $sw = $SWITCH_DB{$switch_name}; |
---|
[220] | 2096 | my ($session, $error) = Net::SNMP->session(snmp_get_rwsession($sw)); |
---|
[210] | 2097 | print "$error \n" if $error; |
---|
| 2098 | |
---|
[216] | 2099 | # Retrieve numeric port value |
---|
| 2100 | my $port_num = snmp_get_switchport_num($session, normalize_port_human_readable($port), $verbose ? 'yes' : ''); |
---|
| 2101 | die "Error : Port $port does not exist on switch $switch_name\n" if not $port_num =~ m/^\d+$/; |
---|
| 2102 | |
---|
| 2103 | my $search_portstatus = $OID_NUMBER{'portUpDown'} .'.'. $port_num; |
---|
| 2104 | print "Info: switch $switch_name port $port SNMP OID $search_portstatus\n" if $verbose; |
---|
| 2105 | |
---|
[210] | 2106 | my $result = $session->set_request( |
---|
| 2107 | -varbindlist => [$search_portstatus, INTEGER, 1], |
---|
| 2108 | ); |
---|
[216] | 2109 | print $session->error()."\n" if $session->error_status(); |
---|
[210] | 2110 | |
---|
| 2111 | $session->close; |
---|
| 2112 | |
---|
[63] | 2113 | #snmpset -v 1 -c community X.X.X.X 1.3.6.1.2.1.2.2.1.7.NoPort = 1 (up) |
---|
| 2114 | #snmpset -v 1 -c community X.X.X.X 1.3.6.1.2.1.2.2.1.7.NoPort = 2 (down) |
---|
[210] | 2115 | #system "snmpset -v 1 -c public $switch 1.3.6.1.2.1.2.2.1.7.$port = 1"; |
---|
[63] | 2116 | return; |
---|
[2] | 2117 | } |
---|
| 2118 | |
---|
[218] | 2119 | #--------------------------------------------------------------- |
---|
[2] | 2120 | sub cmd_disable { |
---|
[210] | 2121 | @ARGV = @_; |
---|
[63] | 2122 | |
---|
[210] | 2123 | my $verbose; |
---|
| 2124 | |
---|
| 2125 | GetOptions( |
---|
| 2126 | 'verbose|v' => \$verbose, |
---|
| 2127 | ); |
---|
| 2128 | |
---|
| 2129 | my $switch_name = shift @ARGV || q{}; |
---|
| 2130 | my $port = shift @ARGV || q{}; |
---|
| 2131 | |
---|
| 2132 | if ($switch_name eq q{} or $port eq q{}) { |
---|
| 2133 | die "Usage: klask disable SWITCH_NAME PORT\n"; |
---|
| 2134 | } |
---|
| 2135 | |
---|
| 2136 | if (not defined $SWITCH_DB{$switch_name}) { |
---|
| 2137 | die "Switch $switch_name must be defined in klask configuration file\n"; |
---|
| 2138 | } |
---|
| 2139 | |
---|
| 2140 | my $sw = $SWITCH_DB{$switch_name}; |
---|
[220] | 2141 | my ($session, $error) = Net::SNMP->session(snmp_get_rwsession($sw)); |
---|
[210] | 2142 | print "$error \n" if $error; |
---|
| 2143 | |
---|
[216] | 2144 | # Retrieve numeric port value |
---|
| 2145 | my $port_num = snmp_get_switchport_num($session, normalize_port_human_readable($port), $verbose ? 'yes' : ''); |
---|
| 2146 | die "Error : Port $port does not exist on switch $switch_name\n" if not $port_num =~ m/^\d+$/; |
---|
| 2147 | |
---|
| 2148 | my $search_portstatus = $OID_NUMBER{'portUpDown'} .'.'. $port_num; |
---|
| 2149 | print "Info: switch $switch_name port $port SNMP OID $search_portstatus\n" if $verbose; |
---|
| 2150 | |
---|
[210] | 2151 | my $result = $session->set_request( |
---|
| 2152 | -varbindlist => [$search_portstatus, INTEGER, 2], |
---|
| 2153 | ); |
---|
[213] | 2154 | print $session->error()."\n" if $session->error_status(); |
---|
[210] | 2155 | |
---|
| 2156 | $session->close; |
---|
| 2157 | |
---|
| 2158 | #system "snmpset -v 1 -c public $switch 1.3.6.1.2.1.2.2.1.7.$port = 2"; |
---|
[63] | 2159 | return; |
---|
[2] | 2160 | } |
---|
| 2161 | |
---|
[218] | 2162 | #--------------------------------------------------------------- |
---|
[2] | 2163 | sub cmd_status { |
---|
[206] | 2164 | @ARGV = @_; |
---|
[63] | 2165 | |
---|
[206] | 2166 | my $verbose; |
---|
| 2167 | |
---|
| 2168 | GetOptions( |
---|
| 2169 | 'verbose|v' => \$verbose, |
---|
| 2170 | ); |
---|
| 2171 | |
---|
| 2172 | my $switch_name = shift @ARGV || q{}; |
---|
| 2173 | my $port = shift @ARGV || q{}; |
---|
| 2174 | |
---|
[207] | 2175 | if ($switch_name eq q{} or $port eq q{}) { |
---|
| 2176 | die "Usage: klask status SWITCH_NAME PORT\n"; |
---|
| 2177 | } |
---|
| 2178 | |
---|
| 2179 | if (not defined $SWITCH_DB{$switch_name}) { |
---|
| 2180 | die "Switch $switch_name must be defined in klask configuration file\n"; |
---|
| 2181 | } |
---|
| 2182 | |
---|
[206] | 2183 | my $sw = $SWITCH_DB{$switch_name}; |
---|
[221] | 2184 | my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}} ); |
---|
[206] | 2185 | print "$error \n" if $error; |
---|
| 2186 | |
---|
[213] | 2187 | # Retrieve numeric port value |
---|
[214] | 2188 | my $port_num = snmp_get_switchport_num($session, normalize_port_human_readable($port), $verbose ? 'yes' : ''); |
---|
[216] | 2189 | die "Error : Port $port does not exist on switch $switch_name\n" if not $port_num =~ m/^\d+$/; |
---|
[213] | 2190 | |
---|
| 2191 | my $search_portstatus = $OID_NUMBER{'portUpDown'} .'.'. $port_num; |
---|
| 2192 | print "Info: switch $switch_name port $port ($port_num) SNMP OID $search_portstatus\n" if $verbose; |
---|
| 2193 | |
---|
[206] | 2194 | my $result = $session->get_request( |
---|
[209] | 2195 | -varbindlist => [$search_portstatus] |
---|
[206] | 2196 | ); |
---|
[216] | 2197 | print $session->error()."\n" if $session->error_status(); |
---|
[206] | 2198 | if (defined $result) { |
---|
[209] | 2199 | print "$PORT_UPDOWN{$result->{$search_portstatus}}\n"; |
---|
[206] | 2200 | } |
---|
| 2201 | |
---|
[216] | 2202 | $session->close; |
---|
| 2203 | |
---|
[206] | 2204 | #system "snmpget -v 1 -c public $switch_name 1.3.6.1.2.1.2.2.1.7.$port"; |
---|
[63] | 2205 | return; |
---|
[2] | 2206 | } |
---|
| 2207 | |
---|
[218] | 2208 | #--------------------------------------------------------------- |
---|
[35] | 2209 | sub cmd_search_mac_on_switch { |
---|
[138] | 2210 | @ARGV = @_; |
---|
[63] | 2211 | |
---|
[138] | 2212 | my $verbose; |
---|
[144] | 2213 | my $vlan_id = 0; |
---|
[138] | 2214 | |
---|
| 2215 | GetOptions( |
---|
| 2216 | 'verbose|v' => \$verbose, |
---|
[144] | 2217 | 'vlan|l=i' => \$vlan_id, |
---|
[138] | 2218 | ); |
---|
| 2219 | |
---|
| 2220 | my $switch_name = shift @ARGV || q{}; |
---|
| 2221 | my $mac_address = shift @ARGV || q{}; |
---|
| 2222 | |
---|
[63] | 2223 | if ($switch_name eq q{} or $mac_address eq q{}) { |
---|
[39] | 2224 | die "Usage: klask search-mac-on-switch SWITCH_NAME MAC_ADDRESS\n"; |
---|
[38] | 2225 | } |
---|
[39] | 2226 | |
---|
[138] | 2227 | $mac_address = normalize_mac_address($mac_address); |
---|
[228] | 2228 | $switch_name = join(',', map {$_->{hostname}} @SWITCH_LIST ) if $switch_name eq q{*} or $switch_name eq q{all}; |
---|
[39] | 2229 | |
---|
[112] | 2230 | for my $sw_name (split /,/, $switch_name) { |
---|
| 2231 | if (not defined $SWITCH_DB{$sw_name}) { |
---|
| 2232 | die "Switch $sw_name must be defined in klask configuration file\n"; |
---|
| 2233 | } |
---|
[39] | 2234 | |
---|
[221] | 2235 | my $research1 = $OID_NUMBER{'searchPort1'} . mac_address_hex_to_dec($mac_address); |
---|
| 2236 | my $research2 = $OID_NUMBER{'searchPort2'} .'.'. $vlan_id . mac_address_hex_to_dec($mac_address); |
---|
[138] | 2237 | print "Klask search OID $research1 on switch $sw_name\n" if $verbose; |
---|
| 2238 | print "Klask search OID $research2 on switch $sw_name\n" if $verbose; |
---|
[35] | 2239 | |
---|
[209] | 2240 | my $sw = $SWITCH_DB{$sw_name}; |
---|
[221] | 2241 | my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}} ); |
---|
[112] | 2242 | print "$error \n" if $error; |
---|
[63] | 2243 | |
---|
[112] | 2244 | my $result = $session->get_request( |
---|
[124] | 2245 | -varbindlist => [$research1] |
---|
[112] | 2246 | ); |
---|
[124] | 2247 | if (not defined $result) { |
---|
| 2248 | $result = $session->get_request( |
---|
| 2249 | -varbindlist => [$research2] |
---|
| 2250 | ); |
---|
| 2251 | $result->{$research1} = $result->{$research2} if defined $result; |
---|
| 2252 | } |
---|
[112] | 2253 | |
---|
[124] | 2254 | if (defined $result and $result->{$research1} ne 'noSuchInstance') { |
---|
[181] | 2255 | my $swport_num = $result->{$research1}; |
---|
[211] | 2256 | my $swport_hr = get_human_readable_port($sw->{model}, snmp_get_switchport_hr($session, $swport_num)); |
---|
[181] | 2257 | print "Klask find MAC $mac_address on switch $sw_name port $swport_hr\n"; |
---|
[123] | 2258 | } |
---|
| 2259 | else { |
---|
[138] | 2260 | print "Klask do not find MAC $mac_address on switch $sw_name\n" if $verbose; |
---|
[112] | 2261 | } |
---|
| 2262 | |
---|
[35] | 2263 | $session->close; |
---|
| 2264 | } |
---|
[63] | 2265 | return; |
---|
[35] | 2266 | } |
---|
| 2267 | |
---|
[218] | 2268 | #--------------------------------------------------------------- |
---|
[4] | 2269 | sub cmd_updatesw { |
---|
[113] | 2270 | @ARGV = @_; |
---|
[2] | 2271 | |
---|
[79] | 2272 | my $verbose; |
---|
| 2273 | |
---|
[138] | 2274 | GetOptions( |
---|
[79] | 2275 | 'verbose|v' => \$verbose, |
---|
| 2276 | ); |
---|
| 2277 | |
---|
[4] | 2278 | init_switch_names('yes'); #nomme les switchs |
---|
[2] | 2279 | print "\n"; |
---|
| 2280 | |
---|
| 2281 | my %where = (); |
---|
| 2282 | my %db_switch_output_port = (); |
---|
[83] | 2283 | my %db_switch_ip_hostnamefq = (); |
---|
[2] | 2284 | |
---|
| 2285 | DETECT_ALL_ROUTER: |
---|
[11] | 2286 | for my $one_router ( get_list_main_router(get_list_network()) ) { |
---|
[196] | 2287 | print "Info: router loop $one_router\n" if $verbose; |
---|
[63] | 2288 | my %resol_arp = resolve_ip_arp_host($one_router, q{*}, q{low}); # resolution arp |
---|
[83] | 2289 | |
---|
[2] | 2290 | next DETECT_ALL_ROUTER if $resol_arp{mac_address} eq 'unknow'; |
---|
[83] | 2291 | print "VERBOSE_1: Router detected $resol_arp{ipv4_address} - $resol_arp{mac_address}\n" if $verbose; |
---|
| 2292 | |
---|
[144] | 2293 | my $vlan_name = get_current_vlan_name_for_interface($resol_arp{interface}); |
---|
| 2294 | my $vlan_id = get_current_vlan_id($vlan_name); |
---|
| 2295 | $where{$resol_arp{ipv4_address}} = find_all_switch_port($resol_arp{mac_address},$vlan_id); # retrouve les emplacements des routeurs |
---|
[2] | 2296 | } |
---|
| 2297 | |
---|
| 2298 | ALL_ROUTER_IP_ADDRESS: |
---|
[83] | 2299 | for my $ip_router (Net::Netmask::sort_by_ip_address(keys %where)) { # '194.254.66.254')) { |
---|
[63] | 2300 | |
---|
[83] | 2301 | next ALL_ROUTER_IP_ADDRESS if not exists $where{$ip_router}; # /a priori/ idiot car ne sers à rien... |
---|
[2] | 2302 | |
---|
| 2303 | ALL_SWITCH_CONNECTED: |
---|
[83] | 2304 | for my $switch_detected ( keys %{$where{$ip_router}} ) { |
---|
[2] | 2305 | |
---|
[83] | 2306 | my $switch = $where{$ip_router}->{$switch_detected}; |
---|
[2] | 2307 | |
---|
| 2308 | next ALL_SWITCH_CONNECTED if $switch->{port} eq '0'; |
---|
[63] | 2309 | |
---|
[154] | 2310 | $db_switch_output_port{$switch->{hostname}} = $switch->{port_hr}; |
---|
| 2311 | print "VERBOSE_2: output port $switch->{hostname} : $switch->{port_hr}\n" if $verbose; |
---|
[2] | 2312 | } |
---|
[63] | 2313 | } |
---|
[2] | 2314 | |
---|
| 2315 | my %db_switch_link_with = (); |
---|
| 2316 | |
---|
[83] | 2317 | my @list_all_switch = (); |
---|
[2] | 2318 | my @list_switch_ipv4 = (); |
---|
[209] | 2319 | for my $sw (@SWITCH_LIST) { |
---|
[83] | 2320 | push @list_all_switch, $sw->{hostname}; |
---|
[2] | 2321 | } |
---|
| 2322 | |
---|
[45] | 2323 | my $timestamp = time; |
---|
| 2324 | |
---|
[2] | 2325 | ALL_SWITCH: |
---|
[198] | 2326 | for my $one_switch (@list_all_switch) { |
---|
| 2327 | my %resol_arp = resolve_ip_arp_host($one_switch, q{*}, q{low}); # arp resolution |
---|
[199] | 2328 | if (exists $SWITCH_DB{$one_switch}{'fake-ip'}) { |
---|
| 2329 | print "WARNING: fake ip on switch $one_switch -> $SWITCH_DB{$one_switch}{'fake-ip'} / $resol_arp{ipv4_address}\n" if $verbose; |
---|
| 2330 | my %resol_arp_alt = resolve_ip_arp_host($SWITCH_DB{$one_switch}{'fake-ip'}, q{*}, q{low}); # arp resolution |
---|
| 2331 | if ($resol_arp_alt{mac_address} ne 'unknow') { |
---|
| 2332 | $resol_arp{mac_address} = $resol_arp_alt{mac_address}; |
---|
| 2333 | $resol_arp{interface} = $resol_arp_alt{interface}; |
---|
| 2334 | $resol_arp{ipv4_address} .= '*'; |
---|
| 2335 | } |
---|
| 2336 | } |
---|
[198] | 2337 | print "Info: switch loop $one_switch\n" if $verbose; |
---|
[2] | 2338 | next ALL_SWITCH if $resol_arp{mac_address} eq 'unknow'; |
---|
[63] | 2339 | |
---|
[82] | 2340 | push @list_switch_ipv4, $resol_arp{ipv4_address}; |
---|
[63] | 2341 | |
---|
[144] | 2342 | my $vlan_name = get_current_vlan_name_for_interface($resol_arp{interface}); |
---|
| 2343 | my $vlan_id = get_current_vlan_id($vlan_name); |
---|
| 2344 | $where{$resol_arp{ipv4_address}} = find_all_switch_port($resol_arp{mac_address},$vlan_id); # find port on all switch |
---|
[2] | 2345 | |
---|
[80] | 2346 | if ($verbose) { |
---|
[198] | 2347 | print "VERBOSE_3: $one_switch $resol_arp{ipv4_address} $resol_arp{mac_address}\n"; |
---|
| 2348 | print "VERBOSE_3: $one_switch --- ", |
---|
[82] | 2349 | join(' + ', keys %{$where{$resol_arp{ipv4_address}}}), |
---|
| 2350 | "\n"; |
---|
[80] | 2351 | } |
---|
| 2352 | |
---|
[83] | 2353 | $db_switch_ip_hostnamefq{$resol_arp{ipv4_address}} = $resol_arp{hostname_fq}; |
---|
| 2354 | print "VERBOSE_4: db_switch_ip_hostnamefq $resol_arp{ipv4_address} -> $resol_arp{hostname_fq}\n" if $verbose; |
---|
[45] | 2355 | |
---|
[198] | 2356 | $SWITCH_DB{$one_switch}->{ipv4_address} = $resol_arp{ipv4_address}; |
---|
| 2357 | $SWITCH_DB{$one_switch}->{mac_address} = $resol_arp{mac_address}; |
---|
| 2358 | $SWITCH_DB{$one_switch}->{timestamp} = $timestamp; |
---|
[2] | 2359 | } |
---|
[63] | 2360 | |
---|
[2] | 2361 | ALL_SWITCH_IP_ADDRESS: |
---|
[199] | 2362 | for my $ip (@list_switch_ipv4) { |
---|
| 2363 | # for my $ip (Net::Netmask::sort_by_ip_address(@list_switch_ipv4)) { |
---|
[63] | 2364 | |
---|
[83] | 2365 | print "VERBOSE_5: loop on $db_switch_ip_hostnamefq{$ip}\n" if $verbose; |
---|
| 2366 | |
---|
[2] | 2367 | next ALL_SWITCH_IP_ADDRESS if not exists $where{$ip}; |
---|
[84] | 2368 | # next ALL_SWITCH_IP_ADDRESS if not exists $SWITCH_PORT_COUNT{ $db_switch_ip_hostnamefq{$ip} }; |
---|
[2] | 2369 | |
---|
| 2370 | DETECTED_SWITCH: |
---|
| 2371 | for my $switch_detected ( keys %{$where{$ip}} ) { |
---|
| 2372 | |
---|
| 2373 | my $switch = $where{$ip}->{$switch_detected}; |
---|
[154] | 2374 | print "VERBOSE_6: $db_switch_ip_hostnamefq{$ip} -> $switch->{hostname} : $switch->{port_hr}\n" if $verbose; |
---|
[2] | 2375 | |
---|
| 2376 | next if $switch->{port} eq '0'; |
---|
[154] | 2377 | next if $switch->{port_hr} eq $db_switch_output_port{$switch->{hostname}}; |
---|
[83] | 2378 | next if $switch->{hostname} eq $db_switch_ip_hostnamefq{$ip}; # $computerdb->{$ip}{hostname}; |
---|
[2] | 2379 | |
---|
[83] | 2380 | $db_switch_link_with{ $db_switch_ip_hostnamefq{$ip} } ||= {}; |
---|
[160] | 2381 | $db_switch_link_with{ $db_switch_ip_hostnamefq{$ip} }->{ $switch->{hostname} } = $switch->{port_hr}; |
---|
[83] | 2382 | print "VERBOSE_7: +++++\n" if $verbose; |
---|
[2] | 2383 | } |
---|
| 2384 | |
---|
| 2385 | } |
---|
[63] | 2386 | |
---|
[2] | 2387 | my %db_switch_connected_on_port = (); |
---|
| 2388 | my $maybe_more_than_one_switch_connected = 'yes'; |
---|
[181] | 2389 | my $cloop = 0; |
---|
[63] | 2390 | |
---|
[181] | 2391 | while ($maybe_more_than_one_switch_connected eq 'yes' and $cloop < 100) { |
---|
| 2392 | $cloop++; |
---|
| 2393 | print "VERBOSE_9: cloop reduction step: $cloop\n" if $verbose; |
---|
[2] | 2394 | for my $sw (keys %db_switch_link_with) { |
---|
| 2395 | for my $connect (keys %{$db_switch_link_with{$sw}}) { |
---|
[63] | 2396 | |
---|
[160] | 2397 | my $port_hr = $db_switch_link_with{$sw}->{$connect}; |
---|
[63] | 2398 | |
---|
[160] | 2399 | $db_switch_connected_on_port{"$connect:$port_hr"} ||= {}; |
---|
| 2400 | $db_switch_connected_on_port{"$connect:$port_hr"}->{$sw}++; # Just to define the key |
---|
[2] | 2401 | } |
---|
| 2402 | } |
---|
| 2403 | |
---|
| 2404 | $maybe_more_than_one_switch_connected = 'no'; |
---|
| 2405 | |
---|
| 2406 | SWITCH_AND_PORT: |
---|
| 2407 | for my $swport (keys %db_switch_connected_on_port) { |
---|
[63] | 2408 | |
---|
[2] | 2409 | next if keys %{$db_switch_connected_on_port{$swport}} == 1; |
---|
[63] | 2410 | |
---|
[2] | 2411 | $maybe_more_than_one_switch_connected = 'yes'; |
---|
| 2412 | |
---|
[63] | 2413 | my ($sw_connect,$port_connect) = split m/ : /xms, $swport; |
---|
[2] | 2414 | my @sw_on_same_port = keys %{$db_switch_connected_on_port{$swport}}; |
---|
[181] | 2415 | print "VERBOSE_10: $swport -- ".$#sw_on_same_port." -- @sw_on_same_port\n" if $verbose; |
---|
[2] | 2416 | |
---|
| 2417 | CONNECTED: |
---|
| 2418 | for my $sw_connected (@sw_on_same_port) { |
---|
[63] | 2419 | |
---|
[2] | 2420 | next CONNECTED if not keys %{$db_switch_link_with{$sw_connected}} == 1; |
---|
[63] | 2421 | |
---|
[2] | 2422 | $db_switch_connected_on_port{$swport} = {$sw_connected => 1}; |
---|
[63] | 2423 | |
---|
[2] | 2424 | for my $other_sw (@sw_on_same_port) { |
---|
| 2425 | next if $other_sw eq $sw_connected; |
---|
[63] | 2426 | |
---|
[2] | 2427 | delete $db_switch_link_with{$other_sw}->{$sw_connect}; |
---|
| 2428 | } |
---|
[63] | 2429 | |
---|
[2] | 2430 | # We can not do better for this switch for this loop |
---|
| 2431 | next SWITCH_AND_PORT; |
---|
| 2432 | } |
---|
| 2433 | } |
---|
| 2434 | } |
---|
| 2435 | |
---|
| 2436 | my %db_switch_parent =(); |
---|
| 2437 | |
---|
| 2438 | for my $sw (keys %db_switch_link_with) { |
---|
| 2439 | for my $connect (keys %{$db_switch_link_with{$sw}}) { |
---|
[63] | 2440 | |
---|
[160] | 2441 | my $port_hr = $db_switch_link_with{$sw}->{$connect}; |
---|
[63] | 2442 | |
---|
[160] | 2443 | $db_switch_connected_on_port{"$connect:$port_hr"} ||= {}; |
---|
| 2444 | $db_switch_connected_on_port{"$connect:$port_hr"}->{$sw} = $port_hr; |
---|
[63] | 2445 | |
---|
[161] | 2446 | $db_switch_parent{$sw} = {switch => $connect, port_hr => $port_hr}; |
---|
[2] | 2447 | } |
---|
| 2448 | } |
---|
| 2449 | |
---|
[63] | 2450 | print "Switch output port and parent port connection\n"; |
---|
[2] | 2451 | print "---------------------------------------------\n"; |
---|
| 2452 | for my $sw (sort keys %db_switch_output_port) { |
---|
| 2453 | if (exists $db_switch_parent{$sw}) { |
---|
[221] | 2454 | printf "%-28s %2s +--> %2s %-25s\n", $sw, $db_switch_output_port{$sw}, $db_switch_parent{$sw}->{port_hr}, $db_switch_parent{$sw}->{'switch'}; |
---|
[2] | 2455 | } |
---|
| 2456 | else { |
---|
[82] | 2457 | printf "%-28s %2s +--> router\n", $sw, $db_switch_output_port{$sw}; |
---|
[2] | 2458 | } |
---|
| 2459 | } |
---|
| 2460 | print "\n"; |
---|
| 2461 | |
---|
| 2462 | print "Switch parent and children port inter-connection\n"; |
---|
| 2463 | print "------------------------------------------------\n"; |
---|
[63] | 2464 | for my $swport (sort keys %db_switch_connected_on_port) { |
---|
| 2465 | my ($sw_connect,$port_connect) = split m/ : /xms, $swport; |
---|
[2] | 2466 | for my $sw (keys %{$db_switch_connected_on_port{$swport}}) { |
---|
| 2467 | if (exists $db_switch_output_port{$sw}) { |
---|
[82] | 2468 | printf "%-28s %2s <--+ %2s %-25s\n", $sw_connect, $port_connect, $db_switch_output_port{$sw}, $sw; |
---|
[2] | 2469 | } |
---|
| 2470 | else { |
---|
[82] | 2471 | printf "%-28s %2s <--+ %-25s\n", $sw_connect, $port_connect, $sw; |
---|
[2] | 2472 | } |
---|
| 2473 | } |
---|
| 2474 | } |
---|
| 2475 | |
---|
| 2476 | my $switch_connection = { |
---|
| 2477 | output_port => \%db_switch_output_port, |
---|
| 2478 | parent => \%db_switch_parent, |
---|
| 2479 | connected_on_port => \%db_switch_connected_on_port, |
---|
| 2480 | link_with => \%db_switch_link_with, |
---|
[25] | 2481 | switch_db => \%SWITCH_DB, |
---|
[2] | 2482 | }; |
---|
[63] | 2483 | |
---|
[44] | 2484 | YAML::Syck::DumpFile("$KLASK_SW_FILE", $switch_connection); |
---|
[63] | 2485 | return; |
---|
[2] | 2486 | } |
---|
| 2487 | |
---|
[218] | 2488 | #--------------------------------------------------------------- |
---|
[4] | 2489 | sub cmd_exportsw { |
---|
[113] | 2490 | @ARGV = @_; |
---|
[2] | 2491 | |
---|
[34] | 2492 | test_switchdb_environnement(); |
---|
| 2493 | |
---|
[4] | 2494 | my $format = 'txt'; |
---|
| 2495 | |
---|
[138] | 2496 | GetOptions( |
---|
[4] | 2497 | 'format|f=s' => \$format, |
---|
| 2498 | ); |
---|
| 2499 | |
---|
| 2500 | my %possible_format = ( |
---|
| 2501 | txt => \&cmd_exportsw_txt, |
---|
| 2502 | dot => \&cmd_exportsw_dot, |
---|
| 2503 | ); |
---|
| 2504 | |
---|
| 2505 | $format = 'txt' if not defined $possible_format{$format}; |
---|
[63] | 2506 | |
---|
[4] | 2507 | $possible_format{$format}->(@ARGV); |
---|
[63] | 2508 | return; |
---|
[4] | 2509 | } |
---|
| 2510 | |
---|
[218] | 2511 | #--------------------------------------------------------------- |
---|
[4] | 2512 | sub cmd_exportsw_txt { |
---|
| 2513 | |
---|
[44] | 2514 | my $switch_connection = YAML::Syck::LoadFile("$KLASK_SW_FILE"); |
---|
[4] | 2515 | |
---|
| 2516 | my %db_switch_output_port = %{$switch_connection->{output_port}}; |
---|
| 2517 | my %db_switch_parent = %{$switch_connection->{parent}}; |
---|
| 2518 | my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}}; |
---|
| 2519 | |
---|
[63] | 2520 | print "Switch output port and parent port connection\n"; |
---|
[4] | 2521 | print "---------------------------------------------\n"; |
---|
| 2522 | for my $sw (sort keys %db_switch_output_port) { |
---|
[233] | 2523 | my $arrow ='-->'; |
---|
| 2524 | $arrow ='==>' if $db_switch_output_port{$sw} =~ m/^(Trk|Br|Po)/; |
---|
[4] | 2525 | if (exists $db_switch_parent{$sw}) { |
---|
[233] | 2526 | printf "%-28s %8s %3s %-8s %-25s\n", $sw, $db_switch_output_port{$sw}, $arrow, $db_switch_parent{$sw}->{port_hr}, $db_switch_parent{$sw}->{'switch'}; |
---|
[4] | 2527 | } |
---|
| 2528 | else { |
---|
[233] | 2529 | printf "%-28s %8s %3s %-8s %-25s\n", $sw, $db_switch_output_port{$sw}, $arrow, '', 'router'; |
---|
[4] | 2530 | } |
---|
| 2531 | } |
---|
| 2532 | print "\n"; |
---|
| 2533 | |
---|
| 2534 | print "Switch parent and children port inter-connection\n"; |
---|
| 2535 | print "------------------------------------------------\n"; |
---|
[63] | 2536 | for my $swport (sort keys %db_switch_connected_on_port) { |
---|
| 2537 | my ($sw_connect,$port_connect) = split m/ : /xms, $swport; |
---|
[4] | 2538 | for my $sw (keys %{$db_switch_connected_on_port{$swport}}) { |
---|
[233] | 2539 | my $arrow ='<--'; |
---|
| 2540 | $arrow ='<==' if $port_connect =~ m/^(Trk|Br|Po)/; |
---|
[4] | 2541 | if (exists $db_switch_output_port{$sw}) { |
---|
[233] | 2542 | printf "%-28s %8s %3s %-8s %-25s\n", $sw_connect, $port_connect, $arrow, $db_switch_output_port{$sw}, $sw; |
---|
[4] | 2543 | } |
---|
| 2544 | else { |
---|
[233] | 2545 | printf "%-28s %8s %3s %-8s %-25s\n", $sw_connect, $port_connect, $arrow, '', $sw; |
---|
[4] | 2546 | } |
---|
| 2547 | } |
---|
| 2548 | } |
---|
[63] | 2549 | return; |
---|
[4] | 2550 | } |
---|
| 2551 | |
---|
[218] | 2552 | #--------------------------------------------------------------- |
---|
[4] | 2553 | sub cmd_exportsw_dot { |
---|
| 2554 | |
---|
[44] | 2555 | my $switch_connection = YAML::Syck::LoadFile("$KLASK_SW_FILE"); |
---|
[63] | 2556 | |
---|
[2] | 2557 | my %db_switch_output_port = %{$switch_connection->{output_port}}; |
---|
| 2558 | my %db_switch_parent = %{$switch_connection->{parent}}; |
---|
| 2559 | my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}}; |
---|
| 2560 | my %db_switch_link_with = %{$switch_connection->{link_with}}; |
---|
[25] | 2561 | my %db_switch_global = %{$switch_connection->{switch_db}}; |
---|
| 2562 | |
---|
[2] | 2563 | my %db_building= (); |
---|
[196] | 2564 | for my $sw (@SWITCH_LIST) { |
---|
[63] | 2565 | my ($building, $location) = split m/ \/ /xms, $sw->{location}, 2; |
---|
[2] | 2566 | $db_building{$building} ||= {}; |
---|
| 2567 | $db_building{$building}->{$location} ||= {}; |
---|
| 2568 | $db_building{$building}->{$location}{ $sw->{hostname} } = 'y'; |
---|
| 2569 | } |
---|
[63] | 2570 | |
---|
| 2571 | |
---|
[2] | 2572 | print "digraph G {\n"; |
---|
[132] | 2573 | print "rankdir = LR;\n"; |
---|
[203] | 2574 | #print "splines=polyline;\n"; |
---|
[2] | 2575 | |
---|
[4] | 2576 | print "site [label = \"site\", color = black, fillcolor = gold, shape = invhouse, style = filled];\n"; |
---|
| 2577 | print "internet [label = \"internet\", color = black, fillcolor = cyan, shape = house, style = filled];\n"; |
---|
[2] | 2578 | |
---|
[202] | 2579 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime time; |
---|
| 2580 | $year += 1900; |
---|
| 2581 | $mon++; |
---|
| 2582 | my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; |
---|
| 2583 | print "\"$date\" [ color = white, fillcolor = black, shape = polygon, sides=14, style = filled, fontcolor = white ]\n"; |
---|
[203] | 2584 | print "site -> \"$date\" [color = white];\n"; |
---|
[201] | 2585 | |
---|
[2] | 2586 | my $b=0; |
---|
| 2587 | for my $building (keys %db_building) { |
---|
| 2588 | $b++; |
---|
[63] | 2589 | |
---|
[4] | 2590 | print "\"building$b\" [label = \"$building\", color = black, fillcolor = gold, style = filled];\n"; |
---|
| 2591 | print "site -> \"building$b\" [len = 2, color = firebrick];\n"; |
---|
[2] | 2592 | |
---|
| 2593 | my $l = 0; |
---|
| 2594 | for my $loc (keys %{$db_building{$building}}) { |
---|
| 2595 | $l++; |
---|
[63] | 2596 | |
---|
| 2597 | print "\"location$b-$l\" [label = \"$building" . q{/} . join(q{\n}, split(m{ / }xms, $loc)) . "\", color = black, fillcolor = orange, style = filled];\n"; |
---|
[33] | 2598 | # print "\"location$b-$l\" [label = \"$building / $loc\", color = black, fillcolor = orange, style = filled];\n"; |
---|
[4] | 2599 | print "\"building$b\" -> \"location$b-$l\" [len = 2, color = firebrick]\n"; |
---|
[2] | 2600 | |
---|
| 2601 | for my $sw (keys %{$db_building{$building}->{$loc}}) { |
---|
| 2602 | |
---|
[156] | 2603 | print "\"$sw:$db_switch_output_port{$sw}\" [label = \"$db_switch_output_port{$sw}\", color = black, fillcolor = lightblue, peripheries = 2, style = filled];\n"; |
---|
[2] | 2604 | |
---|
[25] | 2605 | my $swname = $sw; |
---|
[63] | 2606 | $swname .= q{\n-\n} . "$db_switch_global{$sw}->{model}" if exists $db_switch_global{$sw} and exists $db_switch_global{$sw}->{model}; |
---|
[25] | 2607 | print "\"$sw\" [label = \"$swname\", color = black, fillcolor = palegreen, shape = rect, style = filled];\n"; |
---|
[4] | 2608 | print "\"location$b-$l\" -> \"$sw\" [len = 2, color = firebrick, arrowtail = dot]\n"; |
---|
| 2609 | print "\"$sw\" -> \"$sw:$db_switch_output_port{$sw}\" [len=2, style=bold, arrowhead = normal, arrowtail = invdot]\n"; |
---|
[2] | 2610 | |
---|
| 2611 | |
---|
| 2612 | for my $swport (keys %db_switch_connected_on_port) { |
---|
[63] | 2613 | my ($sw_connect,$port_connect) = split m/ : /xms, $swport; |
---|
[2] | 2614 | next if not $sw_connect eq $sw; |
---|
| 2615 | next if $port_connect eq $db_switch_output_port{$sw}; |
---|
[156] | 2616 | print "\"$sw:$port_connect\" [label = \"$port_connect\", color = black, fillcolor = plum, peripheries = 1, style = filled];\n"; |
---|
[4] | 2617 | print "\"$sw:$port_connect\" -> \"$sw\" [len=2, style=bold, arrowhead= normal, arrowtail = inv]\n"; |
---|
[2] | 2618 | } |
---|
| 2619 | } |
---|
| 2620 | } |
---|
| 2621 | } |
---|
| 2622 | |
---|
[63] | 2623 | # print "Switch output port and parent port connection\n"; |
---|
[2] | 2624 | # print "---------------------------------------------\n"; |
---|
| 2625 | for my $sw (sort keys %db_switch_output_port) { |
---|
| 2626 | if (exists $db_switch_parent{$sw}) { |
---|
[221] | 2627 | # printf " \"%s:%s\" -> \"%s:%s\"\n", $sw, $db_switch_output_port{$sw}, $db_switch_parent{$sw}->{'switch'}, $db_switch_parent{$sw}->{port}; |
---|
[2] | 2628 | } |
---|
| 2629 | else { |
---|
| 2630 | printf " \"%s:%s\" -> internet\n", $sw, $db_switch_output_port{$sw}; |
---|
| 2631 | } |
---|
| 2632 | } |
---|
| 2633 | print "\n"; |
---|
| 2634 | |
---|
| 2635 | # print "Switch parent and children port inter-connection\n"; |
---|
| 2636 | # print "------------------------------------------------\n"; |
---|
[63] | 2637 | for my $swport (sort keys %db_switch_connected_on_port) { |
---|
| 2638 | my ($sw_connect,$port_connect) = split m/ : /xms, $swport; |
---|
[2] | 2639 | for my $sw (keys %{$db_switch_connected_on_port{$swport}}) { |
---|
| 2640 | if (exists $db_switch_output_port{$sw}) { |
---|
| 2641 | printf " \"%s:%s\" -> \"%s:%s\" [color = navyblue]\n", $sw, $db_switch_output_port{$sw}, $sw_connect, $port_connect; |
---|
| 2642 | } |
---|
| 2643 | else { |
---|
| 2644 | printf " \"%s\" -> \"%s%s\"\n", $sw, $sw_connect, $port_connect; |
---|
| 2645 | } |
---|
| 2646 | } |
---|
| 2647 | } |
---|
| 2648 | |
---|
| 2649 | print "}\n"; |
---|
[63] | 2650 | return; |
---|
[2] | 2651 | } |
---|
| 2652 | |
---|
| 2653 | |
---|
[218] | 2654 | ################################################################ |
---|
| 2655 | # documentation |
---|
| 2656 | ################################################################ |
---|
| 2657 | |
---|
[2] | 2658 | __END__ |
---|
| 2659 | |
---|
| 2660 | =head1 NAME |
---|
| 2661 | |
---|
[219] | 2662 | klask - port and search manager for switches, map management |
---|
[2] | 2663 | |
---|
| 2664 | |
---|
[63] | 2665 | =head1 USAGE |
---|
[2] | 2666 | |
---|
[226] | 2667 | klask version |
---|
| 2668 | klask help |
---|
| 2669 | |
---|
[219] | 2670 | klask updatedb [--verbose|-v] [--verb-description|-d] [--chk-hostname|-h] [--chk-location|-l] |
---|
[226] | 2671 | klask exportdb [--format|-f txt|html] |
---|
[223] | 2672 | klask removedb IP* computer* |
---|
[219] | 2673 | klask cleandb [--verbose|-v] --day number_of_day --repair-dns |
---|
[2] | 2674 | |
---|
[219] | 2675 | klask updatesw [--verbose|-v] |
---|
[226] | 2676 | klask exportsw [--format|-f txt|dot] |
---|
[5] | 2677 | |
---|
[226] | 2678 | klask searchdb [--kind|-k host|mac] computer [mac-address] |
---|
[2] | 2679 | klask search computer |
---|
[219] | 2680 | klask search-mac-on-switch [--verbose|-v] [--vlan|-i vlan-id] switch mac_addr |
---|
[2] | 2681 | |
---|
[226] | 2682 | klask ip-free [--verbose|-v] [--day|-d days-to-death] [--format|-f txt|html] [vlan_name] |
---|
[69] | 2683 | |
---|
[224] | 2684 | klask bad-vlan-id [--day|-d days_before_alert] |
---|
| 2685 | |
---|
[219] | 2686 | klask enable [--verbose|-v] switch port |
---|
| 2687 | klask disable [--verbose|-v] switch port |
---|
| 2688 | klask status [--verbose|-v] switch port |
---|
[2] | 2689 | |
---|
| 2690 | |
---|
| 2691 | =head1 DESCRIPTION |
---|
| 2692 | |
---|
[212] | 2693 | Klask is a small tool to find where is a host in a big network. |
---|
| 2694 | Klask mean search in brittany. |
---|
[200] | 2695 | No hight level protocol like CDL, LLDP are use. |
---|
| 2696 | Everything is just done with SNMP request on MAC address. |
---|
[2] | 2697 | |
---|
[212] | 2698 | Limitation : loop cannot be detected and could be problematic when the map is created (C<updatesw> method). |
---|
[200] | 2699 | If you use PVST or MSTP and create loop between VLAN, |
---|
[212] | 2700 | you have to use C<portignore> functionality on switch port to cut manually loop |
---|
[200] | 2701 | (see config file below). |
---|
| 2702 | |
---|
[212] | 2703 | When you use a management port to administrate a switch, |
---|
| 2704 | it's not possible to create the map with this switch because it does not have a MAC address, |
---|
| 2705 | so other switch cannot find the real downlink port... |
---|
| 2706 | One way to work around this problem is, if you have a computer directly connected on the switch, |
---|
| 2707 | to put this IP as the fake ip for the switch. |
---|
[200] | 2708 | The MAC address associated will be use just for the map detection. |
---|
[212] | 2709 | The C<fake-ip> parameter is defined in the config file. |
---|
[200] | 2710 | |
---|
[219] | 2711 | Klask has now a web site dedicated for it: L<http://servforge.legi.grenoble-inp.fr/projects/klask>! |
---|
[2] | 2712 | |
---|
[6] | 2713 | |
---|
[2] | 2714 | =head1 COMMANDS |
---|
| 2715 | |
---|
[225] | 2716 | Some command are defined in the source code but are not documented here. |
---|
| 2717 | Theses could be not well defined, not finished, not well tested... |
---|
| 2718 | You can read the source code and use them at your own risk |
---|
| 2719 | (like for all the Klask code). |
---|
[2] | 2720 | |
---|
| 2721 | =head2 search |
---|
| 2722 | |
---|
[226] | 2723 | klask search computer |
---|
| 2724 | |
---|
[200] | 2725 | This command takes one or more computer in argument. |
---|
| 2726 | It search a computer on the network and give the port and the switch on which the computer is connected. |
---|
[2] | 2727 | |
---|
[227] | 2728 | =head2 search-mac-on-switch |
---|
[2] | 2729 | |
---|
[226] | 2730 | klask search-mac-on-switch [--verbose|-v] [--vlan|-i vlan-id] switch mac_addr |
---|
| 2731 | |
---|
| 2732 | This command search a MAC address on a switch. |
---|
[229] | 2733 | To search on all switch, you could put C<'*'> or C<all>. |
---|
| 2734 | The VLAN parameter could help. |
---|
[226] | 2735 | |
---|
| 2736 | |
---|
[2] | 2737 | =head2 enable |
---|
| 2738 | |
---|
[223] | 2739 | klask enable [--verbose|-v] switch port |
---|
[219] | 2740 | |
---|
[227] | 2741 | This command activate a port (or an agrregate bridge port) on a switch by SNMP. |
---|
[219] | 2742 | So you need to give the switch name and a port on the command line. |
---|
[225] | 2743 | See L</ABBREVIATION FOR PORT>. |
---|
[2] | 2744 | |
---|
[227] | 2745 | Warning: You need to have the SNMP write access on the switch in order to modify it's configuration. |
---|
[2] | 2746 | |
---|
[219] | 2747 | |
---|
[2] | 2748 | =head2 disable |
---|
| 2749 | |
---|
[219] | 2750 | klask disable [--verbose|-v] switch port |
---|
| 2751 | |
---|
[227] | 2752 | This command deactivate a port (or an agrregate bridge port) on a switch by SNMP. |
---|
[219] | 2753 | So you need to give the switch name and a port on the command line. |
---|
[225] | 2754 | See L</ABBREVIATION FOR PORT>. |
---|
[2] | 2755 | |
---|
[227] | 2756 | Warning: You need to have the SNMP write access on the switch in order to modify it's configuration. |
---|
[2] | 2757 | |
---|
[219] | 2758 | |
---|
[2] | 2759 | =head2 status |
---|
| 2760 | |
---|
[223] | 2761 | klask status [--verbose|-v] switch port |
---|
[219] | 2762 | |
---|
[200] | 2763 | This command return the status of a port number on a switch by SNMP. |
---|
[219] | 2764 | The return value could be C<enable> or C<disable> word. |
---|
| 2765 | So you need to give the switch name and a port on the command line. |
---|
[225] | 2766 | See L</ABBREVIATION FOR PORT>. |
---|
[2] | 2767 | |
---|
[227] | 2768 | If it's not possible to change port status with command L</enable> and L</disable> |
---|
| 2769 | (SNMP community read write access), |
---|
| 2770 | it's always possible to have the port status even for bridge agrregate port. |
---|
[219] | 2771 | |
---|
[227] | 2772 | |
---|
[2] | 2773 | =head2 updatedb |
---|
| 2774 | |
---|
[223] | 2775 | klask updatedb [--verbose|-v] [--verb-description|-d] [--chk-hostname|-h] [--chk-location|-l] |
---|
| 2776 | |
---|
[226] | 2777 | This command will scan networks and update the computer database. |
---|
| 2778 | To know which are the cmputer scanned, you have to configure the file F</etc/klask/klask.conf>. |
---|
| 2779 | This file is easy to read and write because Klask use YAML format and not XML |
---|
| 2780 | (see L</CONFIGURATION>). |
---|
[2] | 2781 | |
---|
[226] | 2782 | Option are not stable and could be use manually when you have a new kind of switch. |
---|
[223] | 2783 | Maybe some option will be transfered in a future C<checksw> command! |
---|
[2] | 2784 | |
---|
[227] | 2785 | The network parameter C<scan-mode> can have two values: C<active> or C<passive>. |
---|
| 2786 | By default, a network is C<active>. |
---|
| 2787 | This means that an C<fping> command is done at the beginning on all the IP of the network |
---|
| 2788 | and the computers that was not detected in this pass, but where their Klask entry is less than one week, |
---|
| 2789 | will have an C<arping> |
---|
| 2790 | (some OS do not respond to C<ping> but a computer have to respond to C<arping> if it want to interact with other). |
---|
| 2791 | In the scan mode C<passive>, no C<fping> and no C<arping> are done. |
---|
| 2792 | It's good for big subnet with few computer (telephone...). |
---|
| 2793 | The idea of the C<active> scan mode is to force computer to regulary send packet over the network. |
---|
| 2794 | |
---|
[223] | 2795 | =head2 exportdb |
---|
[2] | 2796 | |
---|
[226] | 2797 | klask exportdb [--format|-f txt|html] |
---|
[223] | 2798 | |
---|
[226] | 2799 | This command print the content of the computer database. |
---|
| 2800 | There is actually only two format : TXT and HTML. |
---|
| 2801 | By default, format is TXT. |
---|
[200] | 2802 | It's very easy to have more format, it's just need times... |
---|
[2] | 2803 | |
---|
[190] | 2804 | =head2 removedb |
---|
[2] | 2805 | |
---|
[223] | 2806 | klask removedb IP* computer* |
---|
| 2807 | |
---|
[190] | 2808 | This command remove an entry in the database. |
---|
| 2809 | There is only one kind of parameter, the IP of the computers to be removed. |
---|
| 2810 | You can put as many IP as you want... |
---|
| 2811 | |
---|
[226] | 2812 | Computer DNS names are also a valid entry because a DNS resolver is executed at the beginning. |
---|
[190] | 2813 | |
---|
| 2814 | =head2 cleandb |
---|
| 2815 | |
---|
[223] | 2816 | klask cleandb [--verbose|-v] --day number_of_day --repair-dns |
---|
[190] | 2817 | |
---|
[223] | 2818 | Remove double entry (same MAC-Address) in the computer database when the older one is older than X day (C<--day>) the new one. |
---|
| 2819 | Computer name beginning by 'float' (regex C<^float>) are not really taken into account but could be remove. |
---|
| 2820 | This could be configure with the global regex parameter C<float-regex> in the configuration file F</etc/klask/klask.conf>. |
---|
| 2821 | This functionality could be use when computer define in VLAN 1 |
---|
| 2822 | could have a float IP when they are connected on VLAN 2. |
---|
| 2823 | In the Klask database, the float DNS entries are less important. |
---|
[190] | 2824 | |
---|
[226] | 2825 | When reverse DNS has not been done by the past, option C<--repair-dns> force a reverse DNS check on all unkown host. |
---|
[223] | 2826 | |
---|
[5] | 2827 | =head2 updatesw |
---|
| 2828 | |
---|
[224] | 2829 | klask updatesw [--verbose|-v] |
---|
| 2830 | |
---|
[200] | 2831 | This command build a map of your manageable switch on your network. |
---|
[226] | 2832 | The list of the switches must be given in the file F</etc/klask/klask.conf> (see L</CONFIGURATION>). |
---|
[5] | 2833 | |
---|
| 2834 | |
---|
[224] | 2835 | =head2 exportsw |
---|
[5] | 2836 | |
---|
[226] | 2837 | klask exportsw [--format|-f txt|dot] |
---|
[224] | 2838 | |
---|
[200] | 2839 | This command print the content of the switch database. There is actually two format. |
---|
[226] | 2840 | One is just TXT for terminal and the other is the DOT format from the graphviz environnement. |
---|
| 2841 | By default, format is TXT. |
---|
[5] | 2842 | |
---|
| 2843 | klask exportsw --format dot > /tmp/map.dot |
---|
| 2844 | dot -Tpng /tmp/map.dot > /tmp/map.png |
---|
| 2845 | |
---|
| 2846 | |
---|
[224] | 2847 | =head2 ip-free |
---|
| 2848 | |
---|
[226] | 2849 | klask ip-free [--verbose|-v] [--day|-d days-to-death] [--format|-f txt|html] [vlan_name] |
---|
[224] | 2850 | |
---|
| 2851 | This command return IP address that was not use (detected by Klask) at this time. |
---|
| 2852 | The list returned could be limited to just one VLAN. |
---|
| 2853 | IP returned could have been never used or no computer have been detected since the number of days specified |
---|
| 2854 | (2 years by default). |
---|
[225] | 2855 | This parameter could also be define in the configuration file F</etc/klask/klask.conf> (see L</CONFIGURATION>). |
---|
[224] | 2856 | |
---|
| 2857 | default: |
---|
| 2858 | days-to-death: 730 |
---|
| 2859 | |
---|
| 2860 | Computer that does not have the good IP but takes a float one (see L</cleandb>) are taken into account. |
---|
| 2861 | |
---|
| 2862 | |
---|
| 2863 | =head2 bad-vlan-id |
---|
| 2864 | |
---|
| 2865 | klask bad-vlan-id [--day|-d days_before_alert] |
---|
| 2866 | |
---|
| 2867 | This command return a list of switch port that are not configure with the good VLAN. |
---|
| 2868 | Computer which are in bad VLAN are detected with the float regex parameter (see L</cleandb>) |
---|
| 2869 | and another prior trace where they had the good IP (good DNS name). |
---|
| 2870 | The computer must stay connected on a bad VLAN more than XX days (15 days by default) before alert. |
---|
[225] | 2871 | This parameter could also define in the configuration file F</etc/klask/klask.conf> (see L</CONFIGURATION>). |
---|
[224] | 2872 | |
---|
| 2873 | default: |
---|
| 2874 | days-before-alert: 15 |
---|
| 2875 | |
---|
| 2876 | This functionality is not need if your switch use RADIUS 802.1X configuration... |
---|
| 2877 | |
---|
| 2878 | |
---|
| 2879 | |
---|
[2] | 2880 | =head1 CONFIGURATION |
---|
| 2881 | |
---|
[219] | 2882 | Because Klask need many parameters, it's not possible actually to use command line parameters for everything. |
---|
[201] | 2883 | The configuration is done in a F</etc/klask/klask.conf> YAML file. |
---|
[200] | 2884 | This format have many advantage over XML, it's easier to read and to write ! |
---|
[2] | 2885 | |
---|
| 2886 | Here an example, be aware with indent, it's important in YAML, do not use tabulation ! |
---|
| 2887 | |
---|
| 2888 | default: |
---|
| 2889 | community: public |
---|
[220] | 2890 | community-rw: private |
---|
[2] | 2891 | snmpport: 161 |
---|
[223] | 2892 | float-regex: '(?^msx: ^float )' |
---|
[227] | 2893 | scan-mode: active |
---|
[2] | 2894 | |
---|
| 2895 | network: |
---|
[5] | 2896 | labnet: |
---|
| 2897 | ip-subnet: |
---|
| 2898 | - add: 192.168.1.0/24 |
---|
| 2899 | - add: 192.168.2.0/24 |
---|
| 2900 | interface: eth0 |
---|
[201] | 2901 | vlan-id: 12 |
---|
[5] | 2902 | main-router: gw1.labnet.local |
---|
[2] | 2903 | |
---|
[5] | 2904 | schoolnet: |
---|
| 2905 | ip-subnet: |
---|
[227] | 2906 | - add: 192.168.3.0/24 |
---|
| 2907 | - add: 192.168.4.0/24 |
---|
[5] | 2908 | interface: eth0.38 |
---|
[201] | 2909 | vlan-id: 13 |
---|
[5] | 2910 | main-router: gw2.schoolnet.local |
---|
[227] | 2911 | scan-mode: passive |
---|
[5] | 2912 | |
---|
[227] | 2913 | etunet: |
---|
| 2914 | ip-subnet: |
---|
| 2915 | - add: 192.168.5.0/24 |
---|
| 2916 | interface: eth2 |
---|
| 2917 | vlan-id: 14 |
---|
| 2918 | main-router: gw3.etunet.local |
---|
| 2919 | scan-mode: passive |
---|
| 2920 | |
---|
[2] | 2921 | switch: |
---|
| 2922 | - hostname: sw1.klask.local |
---|
[227] | 2923 | location: BatY / 1 floor / K004 |
---|
[2] | 2924 | portignore: |
---|
| 2925 | - 1 |
---|
| 2926 | - 2 |
---|
| 2927 | |
---|
| 2928 | - hostname: sw2.klask.local |
---|
[227] | 2929 | location: BatY / 2 floor / K203 |
---|
[5] | 2930 | type: HP2424 |
---|
[2] | 2931 | portignore: |
---|
| 2932 | - 1 |
---|
| 2933 | - 2 |
---|
[200] | 2934 | fake-ip: 192.168.9.14 |
---|
[2] | 2935 | |
---|
[227] | 2936 | - hostname: sw3.klask.local |
---|
| 2937 | location: BatY / 2 floor / K203 |
---|
| 2938 | |
---|
[200] | 2939 | I think it's pretty easy to understand. |
---|
| 2940 | The default section can be overide in any section, if parameter mean something in theses sections. |
---|
[212] | 2941 | Network to be scan are define in the network section. You must put an add by network. |
---|
| 2942 | Maybe I will make a delete line to suppress specific computers. |
---|
[200] | 2943 | The switch section define your switch. |
---|
[219] | 2944 | You have to write the port number to ignore, this was important if your switchs are cascades |
---|
[212] | 2945 | (right now, method C<updatesw> find them automatically) |
---|
| 2946 | and is still important if you have loop (with PVST or MSTP). |
---|
| 2947 | Just put the ports numbers between switch. |
---|
[2] | 2948 | |
---|
[222] | 2949 | The C<community> parameter is use to get SNMP data on switch. |
---|
| 2950 | It could be overload for each switch. |
---|
| 2951 | By default, it's value is C<public> and you have to configure a readonly word for safety reason. |
---|
| 2952 | Some few command change the switch state as the commands L</enable> and L</disable>. |
---|
| 2953 | In theses rares cases, you need a readwrite SNMP community word define in your configuration file. |
---|
| 2954 | Klask then use since version C<0.6.2> the C<community-rw> parameter which by default is egal to C<private>. |
---|
[2] | 2955 | |
---|
[222] | 2956 | |
---|
[218] | 2957 | =head1 ABBREVIATION FOR PORT |
---|
| 2958 | |
---|
| 2959 | HP Procurve and Nexans switches have a simplistic numbering scheme. |
---|
| 2960 | It's just number: 1, 2, 3... 24. |
---|
| 2961 | On HP8000 chassis, ports names begin with an uppercase letter: A1, A2... |
---|
| 2962 | Nothing is done on theses ports names. |
---|
| 2963 | |
---|
| 2964 | On HP Comware and DELL, port digitization schema use a port speed word (generally a very verbose word) |
---|
| 2965 | followed by tree number. |
---|
| 2966 | In order to have short name, |
---|
| 2967 | we made the following rules: |
---|
| 2968 | |
---|
| 2969 | Bridge-Aggregation -> Br |
---|
| 2970 | FastEthernet -> Fa |
---|
| 2971 | Forty-GigabitEthernet -> Fo |
---|
| 2972 | FortyGigabitEthernet -> Fo |
---|
| 2973 | GigabitEthernet -> Gi |
---|
| 2974 | Giga -> Gi |
---|
| 2975 | Port-Channel -> Po |
---|
| 2976 | Ten-GigabitEthernet -> Te |
---|
| 2977 | TenGigabitEthernet -> Te |
---|
| 2978 | Ten -> Te |
---|
| 2979 | |
---|
| 2980 | All Klask command automatically normalize the port name on standart output |
---|
| 2981 | and also on input command line. |
---|
| 2982 | |
---|
| 2983 | |
---|
[2] | 2984 | =head1 FILES |
---|
| 2985 | |
---|
[92] | 2986 | /etc/klask/klask.conf |
---|
[100] | 2987 | /var/lib/klask/klaskdb |
---|
| 2988 | /var/lib/klask/switchdb |
---|
[2] | 2989 | |
---|
[219] | 2990 | |
---|
[2] | 2991 | =head1 SEE ALSO |
---|
| 2992 | |
---|
| 2993 | Net::SNMP, Net::Netmask, Net::CIDR::Lite, NetAddr::IP, YAML |
---|
| 2994 | |
---|
[219] | 2995 | =over |
---|
[2] | 2996 | |
---|
[220] | 2997 | =item * L<Web site|http://servforge.legi.grenoble-inp.fr/projects/klask> |
---|
[219] | 2998 | |
---|
[220] | 2999 | =item * L<Online Manual|http://servforge.legi.grenoble-inp.fr/pub/klask/klask.html> |
---|
[219] | 3000 | |
---|
| 3001 | =back |
---|
| 3002 | |
---|
| 3003 | |
---|
[2] | 3004 | =head1 VERSION |
---|
| 3005 | |
---|
[36] | 3006 | $Id: klask 233 2017-03-11 21:56:58Z g7moreau $ |
---|
[2] | 3007 | |
---|
| 3008 | |
---|
| 3009 | =head1 AUTHOR |
---|
| 3010 | |
---|
[5] | 3011 | Written by Gabriel Moreau, Grenoble - France |
---|
[2] | 3012 | |
---|
| 3013 | |
---|
[63] | 3014 | =head1 LICENSE AND COPYRIGHT |
---|
[2] | 3015 | |
---|
| 3016 | GPL version 2 or later and Perl equivalent |
---|
[45] | 3017 | |
---|
[195] | 3018 | Copyright (C) 2005-2017 Gabriel Moreau. |
---|