source: trunk/klask @ 367

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