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