Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use DBI;
- use Net::Telnet;
- #use strict;
- use SNMP 1.8;
- use Cwd 'abs_path';
- $SNMP::use_sprint_value = 1;
- my $filename = "/tmp/getmac.log";
- my $filename_dlink = "/tmp/getmac_dlink.log";
- my $dataserver='172.16.112.12';
- my $office_id = 1;
- my %switchtable;
- my @exclude_ports;
- my %dbtable;
- my $dbh;
- my $sth;
- my $sth2;
- my $sth3;
- my $sth4;
- my @row;
- my $switchfile='/tmp/getmac.ok';
- my $switchfile_dlink='/tmp/getmac_dlink.ok';
- my $param_ip ="@ARGV";
- my $switch_ip;
- my $switch_type;
- my $system_cmd;
- #full path of script
- my $src_path = abs_path($0);
- my $error_type;
- #how many telnet procces will load script
- my $tp=10;
- my $logging = 0 ;
- my $session = new Net::Telnet ( -telnetmode => 1,
- -timeout => 5,
- -cmd_remove_mode => 1,
- -output_record_separator => "\n");
- $dbh = DBI->connect("dbi:Pg:dbname=clients;host='".$dataserver."'", "postgres", "",
- { RaiseError => 1, AutoCommit => 0 });
- $where_arp = "";
- $where_con = "";
- if ($param_ip ne "") #Neo2SHYAlien's mod
- {
- #multi ip telnet in 1 PID
- #put all ip in massive
- @vals=split(/ /,$param_ip);
- for ($n=0;$n<=$#vals;$n++){
- if($n eq 0){
- $or=" and ";
- }else{
- $or=" or ";
- }
- $where_arp = "$where_arp $or (switch_ip = '$vals[$n]') ";
- $where_con = "$where_con $or (ipaddr = '$vals[$n]') ";
- }
- #print "$where_arp \n $where_con";
- }else{
- $sth = $dbh->prepare("select ipaddr from conpoints where active and (office_id = $office_id ) and (conpointtype = 0) $where_con ORDER BY last_check DESC");
- $sth->execute() or die $sth->errstr;
- if($sth->rows <= $tp ){
- #lower or eq limited devices
- print "Start executing...\n";
- while(@row = $sth->fetchrow_array){
- my $pid = fork();
- if ($pid) {
- # parent
- #print "pid is $pid, parent $$\n";
- } elsif ($pid == 0) {
- # child
- exec("perl $src_path $row[0]");
- exit 0;
- #} else {
- die "couldnt fork: $!\n";
- }
- }
- $sth->finish;
- $dbh->disconnect;
- }else{
- $dbh->{AutoCommit} = 1;
- #more than limited devices
- print "Calibrate for ".$sth->rows." devices...\n";
- my $rest = $sth->rows % $tp;
- my $loops = $sth->rows / $tp;
- for($i=1;$i<=$tp;$i++){
- $exec_arg='';
- for($n=1;$n<=$loops;$n++){
- @row = $sth->fetchrow_array;
- $exec_arg="$exec_arg $row[0]";
- }
- if($i eq $tp){
- for($m=1;$m<=$rest;$m++){
- @row = $sth->fetchrow_array;
- $exec_arg="$exec_arg $row[0]";
- }
- }
- my $pid = fork();
- if ($pid) {
- # parent
- #print "pid is $pid, parent $$\n";
- }elsif ($pid == 0) {
- # child
- #print "$i $exec_arg\n";
- exec("perl $src_path $exec_arg");
- exit 0;
- } else {
- die "couldnt fork: $!\n";
- }
- }
- }
- exit;
- } #end Neo2SHYAlien's mod
- #print $where_arp." ".$where_con."\n";
- #$where_arp
- $sth = $dbh->prepare("select upper(macadr::text),switch_ip,vlan_id,switch_port from arp_perm where (office_id = $office_id) ");
- $sth->execute() or die $sth->errstr;
- while (@row = $sth->fetchrow_array) {
- %dbtable->{$row[0]} = $row[1]." ".$row[2]." ".$row[3];
- }
- $sth->finish;
- $sth3 = $dbh->prepare("select manage_type,ipaddr,man_user,man_pass,manage_params,exclude_ports from conpoints where active and (office_id = $office_id ) and (conpointtype = 0) $where_con ORDER BY last_check DESC");
- # 0 1 2 3 4 5
- $sth3->execute() or die $sth3->errstr;
- while (@row = $sth3->fetchrow_array)
- {
- $switch_ip = $row[1];
- $switch_type = $row[0];
- print "Switch $switch_ip \n";
- #redeclare file names
- $filename="/tmp/getmac.log.$row[1]";
- $switchfile="/tmp/getmac.ok.$row[1]";
- if (-e $filename){ #removing old files
- print "Removing old files...\n";
- unlink $filename;
- unlink $switchfile;
- }
- if ( $switch_type eq "2")
- {
- TelnetRuby($row[1], $row[2], $row[3], $row[4]);
- $system_cmd = "cat ".$filename." | grep Dynamic | grep --invert-match ' 9 ' | grep --invert-match ' 10 ' ";
- @exclude_ports = split (/,/, $row[5]);
- foreach my $ex_port (@exclude_ports)
- {
- # $system_cmd = $system_cmd . "| grep --invert-match ' ".$ex_port." ' ";
- }
- $system_cmd = $system_cmd ." > ".$switchfile;
- system $system_cmd;
- #Get current macs
- %switchtable = &CurrentMACs($switchfile, $switch_ip);
- } elsif ( $switch_type eq "3")
- {
- #telnet Dlink
- TelnetDLink($row[1], $row[2], $row[3], $row[4]);
- $system_cmd = "cat ".$filename_dlink." | grep Dynamic ";
- @exclude_ports = split (/,/, $row[5]);
- foreach my $ex_port (@exclude_ports)
- {
- $system_cmd = $system_cmd . "| grep --invert-match ' ".$ex_port." ' ";
- }
- $system_cmd = $system_cmd ." > ".$switchfile_dlink;
- system $system_cmd;
- #Get current macs
- %switchtable = &CurrentMACsDL($switchfile_dlink, $switch_ip);
- } elsif ( $switch_type eq "4")
- {
- #telnet NetGear
- TelnetNetGear($row[1], $row[2], $row[3], $row[4]);
- $system_cmd = "cat ".$filename." | grep Learned ";
- @exclude_ports = split (/,/, $row[5]);
- foreach my $ex_port (@exclude_ports)
- {
- $system_cmd = $system_cmd . "| grep --invert-match ' ".$ex_port." ' ";
- }
- $system_cmd = $system_cmd ." > ".$switchfile;
- system $system_cmd;
- #Get current macs
- %switchtable = &CurrentMACsNG($switchfile, $switch_ip);
- } elsif ( $switch_type eq "5")
- {
- #telnet Zyxel
- TelnetZyxel($row[1], $row[2], $row[3], $row[4]);
- $system_cmd = "cat ".$filename." | grep Learned ";
- @exclude_ports = split (/,/, $row[5]);
- foreach my $ex_port (@exclude_ports)
- {
- $system_cmd = $system_cmd . "| grep --invert-match '".$ex_port." ' ";
- }
- $system_cmd = $system_cmd ." > ".$switchfile;
- system $system_cmd;
- #Get current macs
- %switchtable = &CurrentMACsZy($switchfile, $switch_ip);
- } elsif ( $switch_type eq "1")
- {
- %switchtable = &SnmpCurrentMACs($switch_ip, $row[3]);
- #SnmpCurrentMACs2($switch_ip, $row[3]);
- }
- foreach my $key (keys %switchtable)
- {
- if ( defined($dbtable{$key}) )
- {
- #print "key = $key data = $dbtable{$key}\n";
- if ( $dbtable{$key} ne $switchtable{$key} )
- {
- my ($switch_ip, $vlan, $port) = split (/\s/, $switchtable{$key});
- #print "update arp_perm set switch_ip = $switch_ip, vlan_id = $vlan, switch_port = $port where (macadr = $key) \n";
- $sth2 = $dbh->prepare("select * from new_mac_upd (?,'$switch_ip'::inet,?,?,?)");
- $sth2->execute($key, $vlan, $port, $office_id);
- $dbh->commit();
- $sth2->finish;
- if ( trim($dbtable{$key}) ne "" )
- {
- #print "here\n";
- $sth2 = $dbh->prepare("insert into arp_change (macadr, switch_ip, old_port, new_port, office_id) VALUES (?, ? , ?, ?, ?) ");
- $sth2->execute($key, $switch_ip, $dbtable{$key},$switchtable{$key}, $office_id);
- $dbh->commit();
- $sth2->finish;
- }
- }
- }
- else
- {
- # $sth2 = $dbh->prepare("insert into arp_perm (macadr, switch_ip, vlan_id , switch_port , office_id ,username , host, ipaddr) VALUES ( ?,?,?,?,?,'unknown','unknown','0.0.0.0') ");
- # $sth2->execute($key, $switch_ip, $vlan, $port , $office_id);
- my ($switch_ip, $vlan, $port) = split (/\s/, $switchtable{$key});
- #print "select * from new_mac_ins ('$key','$switch_ip'::inet,$vlan,'$port',$office_id); \n" ;
- $sth2 = $dbh->prepare("select * from new_mac_ins (?,'$switch_ip'::inet,?,?,?) ");
- $sth2->execute($key, $vlan, $port , $office_id);
- $dbh->commit();
- $sth2->finish;
- }
- }
- #update last_check
- $dbh->do(q{update conpoints set last_check=now() where ipaddr=? }, undef,$switch_ip);
- $dbh->commit();
- }
- $sth3->finish;
- $dbh->disconnect or warn $dbh->errstr;
- sub TelnetRuby
- {
- my $ok;
- my @lines;
- my $line;
- my $fh;
- my $login_error;
- my ($switch_ip,$login_user,$login_pass, $prompt_sym) = @_;
- $session->errmode("return");
- $ok = $session->open($switch_ip);
- #$ok = $session->open(Host => $switch_ip,Port => 23,Errmode => "return",Timeout => 10,);
- if ($session->timed_out() || ($ok ne 1) )
- {
- print "No connection_1. $switch_ip\n";
- #$error_type=1;
- #&error_loging();
- $session->print("logout");
- sleep 2;
- $session->close;
- return;
- }
- $ok = $session->waitfor( Match => "/Managed Switch/",
- Errmode => "return",
- Timeout => 3);
- if ($session->timed_out() || ($ok ne 1) )
- {
- print "No connection_3. $switch_ip\n";
- #$error_type=3;
- #&error_loging();
- $session->print("logout");
- sleep 2;
- $session->close;
- return;
- }
- my $error=$session->login($login_user,$login_pass);
- if ($error ne 1 ){
- print "Error username or password\n";
- #$session->close;
- #$error_type=4;
- #error_loging();
- return;
- }else{
- #check for admin rights
- @chk_line=$session->cmd('restore');
- @inv=split(/ /, @chk_line[0]);
- if($inv[0] eq 'Invalid'){ #if is returned Invalid command restore have anybody connected
- print("Login with non admin acc\n");
- #$error_type=5;
- $session->print("logout");
- #&error_loging();
- $session->close;
- return;
- }
- $session->waitfor("/".$prompt_sym."/");
- $session->print("mac-table");
- $session->waitfor("/".$prompt_sym."/");
- $session->print("");
- $session->print("information");
- $session->waitfor("/".$prompt_sym."/");
- $session->print(" ");
- $session->waitfor("/".$prompt_sym."/");
- sleep 1;
- $line = $session->lastline;
- $session->print(" ");
- $session->waitfor("/".$prompt_sym."/");
- $line = $session->lastline;
- $session->print(" ");
- $session->waitfor("/".$prompt_sym."/");
- $line = $session->lastline;
- $session->print(" ");
- $session->waitfor("/".$prompt_sym."/");
- $line = $session->lastline;
- $session->print(" ");
- $session->waitfor("/".$prompt_sym."/");
- $line = $session->lastline;
- $session->print(" ");
- $session->waitfor("/".$prompt_sym."/");
- $line = $session->lastline;
- $fh = $session->input_log($filename);
- $session->print("show");
- $ok = 0;
- do {
- $session->waitfor(Match => "/(Q to quit)/",Timeout => 5);
- $session->print(" ");
- @lines = $session->getlines(All => 0);
- foreach $msg ( @lines ) { chomp($msg);
- if ($msg=~ m/#/) {$ok=1;} }
- } until ($session->timed_out() || ( $ok eq 1 ));
- $fh = $session->input_log("");
- $session->print("logout");
- sleep 2;
- $session->close;
- }
- }
- sub TelnetNetGear
- {
- my $ok;
- my @lines;
- my $line;
- my $fh;
- my ($switch_ip,$login_user,$login_pass, $prompt_sym) = @_;
- $session->errmode("return");
- $ok = $session->open($switch_ip);
- if ($session->timed_out() || ($ok ne 1) )
- {
- print "No connection_1 .\n";
- $session->print("logout");
- sleep 2;
- $session->close;
- return;
- }
- $session->waitfor("/User:/");
- if ($session->timed_out() )
- {
- print "No connection_2 .\n";
- $session->print("logout");
- sleep 2;
- $session->close;
- return;
- }
- $session->print($login_user);
- $session->waitfor("/Password:/");
- $session->print($login_pass);
- $session->waitfor("/>/");
- $session->print("ena");
- $session->waitfor("/Password:/");
- $session->print("");
- $session->waitfor("/".$prompt_sym."/");
- $fh = $session->input_log($filename);
- $session->print("show mac-addr-table");
- $session->waitfor("/".$prompt_sym."/");
- $ok = 0;
- do {
- $session->waitfor(Match => "--More-- or (q)uit",Timeout => 2);
- $session->print(" ");
- @lines = $session->getlines(All => 0);
- foreach $msg ( @lines ) { chomp($msg); if ($msg=~ m/#/) {$ok=1;} }
- } until ($session->timed_out() || ( $ok eq 1 ));
- $fh = $session->input_log("");
- $session->print("logout");
- sleep 2;
- $session->close;
- }
- sub TelnetDLink
- {
- my $ok;
- my @lines;
- my $line;
- my $fh;
- my ($switch_ip,$login_user,$login_pass, $prompt_sym) = @_;
- $session->errmode("return");
- $ok = $session->open($switch_ip);
- if ($session->timed_out() || ($ok ne 1) )
- {
- print "No connection_1 .\n";
- $session->print("logout");
- sleep 2;
- $session->close;
- return;
- }
- $session->waitfor("/ame:/");
- if ($session->timed_out() )
- {
- print "No connection_2 .\n";
- $session->print("logout");
- sleep 2;
- $session->close;
- return;
- }
- $session->print($login_user);
- $session->waitfor("/ord:/");
- $session->print($login_pass);
- $session->waitfor("/".$prompt_sym."/");
- sleep 1;
- $session->print(" ");
- $session->waitfor("/".$prompt_sym."/");
- $line = $session->lastline;
- $session->print(" ");
- $session->waitfor("/".$prompt_sym."/");
- $line = $session->lastline;
- $session->print(" ");
- $session->waitfor("/".$prompt_sym."/");
- $line = $session->lastline;
- $session->print(" ");
- $session->waitfor("/".$prompt_sym."/");
- $line = $session->lastline;
- $fh = $session->input_log($filename_dlink);
- $session->print("show fdb");
- $ok = 0;
- do {
- $session->waitfor(Match => "/Next/",Timeout => 1);
- $session->print("n");
- @lines = $session->getlines(All => 0);
- foreach $msg ( @lines ) { chomp($msg); if ($msg=~ m/Total/) {$ok=1;} }
- } until ($session->timed_out() || ( $ok eq 1 ));
- $fh = $session->input_log("");
- $session->print("logout");
- sleep 2;
- $session->close;
- }
- sub TelnetZyxel
- {
- my $ok;
- my @lines;
- my $line;
- my $fh;
- my ($switch_ip,$login_user,$login_pass, $prompt_sym) = @_;
- $session->errmode("return");
- $ok = $session->open($switch_ip);
- if ($session->timed_out() || ($ok ne 1) )
- {
- print "No connection_1 .\n";
- $session->print("logout");
- sleep 2;
- $session->close;
- return;
- }
- $session->waitfor("/User name:/");
- if ($session->timed_out() )
- {
- print "No connection_2 .\n";
- $session->print("logout");
- sleep 2;
- $session->close;
- return;
- }
- $session->print($login_user);
- $session->waitfor("/Password:/");
- $session->print($login_pass);
- $session->waitfor("/".$prompt_sym."/");
- $fh = $session->input_log($filename);
- $session->print("show mac address-table all");
- $session->waitfor("/".$prompt_sym."/");
- $ok = 0;
- do {
- @lines = $session->getlines(All => 0);
- foreach $msg ( @lines ) { chomp($msg); if ($msg=~ m/#/) {$ok=1;} }
- } until ($session->timed_out() || ( $ok eq 1 ));
- $fh = $session->input_log("");
- $session->print("logout");
- sleep 2;
- $session->close;
- }
- sub SnmpCurrentMACs2
- {
- my ($switch_ip,$comm) = @_;
- use Net::SNMP;
- #print $switch_ip. " " .$comm."\n";
- # requires a hostname and a community string as its arguments
- ($se,$error) = Net::SNMP->session(Hostname => $switch_ip,
- Community => $comm);
- die "session error: $error" unless ($se);
- # iso.org.dod.internet.mgmt.mib-2.interfaces.ifNumber.0 =
- # 1.3.6.1.2.1.2.1.0
- $result = $se->get_request(".2.17.4.3.1.2.0.8.199.187.207.220");
- die "request error: ".$se->error unless (defined $result);
- $se->close;
- #print $result->{".2.17.4.3.1.2.0.8.199.187.207.220"}."\n";
- }
- sub SnmpCurrentMACs
- {
- my ($switch_ip,$comm) = @_;
- my $sess = new SNMP::Session(DestHost => $switch_ip, Community => $comm);
- # my $var = new SNMP::Varbind([ipNetToMediaPhysAddress]);
- my $var = new SNMP::Varbind(["SNMPv2-SMI::mib-2"]);
- my %newtable;
- my $val;
- my $vlan;
- my $ip;
- my $ip1;
- my $ip2;
- my $ip3;
- my $ip4;
- my $m1;
- my $m2;
- my $m3;
- my $m4;
- my $m5;
- my $m6;
- my $mac_addr;
- do {
- $val = $sess->getnext($var);
- #print $var->[$SNMP::Varbind::tag_f]." - ".$var->[$SNMP::Varbind::iid_f]."\n";
- if ("$var->[$SNMP::Varbind::tag_f].$var->[$SNMP::Varbind::iid_f]" =~ m/ipNetToMediaPhysAddress/)
- {
- ($vlan,$ip1,$ip2,$ip3,$ip4) = split (/\./ , "$var->[$SNMP::Varbind::iid_f]\n");
- $ip = $ip1.".".$ip2.".".$ip3.".".$ip4;
- chomp($ip);
- ($m1,$m2,$m3,$m4,$m5,$m6) = split (/:/ , "$var->[$SNMP::Varbind::val_f]");
- $mac_addr = sprintf("%02s:%02s:%02s:%02s:%02s:%02s", $m1, $m2, $m3, $m4, $m5, $m6 );
- $newtable{$ip} = $mac_addr;
- #print $ip," ",$mac_addr,"\n";
- }
- } until ($sess->{ErrorStr});
- return %newtable;
- }
- sub CurrentMACs
- {
- my %newtable;
- my $mac;
- my $port;
- my $vlan;
- my $tmp;
- my $rec;
- my ($filename,$switch_ip) = @_;
- open(FILE, "<$filename") or die "Error 1 \n";
- while (<FILE>)
- {
- $tmp = trim($_);
- ($mac,$port,$vlan,$tmp) = split (/\s+/, $tmp);
- $mac =~ s/-+/:/g;
- upcase_in($mac);
- $rec = "$switch_ip $vlan $port";
- $newtable{$mac} = $rec;
- }
- close(FILE);
- return %newtable;
- }
- sub CurrentMACsDL
- {
- my %newtable;
- my $mac;
- my $port;
- my $vlan;
- my $tmp;
- my $rec;
- my ($filename,$switch_ip) = @_;
- open(FILE, "<$filename") or die "Error 1 \n";
- while (<FILE>)
- {
- $tmp = trim($_);
- ($vlan,$tmp,$mac,$port) = split (/\s+/, $tmp);
- $mac =~ s/-+/:/g;
- upcase_in($mac);
- $rec = "$switch_ip $vlan $port";
- #print $rec."-".$mac."\n";
- $newtable{$mac} = $rec;
- }
- close(FILE);
- return %newtable;
- }
- sub CurrentMACsNG
- {
- my %newtable;
- my $mac;
- my $port;
- my $vlan;
- my $tmp;
- my $rec;
- my $vlanmac;
- my ($filename,$switch_ip) = @_;
- open(FILE, "<$filename") or die "Error 1 \n";
- while (<FILE>)
- {
- $tmp = trim($_);
- ($mac,$tmp,$port,$vlan) = split (/\s+/, $tmp);
- # $vlan = hex(substr($vlanmac,0,1)) * 256 + hex(substr($vlanmac,3,4));
- # $mac = substr($vlanmac,6,22);
- $mac =~ s/-+/:/g;
- upcase_in($mac);
- $rec = "$switch_ip $vlan $port";
- #print $rec."-".$mac."\n";
- $newtable{$mac} = $rec;
- }
- close(FILE);
- return %newtable;
- }
- sub CurrentMACsZy
- {
- my %newtable;
- my $mac;
- my $port;
- my $vlan;
- my $tmp;
- my $rec;
- my $vlanmac;
- my ($filename,$switch_ip) = @_;
- open(FILE, "<$filename") or die "Error 1 \n";
- while (<FILE>)
- {
- $tmp = trim($_);
- ($port,$vlan,$mac,$tmp) = split (/\s+/, $tmp);
- $mac =~ s/-+/:/g;
- upcase_in($mac);
- $rec = "$switch_ip $vlan $port";
- #print $rec."-".$mac."\n";
- $newtable{$mac} = $rec;
- }
- close(FILE);
- return %newtable;
- }
- sub upcase_in { for (@_) { tr/a-z/A-Z/ } }
- sub trim
- {
- my @out = @_;
- for (@out)
- {
- s/^\s+//;
- s/\s+$//;
- }
- return wantarray ? @out : $out[0];
- }
- sub error_loging(){
- if ( $logging > 1)
- {
- print "\n Saving error.... \n";
- $sth4 = $dbh->prepare("INSERT INTO errors(ip, \"type\", date) VALUES ('$switch_ip', $error_type, now());");
- $sth4->execute() or die $sth4->errstr;
- $sth4->finish;
- }
- }
Add Comment
Please, Sign In to add comment