Index: trunk/klask
===================================================================
--- trunk/klask	(revision 3)
+++ trunk/klask	(revision 4)
@@ -35,11 +35,12 @@
    updatedb   => \&cmd_updatedb,
    searchdb   => \&cmd_searchdb,
+   removedb   => \&cmd_removedb,
    search     => \&cmd_search,
    enable     => \&cmd_enable,
    disable    => \&cmd_disable,
    status     => \&cmd_status,
-   switchmap  => \&cmd_switchmap,
-   dotsw      => \&cmd_swdot,
-   removedb   => \&cmd_removedb,
+   updatesw   => \&cmd_updatesw,
+   exportsw   => \&cmd_exportsw,
+   dotsw      => \&cmd_exportsw_dot,
    iplocation => \&cmd_iplocation,
    );
@@ -159,5 +160,5 @@
 ###
 # va rechercher le nom des switchs pour savoir qui est qui
-sub initswitchnames {
+sub init_switch_names {
    my $verbose = shift;
    
@@ -211,12 +212,11 @@
 ###
 # convertit l'hexa (uniquement 2 chiffres) en decimal
-sub hextodec {
+sub hex_to_dec {
    #00:0F:1F:43:E4:2B
-   my $car = '00' . shift;
-
-   return '00' if $car eq '00unknow';
+   my $car = '00' . uc(shift);
+
+   return '00' if $car eq '00UNKNOW';
    my %table = (
       "0"=>"0",  "1"=>"1",  "2"=>"2",  "3"=>"3",  "4"=>"4",  "5"=>"5", "6"=>"6", "7"=>"7", "8"=>"8", "9"=>"9",
-      "a"=>"10", "b"=>"11", "c"=>"12", "d"=>"13", "e"=>"14", "f"=>"15",
       "A"=>"10", "B"=>"11", "C"=>"12", "D"=>"13", "E"=>"14", "F"=>"15"
       );
@@ -227,5 +227,5 @@
 ###
 # convertit l'@ arp en decimal
-sub arphextodec {
+sub arp_hex_to_dec {
    #00:0F:1F:43:E4:2B
    my $arp = shift;
@@ -234,5 +234,5 @@
    my $return = '';
    foreach(@paquets) {
-      $return .= ".".hextodec($_);
+      $return .= ".".hex_to_dec($_);
       }
    return $return;
@@ -260,5 +260,5 @@
       }
 
-   my $research = "1.3.6.1.2.1.17.4.3.1.2".arphextodec($arp);
+   my $research = "1.3.6.1.2.1.17.4.3.1.2".arp_hex_to_dec($arp);
    
    LOOP_ON_SWITCH:
@@ -327,5 +327,5 @@
       }
 
-   my $research = "1.3.6.1.2.1.17.4.3.1.2".arphextodec($arp);
+   my $research = "1.3.6.1.2.1.17.4.3.1.2".arp_hex_to_dec($arp);
 #print " $arp -> $research\n";
    LOOP_ON_ALL_SWITCH:
@@ -390,5 +390,5 @@
       }
 
-   my @res =();
+   my @res = ();
 
    for my $cidr ($cidrlist->list()) {
@@ -454,5 +454,5 @@
    my @computer = @_;
    
-   initswitchnames();    #nomme les switchs
+   init_switch_names();    #nomme les switchs
    fastping(@computer);
    for my $clientname (@computer) {
@@ -507,5 +507,5 @@
    my $detected_computer = 0;
    
-   initswitchnames('yes');    #nomme les switchs
+   init_switch_names('yes');    #nomme les switchs
 
    my %router_mac_ip = ();
@@ -741,7 +741,7 @@
 
 
-sub cmd_switchmap {
-
-   initswitchnames('yes');    #nomme les switchs
+sub cmd_updatesw {
+
+   init_switch_names('yes');    #nomme les switchs
    print "\n";
 
@@ -758,5 +758,4 @@
       }
 
-print "etape 1\n";
    ALL_ROUTER_IP_ADDRESS:
    for my $ip (Net::Netmask::sort_by_ip_address('194.254.66.254')) {
@@ -774,5 +773,4 @@
          }
       }   
-print "etape 2\n";
 
 #   print "Switch output port\n";  
@@ -792,5 +790,4 @@
       }
 
-print "etape 3\n";
    ALL_SWITCH:
    for my $one_computer (@list_switch_ip) {
@@ -805,5 +802,4 @@
       }
       
-print "etape 4\n";
    ALL_SWITCH_IP_ADDRESS:
    for my $ip (Net::Netmask::sort_by_ip_address(@list_switch_ipv4)) {
@@ -828,5 +824,4 @@
       }
    
-print "etape 5\n";
    my %db_switch_connected_on_port = ();
    my $maybe_more_than_one_switch_connected = 'yes';
@@ -874,5 +869,4 @@
       }
 
-print "etape 6\n";
    my %db_switch_parent =();
 
@@ -925,5 +919,60 @@
    }
 
-sub cmd_swdot {
+sub cmd_exportsw {
+   my @ARGV   = @_;
+
+   my $format = 'txt';
+   use Getopt::Long;
+
+   my $ret = GetOptions(
+      'format|f=s'  => \$format,
+      );
+
+   my %possible_format = (
+      txt => \&cmd_exportsw_txt,
+      dot => \&cmd_exportsw_dot,
+      );
+
+   $format = 'txt' if not defined $possible_format{$format};
+   
+   $possible_format{$format}->(@ARGV);
+   }
+
+sub cmd_exportsw_txt {
+
+   my $switch_connection = YAML::LoadFile("/var/cache/klask/switchdb");
+
+   my %db_switch_output_port       = %{$switch_connection->{output_port}};
+   my %db_switch_parent            = %{$switch_connection->{parent}};
+   my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}};
+
+   print "Switch output port and parent port connection\n";  
+   print "---------------------------------------------\n";
+   for my $sw (sort keys %db_switch_output_port) {
+      if (exists $db_switch_parent{$sw}) {
+         printf "%-25s  %2s  +-->  %2s  %-25s\n", $sw, $db_switch_output_port{$sw}, $db_switch_parent{$sw}->{port}, $db_switch_parent{$sw}->{switch};
+         }
+      else {
+         printf "%-25s  %2s  +-->  router\n", $sw, $db_switch_output_port{$sw};
+         }
+      }
+   print "\n";
+
+   print "Switch parent and children port inter-connection\n";
+   print "------------------------------------------------\n";
+   for my $swport (sort keys %db_switch_connected_on_port) {        
+      my ($sw_connect,$port_connect) = split ':', $swport;
+      for my $sw (keys %{$db_switch_connected_on_port{$swport}}) {
+         if (exists $db_switch_output_port{$sw}) {
+            printf "%-25s  %2s  <--+  %2s  %-25s\n", $sw_connect, $port_connect, $db_switch_output_port{$sw}, $sw;
+            }
+         else {
+            printf "%-25s  %2s  <--+      %-25s\n", $sw_connect, $port_connect, $sw;
+            }
+         }
+      }
+   }
+
+sub cmd_exportsw_dot {
 
    my $switch_connection = YAML::LoadFile("/var/cache/klask/switchdb");
@@ -945,8 +994,6 @@
    print "digraph G {\n";
 
-#print "size = \"11.4,8.2!\"; dpi = 600; orientation = landscape;\n";
-
-print "site [label = \"site\", color = black, fillcolor = gold, shape = invhouse, style = filled];\n";
-print "internet [label = \"internet\", color = black, fillcolor = cyan, shape = house, style = filled];\n";
+   print "site [label = \"site\", color = black, fillcolor = gold, shape = invhouse, style = filled];\n";
+   print "internet [label = \"internet\", color = black, fillcolor = cyan, shape = house, style = filled];\n";
 
    my $b=0;
@@ -954,13 +1001,6 @@
       $b++;
       
-#      print <<ENDB;
-#   subgraph "cluster_$b" {
-#      label = "$building";
-#      style = filled;
-#      color = yellow;
-#ENDB
-
-print "\"building$b\" [label = \"$building\", color = black, fillcolor = gold, style = filled];\n";
-print "site -> \"building$b\" [len = 2, color = firebrick];\n";
+      print "\"building$b\" [label = \"$building\", color = black, fillcolor = gold, style = filled];\n";
+      print "site -> \"building$b\" [len = 2, color = firebrick];\n";
 
       my $l = 0;
@@ -968,28 +1008,14 @@
          $l++;
  
-#         print <<ENDL;
-#      subgraph "cluster_$b-$l" {
-#         label = "$building / $loc";
-#         style = filled;
-#         color = purple;
-#ENDL
-
-print "\"location$b-$l\" [label = \"$building / $loc\", color = black, fillcolor = orange, style = filled];\n";
-print "\"building$b\" -> \"location$b-$l\" [len = 2, color = firebrick]\n";
+         print "\"location$b-$l\" [label = \"$building / $loc\", color = black, fillcolor = orange, style = filled];\n";
+         print "\"building$b\" -> \"location$b-$l\" [len = 2, color = firebrick]\n";
 
          for my $sw (keys %{$db_building{$building}->{$loc}}) {
 
-#            print <<ENDS;
-#         subgraph "cluster_$sw" {
-#            label = "$sw";
-#            style = filled;
-#            color = hot_pink;
-#ENDS
-# "sw-batB-legi:24" [label = 24, color = white,  peripheries = 3, style = filled];
             print "\"$sw:$db_switch_output_port{$sw}\" [label = $db_switch_output_port{$sw}, color = black, fillcolor = lightblue,  peripheries = 2, style = filled];\n";
 
-print "\"$sw\" [label = \"$sw\", color = black, fillcolor = palegreen, shape = rect, style = filled];\n";
-print "\"location$b-$l\" -> \"$sw\" [len = 2, color = firebrick, arrowtail = dot]\n";
-print "\"$sw\" -> \"$sw:$db_switch_output_port{$sw}\" [len=2, style=bold, arrowhead = normal, arrowtail = invdot]\n";
+            print "\"$sw\" [label = \"$sw\", color = black, fillcolor = palegreen, shape = rect, style = filled];\n";
+            print "\"location$b-$l\" -> \"$sw\" [len = 2, color = firebrick, arrowtail = dot]\n";
+            print "\"$sw\" -> \"$sw:$db_switch_output_port{$sw}\" [len=2, style=bold, arrowhead = normal, arrowtail = invdot]\n";
 
 
@@ -999,23 +1025,8 @@
                next if $port_connect eq $db_switch_output_port{$sw};
                print "\"$sw:$port_connect\" [label = $port_connect, color = black, fillcolor = plum,  peripheries = 1, style = filled];\n";
-print "\"$sw:$port_connect\" -> \"$sw\" [len=2, style=bold, arrowhead= normal, arrowtail = inv]\n";
+               print "\"$sw:$port_connect\" -> \"$sw\" [len=2, style=bold, arrowhead= normal, arrowtail = inv]\n";
               }
-
-#            print <<ENDS;
-#            }
-#ENDS
-
-            }
-
-#         print <<ENDL;
-#         }
-#ENDL
-        
-         }
-
-#      print <<ENDB;
-#      }
-#ENDB
-
+            }
+         }
       }
 
