Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use threads;
- use strict;
- use Switch;
- use Net::SNMP;
- use Thread::Queue;
- use DBI;
- my $b_ip : shared;
- my $b_oid : shared;
- my @mas_ip : shared;
- my @mas_id_mod : shared;
- my @users=0..31;
- my @thread;
- my @globx : shared;
- my $l : shared;
- my $dbh = DBI->connect("DBI:Pg:dbname=postgres;host=10.240.225.6",
- "work", "work") || die $DBI::errstr;
- my $query = "SELECT switch_map.switch_place2.ip_address, switch_map.models.id
- FROM switch_map.switch_place2, switch_map.sw_warehouse, switch_map.models
- WHERE (switch_map.switch_place2.sw_id = switch_map.sw_warehouse.id) AND (switch_map.sw_warehouse.model_id = switch_map.models.id) AND
- (switch_map.models.id IN (1,2,3,7,15));";
- my $output = $dbh->prepare($query);
- $output->execute;
- my ($b_ip, $id_mod);
- $output->bind_columns(undef, \($b_ip, $id_mod));
- while ($output->fetch) {
- push @mas_ip, $b_ip;
- push @mas_id_mod, $id_mod;
- }
- for (my $j=0; $j<$#mas_ip; $j++){
- =head
- switch($mas_id_mod[$j]){
- case 1 {$b_oid = "1.3.6.1.4.1.171.11.64.1.2.10.5.1.4";}
- case 2 {$b_oid = "1.3.6.1.4.1.171.11.63.6.2.7.11.1.4";}
- case 3 {$b_oid = "1.3.6.1.4.1.171.11.63.3.2.7.5.1.4";}
- case 7 {$b_oid = "1.3.6.1.4.1.171.11.63.6.2.7.11.1.4";}
- case 15 {$b_oid = "1.3.6.1.4.1.171.12.73.3.1.3.1.2";}
- }
- $b_ip = $mas_ip[$j];
- threads->create(\&kar, $b_ip, $b_oid);
- =cut
- foreach(@users) {
- if ($j<$#mas_ip){
- switch($mas_id_mod[$j]){
- case 1 {$b_oid = "1.3.6.1.4.1.171.11.64.1.2.10.5.1.4";}
- case 2 {$b_oid = "1.3.6.1.4.1.171.11.63.6.2.7.11.1.4";}
- case 3 {$b_oid = "1.3.6.1.4.1.171.11.63.3.2.7.5.1.4";}
- case 7 {$b_oid = "1.3.6.1.4.1.171.11.63.6.2.7.11.1.4";}
- case 15 {$b_oid = "1.3.6.1.4.1.171.12.73.3.1.3.1.2";}
- }
- $b_ip = $mas_ip[$j];
- $globx[$_] = Thread::Queue->new;
- $thread[$_] = threads->create(\&kar, $b_ip, $b_oid)->join;
- $j++; $l++;
- }
- }
- foreach (@users){
- if ($j<$#mas_ip){
- switch($mas_id_mod[$j]){
- case 1 {$b_oid = "1.3.6.1.4.1.171.11.64.1.2.10.5.1.4";}
- case 2 {$b_oid = "1.3.6.1.4.1.171.11.63.6.2.7.11.1.4";}
- case 3 {$b_oid = "1.3.6.1.4.1.171.11.63.3.2.7.5.1.4";}
- case 7 {$b_oid = "1.3.6.1.4.1.171.11.63.6.2.7.11.1.4";}
- case 15 {$b_oid = "1.3.6.1.4.1.171.12.73.3.1.3.1.2";}
- }
- $b_ip = $mas_ip[$j];
- if ($globx[$_]->pending eq 0) {
- $globx[$_]->enqueue(-1);
- $thread[$_] = threads->create(\&kar, $b_ip, $b_oid)->join;
- $j++; $l++;
- }
- }
- }
- }
- sub kar
- {
- my $community = 'public';
- my $oid_old = $b_oid;
- my @ent;
- my @ports;
- my %vlan;
- my $ip= $b_ip;
- my $r;
- my @erp;
- chomp $ip;
- my $snmp = Net::SNMP->session(hostname => $ip,
- community => $community,
- version => 2,
- timeout => 3
- ) or die('Не удалось соединиться!');
- my $oid = $oid_old;
- output:while (my $res = $snmp->get_next_request($oid))
- {
- while (my ($oid_new, $val) = each(%$res))
- {
- if ($oid !~ /^$oid_old.*/)
- {
- last output;
- }
- else
- {
- #=head
- my %h2b = (0 => "0000", 1 => "0001", 2 => "0010", 3 => "0011",
- 4 => "0100", 5 => "0101", 6 => "0110", 7 => "0111",
- 8 => "1000", 9 => "1001", a => "1010", b => "1011",
- c => "1100", d => "1101", e => "1110", f => "1111");
- (my $number = $val) =~ s/(.)/$h2b{lc $1}/g;
- my @num = split(/ */, $number);
- for (my $i=0; $i<=48; $i++){
- if ($num[$i]=="1") {
- $r= $i-3;
- push @erp, $r}}
- push @ent, "$oid_new:@erp\n";
- $r=''; $#erp=(-1);
- #=cut
- =head
- $val = unpack("B*", $val);
- while($val =~m/(1)/g){
- push @erp, pos($val);
- my $i++;
- }
- push @ent, "$oid_new:@erp\n"; $#erp=(-1);
- =cut
- $oid = $oid_new;
- }
- }
- }
- foreach my $i (@ent)
- {
- $i =~ s/^$oid_old.\d+.//;
- $i =~ s/\S+:$//;
- $i =~ s/\.0\.0\.0\.0//i;
- push @ports, $i if ((substr($i,0,3)eq'224')||(substr($i,0,3)eq'239'));
- }
- if (@ports!=''){
- $vlan{"$ip"} = "@ports";
- }
- foreach my $k (keys %vlan)
- {
- print "($l)$k = $vlan{$k}\n";
- }
- }
Add Comment
Please, Sign In to add comment