Guest User

Untitled

a guest
Jan 23rd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.92 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use threads;
  4. use strict;
  5. use Switch;
  6. use Net::SNMP;
  7. use Thread::Queue;
  8. use DBI;
  9.  
  10. my $b_ip : shared;
  11. my $b_oid : shared;
  12. my @mas_ip : shared;
  13. my @mas_id_mod : shared;
  14. my @users=0..31;
  15. my @thread;
  16. my @globx : shared;
  17. my $l : shared;
  18.  
  19. my $dbh = DBI->connect("DBI:Pg:dbname=postgres;host=10.240.225.6",
  20. "work", "work") || die $DBI::errstr;
  21.  
  22. my $query = "SELECT switch_map.switch_place2.ip_address, switch_map.models.id
  23. FROM switch_map.switch_place2, switch_map.sw_warehouse, switch_map.models
  24. WHERE (switch_map.switch_place2.sw_id = switch_map.sw_warehouse.id) AND (switch_map.sw_warehouse.model_id = switch_map.models.id) AND
  25. (switch_map.models.id IN (1,2,3,7,15));";
  26.  
  27. my $output = $dbh->prepare($query);
  28. $output->execute;
  29. my ($b_ip, $id_mod);
  30. $output->bind_columns(undef, \($b_ip, $id_mod));
  31.  
  32. while ($output->fetch) {
  33.     push @mas_ip, $b_ip;
  34.     push @mas_id_mod, $id_mod;
  35. }
  36.  
  37. for (my $j=0; $j<$#mas_ip; $j++){
  38. =head
  39.     switch($mas_id_mod[$j]){
  40.         case 1 {$b_oid = "1.3.6.1.4.1.171.11.64.1.2.10.5.1.4";}
  41.         case 2 {$b_oid = "1.3.6.1.4.1.171.11.63.6.2.7.11.1.4";}
  42.         case 3 {$b_oid = "1.3.6.1.4.1.171.11.63.3.2.7.5.1.4";}
  43.         case 7 {$b_oid = "1.3.6.1.4.1.171.11.63.6.2.7.11.1.4";}
  44.         case 15 {$b_oid = "1.3.6.1.4.1.171.12.73.3.1.3.1.2";}
  45.     }
  46.     $b_ip = $mas_ip[$j];
  47.     threads->create(\&kar, $b_ip, $b_oid);
  48. =cut
  49.     foreach(@users) {
  50.         if ($j<$#mas_ip){
  51.             switch($mas_id_mod[$j]){
  52.                 case 1 {$b_oid = "1.3.6.1.4.1.171.11.64.1.2.10.5.1.4";}
  53.                 case 2 {$b_oid = "1.3.6.1.4.1.171.11.63.6.2.7.11.1.4";}
  54.                 case 3 {$b_oid = "1.3.6.1.4.1.171.11.63.3.2.7.5.1.4";}
  55.                 case 7 {$b_oid = "1.3.6.1.4.1.171.11.63.6.2.7.11.1.4";}
  56.                 case 15 {$b_oid = "1.3.6.1.4.1.171.12.73.3.1.3.1.2";}
  57.             }
  58.             $b_ip = $mas_ip[$j];
  59.             $globx[$_] = Thread::Queue->new;
  60.             $thread[$_] = threads->create(\&kar, $b_ip, $b_oid)->join;
  61.             $j++; $l++;
  62.         }
  63.     }
  64.  
  65.     foreach (@users){
  66.         if ($j<$#mas_ip){
  67.             switch($mas_id_mod[$j]){
  68.                 case 1 {$b_oid = "1.3.6.1.4.1.171.11.64.1.2.10.5.1.4";}
  69.                 case 2 {$b_oid = "1.3.6.1.4.1.171.11.63.6.2.7.11.1.4";}
  70.                 case 3 {$b_oid = "1.3.6.1.4.1.171.11.63.3.2.7.5.1.4";}
  71.                 case 7 {$b_oid = "1.3.6.1.4.1.171.11.63.6.2.7.11.1.4";}
  72.                 case 15 {$b_oid = "1.3.6.1.4.1.171.12.73.3.1.3.1.2";}
  73.             }
  74.             $b_ip = $mas_ip[$j];
  75.             if ($globx[$_]->pending eq 0) {
  76.                 $globx[$_]->enqueue(-1);
  77.                 $thread[$_] = threads->create(\&kar, $b_ip, $b_oid)->join;
  78.                 $j++; $l++;
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. sub kar
  85. {
  86. my $community = 'public';
  87. my $oid_old = $b_oid;
  88. my @ent;
  89. my @ports;
  90. my %vlan;
  91. my $ip= $b_ip;
  92. my $r;
  93. my @erp;
  94. chomp $ip;
  95.  
  96.     my $snmp = Net::SNMP->session(hostname => $ip,
  97.                     community => $community,
  98.                     version => 2,
  99.                     timeout => 3
  100.                     ) or die('Не удалось соединиться!');
  101.  
  102.     my $oid = $oid_old;
  103.     output:while (my $res = $snmp->get_next_request($oid))
  104.     {
  105.         while (my ($oid_new, $val) = each(%$res))
  106.         {
  107.             if ($oid !~ /^$oid_old.*/)
  108.             {
  109.                 last output;
  110.             }
  111.             else
  112.             {
  113. #=head
  114.                 my %h2b = (0 => "0000", 1 => "0001", 2 => "0010", 3 => "0011",
  115.                 4 => "0100", 5 => "0101", 6 => "0110", 7 => "0111",
  116.                 8 => "1000", 9 => "1001", a => "1010", b => "1011",
  117.                 c => "1100", d => "1101", e => "1110", f => "1111");
  118.  
  119.                 (my $number = $val) =~ s/(.)/$h2b{lc $1}/g;
  120.                 my @num = split(/ */, $number);
  121.                 for (my $i=0; $i<=48; $i++){
  122.                     if ($num[$i]=="1")  {
  123.                         $r= $i-3;
  124.                         push @erp, $r}}
  125.                 push @ent, "$oid_new:@erp\n";
  126.                 $r=''; $#erp=(-1);
  127. #=cut
  128. =head
  129.                 $val = unpack("B*", $val);
  130.                 while($val =~m/(1)/g){
  131.                     push @erp, pos($val);
  132.                     my $i++;
  133.                 }
  134.                 push @ent, "$oid_new:@erp\n"; $#erp=(-1);
  135. =cut
  136.                 $oid = $oid_new;
  137.             }
  138.         }
  139.     }
  140.     foreach my $i (@ent)
  141.     {
  142.         $i =~ s/^$oid_old.\d+.//;
  143.         $i =~ s/\S+:$//;
  144.         $i =~ s/\.0\.0\.0\.0//i;
  145.         push @ports, $i if ((substr($i,0,3)eq'224')||(substr($i,0,3)eq'239'));
  146.     }
  147.     if (@ports!=''){
  148.     $vlan{"$ip"} = "@ports";
  149.     }
  150.     foreach my $k (keys %vlan)
  151.     {
  152.         print "($l)$k = $vlan{$k}\n";
  153.     }
  154. }
Add Comment
Please, Sign In to add comment