Changeset 218


Ignore:
Timestamp:
Feb 18, 2017, 3:56:47 PM (7 years ago)
Author:
g7moreau
Message:
  • Better documented code
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r217 r218  
    77use strict;
    88use warnings;
    9 use version; our $VERSION = qv('0.6.0');
     9use version; our $VERSION = qv('0.6.1');
    1010
    1111use Readonly;
     
    2424# libcrypt-des-perl libcrypt-hcesha-perl libdigest-hmac-perl
    2525# arping net-tools fping bind9-host arpwatch
     26
     27################################################################
     28# general initialization
     29################################################################
    2630
    2731my $KLASK_VAR      = '/var/lib/klask';
     
    8589   );
    8690
    87 Readonly my %INTERNAL_PORT_MAP => (
    88    0 => 'A',
    89    1 => 'B',
    90    2 => 'C',
    91    3 => 'D',
    92    4 => 'E',
    93    5 => 'F',
    94    6 => 'G',
    95    7 => 'H',
    96    );
    97 Readonly my %INTERNAL_PORT_MAP_REV => reverse %INTERNAL_PORT_MAP;
     91#Readonly my %INTERNAL_PORT_MAP => (
     92#   0 => 'A',
     93#   1 => 'B',
     94#   2 => 'C',
     95#   3 => 'D',
     96#   4 => 'E',
     97#   5 => 'F',
     98#   6 => 'G',
     99#   7 => 'H',
     100#   );
     101#Readonly my %INTERNAL_PORT_MAP_REV => reverse %INTERNAL_PORT_MAP;
    98102
    99103Readonly my %SWITCH_KIND => (
     
    180184Readonly my $RE_FLOAT_HOSTNAME => qr{ ^float }xms;
    181185
    182 ################
     186################################################################
    183187# main program
    184 ################
     188################################################################
    185189
    186190my $cmd = shift @ARGV || 'help';
     
    196200exit;
    197201
    198 ################
     202################################################################
    199203# subroutine
    200 ################
    201 
     204################################################################
     205
     206#---------------------------------------------------------------
    202207sub test_running_environnement {
    203208   die "Configuration file $KLASK_CFG_FILE does not exists. Klask need it !\n" if not -e "$KLASK_CFG_FILE";
     
    206211   }
    207212
     213#---------------------------------------------------------------
    208214sub test_switchdb_environnement {
    209215   die "Switch database $KLASK_SW_FILE does not exists. Launch updatesw before this command !\n" if not -e "$KLASK_SW_FILE";
     
    211217   }
    212218
     219#---------------------------------------------------------------
    213220sub test_maindb_environnement {
    214221   die "Main database $KLASK_DB_FILE does not exists. Launch updatedb before this command !\n" if not -e "$KLASK_DB_FILE";
     
    216223   }
    217224
    218 ###
     225#---------------------------------------------------------------
    219226# fast ping dont l'objectif est de remplir la table arp de la machine
    220227sub fast_ping {
     
    224231   }
    225232
     233#---------------------------------------------------------------
    226234sub shell_command {
    227235   my $cmd = shift;
     
    236244   }
    237245
    238 ###
     246#---------------------------------------------------------------
    239247# donne l'@ ip, dns, arp en fonction du dns OU de l'ip
    240248sub resolve_ip_arp_host {
     
    339347   }
    340348
    341 ###
     349#---------------------------------------------------------------
    342350# va rechercher le nom des switchs pour savoir qui est qui
    343351sub init_switch_names {
     
    399407   }
    400408
    401 ###
     409#---------------------------------------------------------------
    402410# convertit l'hexa (uniquement 2 chiffres) en decimal
    403411sub digit_hex_to_dec {
     
    415423   }
    416424
    417 #--------------------------------------------------------------------------------
     425#---------------------------------------------------------------
    418426
    419427sub normalize_mac_address {
     
    430438   }
    431439
    432 #--------------------------------------------------------------------------------
     440#---------------------------------------------------------------
    433441# convertit l'@ mac en decimal
    434442sub mac_address_hex_to_dec {
     
    444452   }
    445453
    446 ###
     454#---------------------------------------------------------------
    447455# va rechercher le port et le switch sur lequel est la machine
    448456sub find_switch_port {
     
    515523   }
    516524
    517 ###
     525#---------------------------------------------------------------
    518526# va rechercher les port et les switch sur lequel est la machine
    519527sub find_all_switch_port {
     
    568576   }
    569577
     578#---------------------------------------------------------------
    570579sub get_list_network {
    571580
     
    573582   }
    574583
     584#---------------------------------------------------------------
    575585sub get_current_interface {
    576586   my $vlan_name = shift;
     
    579589   }
    580590
     591#---------------------------------------------------------------
    581592sub get_current_vlan_id {
    582593   my $vlan_name = shift;
     
    586597   }
    587598
     599#---------------------------------------------------------------
    588600sub get_current_scan_mode {
    589601   my $vlan_name = shift;
     
    592604   }
    593605
     606#---------------------------------------------------------------
    594607sub get_current_vlan_name_for_interface {
    595608   my $interface = shift;
     
    601614   }
    602615
    603 ###
     616#---------------------------------------------------------------
    604617# liste l'ensemble des adresses ip d'un réseau
    605618sub get_list_ip {
     
    630643   }
    631644
     645#---------------------------------------------------------------
    632646# liste l'ensemble des routeurs du réseau
    633647sub get_list_main_router {
     
    643657   }
    644658
     659#---------------------------------------------------------------
    645660sub get_human_readable_port {
    646661   my $sw_model = shift;
     
    650665   # get port name by snmp
    651666   return $sw_port;
    652 
    653    if ($sw_model eq 'HP8000M') {
    654 
    655       my $reste = (($sw_port - 1) % 8) + 1;
    656       my $major = int (($sw_port - 1) / 8);
    657       return "$INTERNAL_PORT_MAP{$major}$reste";
    658       }
    659 
    660    if ($sw_model eq 'HP2424M') {
    661       if ($sw_port > 24) {
    662 
    663          my $reste = $sw_port - 24;
    664          return "A$reste";
    665          }
    666       }
    667 
    668    if ($sw_model eq 'HP1600M') {
    669       if ($sw_port > 16) {
    670 
    671          my $reste = $sw_port - 16;
    672          return "A$reste";
    673          }
    674       }
    675 
    676    if ($sw_model eq 'HP2810-48G' or $sw_model eq 'HP2810-24G') {
    677       if ($sw_port > 48) {
    678 
    679          my $reste = $sw_port - 48;
    680          return "Trk$reste";
    681          }
    682       }
    683 
    684    if ($sw_model eq 'HP3500-24G') {
    685       if ($sw_port > 289) {
    686 
    687          my $reste = $sw_port - 289;
    688          return "Trk$reste";
    689          }
    690       }
    691 
    692    return $sw_port;
    693    }
    694 
    695 sub get_numerical_port {
    696    my $sw_model = shift;
    697    my $sw_port  = shift;
    698 
    699    if ($sw_model eq 'HP8000M') {
    700 
    701       my $letter = substr $sw_port, 0, 1;
    702       my $reste =  substr $sw_port, 1;
    703 
    704       return $INTERNAL_PORT_MAP_REV{$letter} * 8 + $reste;
    705       }
    706 
    707    if ($sw_model eq 'HP2424M') {
    708       if ($sw_port =~ m/^A/xms ) {
    709 
    710          my $reste =  substr $sw_port, 1;
    711 
    712          return 24 + $reste;
    713          }
    714       }
    715 
    716    if ($sw_model eq 'HP1600M') {
    717       if ($sw_port =~ m/^A/xms ) {
    718 
    719          my $reste =  substr $sw_port, 1;
    720 
    721          return 16 + $reste;
    722          }
    723       }
    724 
    725    if ($sw_model eq 'HP2810-48G' or $sw_model eq 'HP2810-24G') {
    726       if ($sw_port =~ m/^Trk/xms ) {
    727 
    728          my $reste =  substr $sw_port, 3;
    729 
    730          return 48 + $reste;
    731          }
    732       }
    733 
    734    if ($sw_model eq 'HP3500-24G') {
    735       if ($sw_port =~ m/^Trk/xms ) {
    736 
    737          my $reste =  substr $sw_port, 3;
    738 
    739          return 289 + $reste;
    740          }
    741       }
    742 
    743    return $sw_port;
    744    }
    745 
     667   }
     668
     669#   if ($sw_model eq 'HP8000M') {
     670#
     671#      my $reste = (($sw_port - 1) % 8) + 1;
     672#      my $major = int (($sw_port - 1) / 8);
     673#      return "$INTERNAL_PORT_MAP{$major}$reste";
     674#      }
     675#
     676#   if ($sw_model eq 'HP2424M') {
     677#      if ($sw_port > 24) {
     678#
     679#         my $reste = $sw_port - 24;
     680#         return "A$reste";
     681#         }
     682#      }
     683#
     684#   if ($sw_model eq 'HP1600M') {
     685#      if ($sw_port > 16) {
     686#
     687#         my $reste = $sw_port - 16;
     688#         return "A$reste";
     689#         }
     690#      }
     691#
     692#   if ($sw_model eq 'HP2810-48G' or $sw_model eq 'HP2810-24G') {
     693#      if ($sw_port > 48) {
     694#
     695#         my $reste = $sw_port - 48;
     696#         return "Trk$reste";
     697#         }
     698#      }
     699#
     700#   if ($sw_model eq 'HP3500-24G') {
     701#      if ($sw_port > 289) {
     702#
     703#         my $reste = $sw_port - 289;
     704#         return "Trk$reste";
     705#         }
     706#      }
     707#
     708#   return $sw_port;
     709
     710#sub get_numerical_port {
     711#   my $sw_model = shift;
     712#   my $sw_port  = shift;
     713#
     714#   if ($sw_model eq 'HP8000M') {
     715#
     716#      my $letter = substr $sw_port, 0, 1;
     717#      my $reste =  substr $sw_port, 1;
     718#
     719#      return $INTERNAL_PORT_MAP_REV{$letter} * 8 + $reste;
     720#      }
     721#
     722#   if ($sw_model eq 'HP2424M') {
     723#      if ($sw_port =~ m/^A/xms ) {
     724#
     725#         my $reste =  substr $sw_port, 1;
     726#
     727#         return 24 + $reste;
     728#         }
     729#      }
     730#
     731#   if ($sw_model eq 'HP1600M') {
     732#      if ($sw_port =~ m/^A/xms ) {
     733#
     734#         my $reste =  substr $sw_port, 1;
     735#
     736#         return 16 + $reste;
     737#         }
     738#      }
     739#
     740#   if ($sw_model eq 'HP2810-48G' or $sw_model eq 'HP2810-24G') {
     741#      if ($sw_port =~ m/^Trk/xms ) {
     742#
     743#         my $reste =  substr $sw_port, 3;
     744#
     745#         return 48 + $reste;
     746#         }
     747#      }
     748#
     749#   if ($sw_model eq 'HP3500-24G') {
     750#      if ($sw_port =~ m/^Trk/xms ) {
     751#
     752#         my $reste =  substr $sw_port, 3;
     753#
     754#         return 289 + $reste;
     755#         }
     756#      }
     757#
     758#   return $sw_port;
     759#   }
     760
     761#---------------------------------------------------------------
    746762sub normalize_port_human_readable {
    747763   my $sw_port_hr  = shift;
     
    762778   }
    763779
     780#---------------------------------------------------------------
    764781sub snmp_get_switchport_hr {
    765782   my ($snmp_session, $swport) = @_;
     
    781798   }
    782799
     800#---------------------------------------------------------------
    783801# Reverse search port number
    784802sub snmp_get_switchport_num {
     
    819837   }
    820838
     839#---------------------------------------------------------------
    821840# Load computer database
    822841sub computerdb_load {
     
    834853   }
    835854
    836 ################
    837 # Les commandes
    838 ################
    839 
     855################################################################
     856# command
     857################################################################
     858
     859#---------------------------------------------------------------
    840860sub cmd_help {
    841861
     
    868888   }
    869889
     890#---------------------------------------------------------------
    870891sub cmd_version {
    871892
     
    904925   }
    905926
     927#---------------------------------------------------------------
    906928sub cmd_searchdb {
    907929   my @ARGV  = @_;
     
    925947
    926948
     949#---------------------------------------------------------------
    927950sub cmd_searchdb_host {
    928951   my @computer = @_;
     
    954977   }
    955978
     979#---------------------------------------------------------------
    956980sub cmd_searchdb_mac {
    957981   my @mac = map { normalize_mac_address($_) } @_;
     
    9841008   }
    9851009
     1010#---------------------------------------------------------------
    9861011sub cmd_updatedb {
    9871012   @ARGV = @_;
     
    11731198   }
    11741199
     1200#---------------------------------------------------------------
    11751201sub cmd_removedb {
    11761202   my @computer = @_;
     
    12011227   }
    12021228
     1229#---------------------------------------------------------------
    12031230sub cmd_cleandb {
    12041231   my @ARGV  = @_;
     
    13151342   }
    13161343
     1344#---------------------------------------------------------------
    13171345sub cmd_exportdb {
    13181346   @ARGV = @_;
     
    13351363   }
    13361364
     1365#---------------------------------------------------------------
    13371366sub cmd_exportdb_txt {
    13381367   test_maindb_environnement();
     
    13741403   }
    13751404
     1405#---------------------------------------------------------------
    13761406sub cmd_exportdb_html {
    13771407   test_maindb_environnement();
     
    15961626   }
    15971627
     1628#---------------------------------------------------------------
    15981629sub cmd_bad_vlan_id {
    15991630   test_maindb_environnement();
     
    16841715   }
    16851716
     1717#---------------------------------------------------------------
    16861718sub cmd_set_vlan_port {
    16871719   my $switch_name = shift || q{};
     
    17311763   }
    17321764
     1765#---------------------------------------------------------------
    17331766sub cmd_get_vlan_port {
    17341767   @ARGV = @_;
     
    17741807   }
    17751808
     1809#---------------------------------------------------------------
    17761810sub cmd_set_vlan_name {
    17771811   }
    17781812
     1813#---------------------------------------------------------------
    17791814# snmpset -v 1 -c public sw1-batG0-legi.hmg.priv "$OID_NUMBER{'hpicfReset'}.0" i 2;
    17801815sub cmd_rebootsw {
     
    18101845   }
    18111846
     1847#---------------------------------------------------------------
    18121848sub cmd_get_vlan_name {
    18131849   my $switch_name = shift || q{};
     
    18481884   }
    18491885
     1886#---------------------------------------------------------------
    18501887sub cmd_ip_location {
    18511888   my $computerdb = computerdb_load();
     
    18721909   }
    18731910
     1911#---------------------------------------------------------------
    18741912sub cmd_ip_free {
    18751913   @ARGV = @_; # VLAN name with option
     
    19481986   }
    19491987
     1988#---------------------------------------------------------------
    19501989sub cmd_ip_free_txt {
    19511990   my %result_ip = @_;
     
    19601999   }
    19612000
     2001#---------------------------------------------------------------
    19622002sub cmd_ip_free_html {
    19632003   my %result_ip = @_;
     
    20122052   }
    20132053
     2054#---------------------------------------------------------------
    20142055sub cmd_enable {
    20152056   @ARGV = @_;
     
    20562097   }
    20572098
     2099#---------------------------------------------------------------
    20582100sub cmd_disable {
    20592101   @ARGV = @_;
     
    20982140   }
    20992141
     2142#---------------------------------------------------------------
    21002143sub cmd_status {
    21012144   @ARGV = @_;
     
    21432186   }
    21442187
     2188#---------------------------------------------------------------
    21452189sub cmd_search_mac_on_switch {
    21462190   @ARGV = @_;
     
    22022246   }
    22032247
     2248#---------------------------------------------------------------
    22042249sub cmd_updatesw {
    22052250   @ARGV = @_;
     
    24212466   }
    24222467
     2468#---------------------------------------------------------------
    24232469sub cmd_exportsw {
    24242470   @ARGV = @_;
     
    24432489   }
    24442490
     2491#---------------------------------------------------------------
    24452492sub cmd_exportsw_txt {
    24462493
     
    24792526   }
    24802527
     2528#---------------------------------------------------------------
    24812529sub cmd_exportsw_dot {
    24822530
     
    25802628
    25812629
     2630################################################################
     2631# documentation
     2632################################################################
     2633
    25822634__END__
    25832635
     
    26452697
    26462698This command activate a port on a switch by SNMP.
    2647 So you need to give the switch and the port number on the command line.
    2648 Actually, port number must be a number and not the name alias...
     2699So you need to give the switch and a port on the command line.
     2700See L<ABBREVIATION FOR PORT>.
    26492701
    26502702
     
    26522704
    26532705This command deactivate a port on a switch by SNMP.
    2654 So you need to give the switch and the port number on the command line.
    2655 Actually, port number must be a number and not the name alias...
     2706So you need to give the switch and a port on the command line.
     2707See L<ABBREVIATION FOR PORT>.
    26562708
    26572709
     
    26592711
    26602712This command return the status of a port number on a switch by SNMP.
    2661 So you need to give the switch name and the port number on the command line.
    2662 Actually, port number must be a number and not the name alias...
    2663 
     2713So you need to give the switch and a port on the command line.
     2714See L<ABBREVIATION FOR PORT>.
    26642715
    26652716=head2 updatedb
     
    27602811
    27612812
     2813=head1 ABBREVIATION FOR PORT
     2814
     2815HP Procurve and Nexans switches have a simplistic numbering scheme.
     2816It's just number: 1, 2, 3... 24.
     2817On HP8000 chassis, ports names begin with an uppercase letter: A1, A2...
     2818Nothing is done on theses ports names.
     2819
     2820On HP Comware and DELL, port digitization schema use a port speed word (generally a very verbose word)
     2821followed by tree number.
     2822In order to have short name,
     2823we made the following rules:
     2824
     2825 Bridge-Aggregation     -> Br
     2826 FastEthernet           -> Fa
     2827 Forty-GigabitEthernet  -> Fo
     2828 FortyGigabitEthernet   -> Fo
     2829 GigabitEthernet        -> Gi
     2830 Giga                   -> Gi
     2831 Port-Channel           -> Po
     2832 Ten-GigabitEthernet    -> Te
     2833 TenGigabitEthernet     -> Te
     2834 Ten                    -> Te
     2835
     2836All Klask command automatically normalize the port name on standart output
     2837and also on input command line.
     2838
     2839
    27622840=head1 FILES
    27632841
Note: See TracChangeset for help on using the changeset viewer.