Guest User

Untitled

a guest
Feb 21st, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 19.54 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use DBI;
  4. use Net::Telnet;
  5. #use strict;
  6. use SNMP 1.8;
  7. use Cwd 'abs_path';
  8.  
  9. $SNMP::use_sprint_value = 1;
  10.  
  11. my $filename = "/tmp/getmac.log";
  12. my $filename_dlink = "/tmp/getmac_dlink.log";
  13. my $dataserver='172.16.112.12';
  14. my $office_id = 1;
  15. my %switchtable;
  16. my @exclude_ports;
  17. my %dbtable;
  18. my $dbh;
  19. my $sth;
  20. my $sth2;
  21. my $sth3;
  22. my $sth4;
  23. my @row;
  24. my $switchfile='/tmp/getmac.ok';
  25. my $switchfile_dlink='/tmp/getmac_dlink.ok';
  26. my $param_ip ="@ARGV";
  27. my $switch_ip;
  28. my $switch_type;
  29. my $system_cmd;
  30. #full path of script
  31. my $src_path = abs_path($0);
  32. my $error_type;
  33. #how many telnet procces will load script
  34. my $tp=10;
  35. my $logging =  0 ;
  36. my $session = new Net::Telnet (  -telnetmode => 1,
  37.                                     -timeout => 5,
  38.                                     -cmd_remove_mode => 1,
  39.                                     -output_record_separator => "\n");
  40.  
  41. $dbh = DBI->connect("dbi:Pg:dbname=clients;host='".$dataserver."'", "postgres", "",
  42.                                      { RaiseError => 1, AutoCommit => 0 });    
  43. $where_arp = "";
  44. $where_con = "";
  45. if ($param_ip ne "") #Neo2SHYAlien's mod
  46. {
  47.     #multi ip telnet in 1 PID
  48.     #put all ip in massive
  49.     @vals=split(/ /,$param_ip);
  50.     for ($n=0;$n<=$#vals;$n++){
  51.         if($n eq 0){
  52.             $or=" and ";
  53.         }else{
  54.             $or=" or ";
  55.         }
  56.         $where_arp = "$where_arp $or (switch_ip = '$vals[$n]') ";
  57.         $where_con = "$where_con $or (ipaddr = '$vals[$n]') ";
  58.     }
  59.     #print "$where_arp \n $where_con";
  60. }else{
  61.     $sth = $dbh->prepare("select ipaddr from conpoints where active and (office_id = $office_id ) and (conpointtype = 0) $where_con ORDER BY last_check DESC");
  62.     $sth->execute() or die $sth->errstr;
  63.        
  64.     if($sth->rows <= $tp ){
  65.         #lower or eq limited devices
  66.         print "Start executing...\n";
  67.         while(@row = $sth->fetchrow_array){
  68.             my $pid = fork();
  69.             if ($pid) {
  70.                 # parent
  71.                 #print "pid is $pid, parent $$\n";
  72.             } elsif ($pid == 0) {
  73.                 # child
  74.                 exec("perl $src_path $row[0]");
  75.                 exit 0;
  76.             #} else {
  77.                 die "couldnt fork: $!\n";
  78.             }
  79.         }
  80.    
  81.     $sth->finish;
  82.     $dbh->disconnect;
  83.    
  84.     }else{
  85.         $dbh->{AutoCommit} = 1;
  86.         #more than limited devices
  87.         print "Calibrate for ".$sth->rows." devices...\n";
  88.         my $rest = $sth->rows % $tp;
  89.         my $loops = $sth->rows / $tp;
  90.         for($i=1;$i<=$tp;$i++){
  91.             $exec_arg='';
  92.             for($n=1;$n<=$loops;$n++){
  93.                 @row = $sth->fetchrow_array;
  94.                 $exec_arg="$exec_arg $row[0]";
  95.             }
  96.             if($i eq $tp){
  97.                 for($m=1;$m<=$rest;$m++){
  98.                     @row = $sth->fetchrow_array;
  99.                     $exec_arg="$exec_arg $row[0]";
  100.                 }
  101.             }
  102.  
  103.             my $pid = fork();
  104.             if ($pid) {
  105.                 # parent
  106.                 #print "pid is $pid, parent $$\n";
  107.             }elsif ($pid == 0) {
  108.                 # child
  109.                 #print "$i $exec_arg\n";
  110.                 exec("perl $src_path $exec_arg");
  111.                 exit 0;
  112.             } else {
  113.                 die "couldnt fork: $!\n";
  114.             }
  115.         }
  116.     }
  117.     exit;
  118. } #end Neo2SHYAlien's mod
  119.  
  120. #print $where_arp." ".$where_con."\n";
  121. #$where_arp
  122. $sth = $dbh->prepare("select upper(macadr::text),switch_ip,vlan_id,switch_port from arp_perm where (office_id = $office_id)  ");
  123. $sth->execute() or die $sth->errstr;
  124. while (@row = $sth->fetchrow_array) {
  125.     %dbtable->{$row[0]} = $row[1]." ".$row[2]." ".$row[3];
  126. }
  127. $sth->finish;
  128.  
  129. $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");
  130. #                               0            1       2        3         4          5
  131. $sth3->execute() or die $sth3->errstr;
  132. while (@row = $sth3->fetchrow_array)
  133. {
  134.     $switch_ip =  $row[1];
  135.     $switch_type =  $row[0];
  136.     print "Switch $switch_ip \n";
  137.    
  138.     #redeclare file names
  139.     $filename="/tmp/getmac.log.$row[1]";
  140.     $switchfile="/tmp/getmac.ok.$row[1]";
  141.     if (-e $filename){ #removing old files
  142.         print "Removing old files...\n";
  143.         unlink $filename;
  144.         unlink $switchfile;
  145.     }
  146.    
  147.     if ( $switch_type eq "2")
  148.     {
  149.         TelnetRuby($row[1], $row[2], $row[3], $row[4]);
  150.  
  151.         $system_cmd = "cat ".$filename." | grep Dynamic | grep --invert-match  ' 9 ' | grep --invert-match ' 10 ' ";
  152.         @exclude_ports = split (/,/, $row[5]);
  153.  
  154.         foreach my $ex_port (@exclude_ports)
  155.         {
  156. #            $system_cmd = $system_cmd . "| grep --invert-match ' ".$ex_port."  ' ";
  157.         }
  158.  
  159.         $system_cmd = $system_cmd ." > ".$switchfile;
  160.         system $system_cmd;
  161.  
  162. #Get current macs
  163.         %switchtable = &CurrentMACs($switchfile, $switch_ip);
  164.     } elsif ( $switch_type eq "3")
  165.     {
  166. #telnet Dlink
  167.         TelnetDLink($row[1], $row[2], $row[3], $row[4]);
  168.  
  169.         $system_cmd = "cat ".$filename_dlink." | grep Dynamic ";
  170.         @exclude_ports = split (/,/, $row[5]);
  171.  
  172.         foreach my $ex_port (@exclude_ports)
  173.         {
  174.             $system_cmd = $system_cmd . "| grep --invert-match ' ".$ex_port."  ' ";
  175.         }
  176.  
  177.         $system_cmd = $system_cmd ." > ".$switchfile_dlink;
  178.         system $system_cmd;
  179.  
  180. #Get current macs
  181.         %switchtable = &CurrentMACsDL($switchfile_dlink, $switch_ip);
  182.     } elsif ( $switch_type eq "4")
  183.     {
  184. #telnet NetGear
  185.         TelnetNetGear($row[1], $row[2], $row[3], $row[4]);
  186.  
  187.         $system_cmd = "cat ".$filename." | grep Learned ";
  188.         @exclude_ports = split (/,/, $row[5]);
  189.  
  190.         foreach my $ex_port (@exclude_ports)
  191.         {
  192.             $system_cmd = $system_cmd . "| grep --invert-match ' ".$ex_port."  ' ";
  193.         }
  194.  
  195.         $system_cmd = $system_cmd ." > ".$switchfile;
  196.         system $system_cmd;
  197.  
  198. #Get current macs
  199.         %switchtable = &CurrentMACsNG($switchfile, $switch_ip);
  200.     } elsif ( $switch_type eq "5")
  201.     {
  202. #telnet Zyxel
  203.         TelnetZyxel($row[1], $row[2], $row[3], $row[4]);
  204.  
  205.         $system_cmd = "cat ".$filename." | grep Learned ";
  206.         @exclude_ports = split (/,/, $row[5]);
  207.  
  208.         foreach my $ex_port (@exclude_ports)
  209.         {
  210.             $system_cmd = $system_cmd . "| grep --invert-match '".$ex_port."  ' ";
  211.         }
  212.  
  213.         $system_cmd = $system_cmd ." > ".$switchfile;
  214.         system $system_cmd;
  215.  
  216. #Get current macs
  217.         %switchtable = &CurrentMACsZy($switchfile, $switch_ip);
  218.     } elsif ( $switch_type eq "1")
  219.     {
  220.         %switchtable = &SnmpCurrentMACs($switch_ip, $row[3]);
  221.         #SnmpCurrentMACs2($switch_ip, $row[3]);
  222.     }
  223.  
  224.     foreach my $key (keys %switchtable)
  225.     {
  226.         if ( defined($dbtable{$key}) )
  227.         {
  228. #print "key = $key data = $dbtable{$key}\n";
  229.             if ( $dbtable{$key} ne $switchtable{$key} )
  230.             {
  231.                 my ($switch_ip, $vlan, $port) = split (/\s/, $switchtable{$key});
  232. #print "update arp_perm set switch_ip = $switch_ip, vlan_id = $vlan, switch_port = $port where (macadr = $key)  \n";
  233.                 $sth2 = $dbh->prepare("select * from new_mac_upd (?,'$switch_ip'::inet,?,?,?)");
  234.                 $sth2->execute($key, $vlan, $port, $office_id);
  235.                 $dbh->commit();
  236.                 $sth2->finish;
  237.                 if ( trim($dbtable{$key}) ne "" )
  238.                 {
  239. #print "here\n";
  240.                     $sth2 = $dbh->prepare("insert into arp_change (macadr, switch_ip, old_port, new_port, office_id) VALUES (?,  ? , ?, ?, ?) ");
  241.                     $sth2->execute($key, $switch_ip, $dbtable{$key},$switchtable{$key}, $office_id);
  242.                     $dbh->commit();
  243.                     $sth2->finish;
  244.                 }
  245.             }
  246.         }
  247.     else
  248.     {
  249. #                $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') ");
  250. #                $sth2->execute($key, $switch_ip, $vlan, $port , $office_id);
  251.                 my ($switch_ip, $vlan, $port) = split (/\s/, $switchtable{$key});
  252.  
  253. #print "select * from new_mac_ins ('$key','$switch_ip'::inet,$vlan,'$port',$office_id); \n" ;
  254.                 $sth2 = $dbh->prepare("select * from new_mac_ins (?,'$switch_ip'::inet,?,?,?) ");
  255.                 $sth2->execute($key, $vlan, $port , $office_id);
  256.                 $dbh->commit();
  257.                 $sth2->finish;
  258.     }
  259.     }
  260. #update last_check
  261. $dbh->do(q{update  conpoints  set last_check=now() where ipaddr=? },    undef,$switch_ip);
  262. $dbh->commit();
  263.  
  264. }
  265. $sth3->finish;
  266.  
  267. $dbh->disconnect  or warn $dbh->errstr;
  268.  
  269. sub TelnetRuby
  270. {
  271.     my $ok;
  272.     my @lines;
  273.     my $line;
  274.     my $fh;
  275.     my $login_error;
  276.     my ($switch_ip,$login_user,$login_pass, $prompt_sym) = @_;
  277.  
  278.  
  279.     $session->errmode("return");
  280.     $ok = $session->open($switch_ip);
  281.  
  282. #$ok = $session->open(Host => $switch_ip,Port => 23,Errmode => "return",Timeout => 10,);
  283.     if ($session->timed_out() || ($ok ne 1) )
  284.     {
  285.         print "No connection_1. $switch_ip\n";
  286.         #$error_type=1;
  287.         #&error_loging();
  288.         $session->print("logout");
  289.         sleep 2;
  290.         $session->close;
  291.         return;
  292.     }
  293.  
  294.  
  295.     $ok =  $session->waitfor( Match      => "/Managed Switch/",
  296.                             Errmode => "return",
  297.                             Timeout => 3);
  298.     if ($session->timed_out() || ($ok ne 1) )
  299.     {
  300.         print "No connection_3. $switch_ip\n";
  301.         #$error_type=3;
  302.         #&error_loging();
  303.         $session->print("logout");
  304.         sleep 2;
  305.         $session->close;
  306.         return;
  307.     }
  308.    
  309.     my $error=$session->login($login_user,$login_pass);
  310.     if ($error ne 1 ){
  311.         print "Error username or password\n";
  312.         #$session->close;
  313.         #$error_type=4;
  314.         #error_loging();
  315.         return;
  316.     }else{
  317.         #check for admin rights
  318.         @chk_line=$session->cmd('restore');
  319.         @inv=split(/ /, @chk_line[0]);
  320.         if($inv[0] eq 'Invalid'){ #if is returned Invalid command restore have anybody connected
  321.             print("Login with non admin acc\n");
  322.             #$error_type=5;
  323.             $session->print("logout");
  324.             #&error_loging();
  325.             $session->close;
  326.             return;
  327.         }
  328.         $session->waitfor("/".$prompt_sym."/");
  329.         $session->print("mac-table");
  330.         $session->waitfor("/".$prompt_sym."/");
  331.         $session->print("");
  332.         $session->print("information");
  333.         $session->waitfor("/".$prompt_sym."/");
  334.         $session->print(" ");
  335.         $session->waitfor("/".$prompt_sym."/");
  336.         sleep 1;
  337.         $line = $session->lastline;
  338.         $session->print(" ");
  339.         $session->waitfor("/".$prompt_sym."/");
  340.         $line = $session->lastline;
  341.         $session->print(" ");
  342.         $session->waitfor("/".$prompt_sym."/");
  343.         $line = $session->lastline;
  344.         $session->print(" ");
  345.         $session->waitfor("/".$prompt_sym."/");
  346.         $line = $session->lastline;
  347.         $session->print(" ");
  348.         $session->waitfor("/".$prompt_sym."/");
  349.         $line = $session->lastline;
  350.  
  351.         $session->print(" ");
  352.         $session->waitfor("/".$prompt_sym."/");
  353.         $line = $session->lastline;
  354.         $fh = $session->input_log($filename);
  355.         $session->print("show");
  356.         $ok = 0;
  357.         do {
  358.             $session->waitfor(Match      => "/(Q to quit)/",Timeout    => 5);
  359.             $session->print(" ");
  360.             @lines = $session->getlines(All => 0);
  361.             foreach $msg ( @lines ) {   chomp($msg);
  362.                                         if ($msg=~ m/#/) {$ok=1;} }
  363.  
  364.         } until ($session->timed_out() || ( $ok eq 1 ));
  365.  
  366.  
  367.         $fh = $session->input_log("");
  368.         $session->print("logout");
  369.         sleep 2;
  370.         $session->close;
  371.     }
  372. }
  373.  
  374. sub TelnetNetGear
  375. {
  376.     my $ok;
  377.     my @lines;
  378.     my $line;
  379.     my $fh;
  380.  
  381.     my ($switch_ip,$login_user,$login_pass, $prompt_sym) = @_;
  382.  
  383.  
  384.     $session->errmode("return");
  385.  
  386. $ok =    $session->open($switch_ip);
  387. if ($session->timed_out() || ($ok ne 1) )
  388. {
  389. print "No connection_1 .\n";
  390.     $session->print("logout");
  391.     sleep 2;
  392.     $session->close;
  393. return;
  394. }
  395.  
  396.     $session->waitfor("/User:/");
  397. if ($session->timed_out() )
  398. {
  399. print "No connection_2 .\n";
  400.     $session->print("logout");
  401.     sleep 2;
  402.     $session->close;
  403. return;
  404. }
  405.  
  406.     $session->print($login_user);
  407.     $session->waitfor("/Password:/");
  408.     $session->print($login_pass);
  409.     $session->waitfor("/>/");
  410.     $session->print("ena");
  411.     $session->waitfor("/Password:/");
  412.     $session->print("");
  413.     $session->waitfor("/".$prompt_sym."/");
  414.  
  415.     $fh = $session->input_log($filename);
  416.  
  417.     $session->print("show mac-addr-table");
  418.     $session->waitfor("/".$prompt_sym."/");
  419.  
  420.     $ok = 0;
  421.     do {
  422.         $session->waitfor(Match      => "--More-- or (q)uit",Timeout    => 2);
  423.         $session->print(" ");
  424.         @lines = $session->getlines(All => 0);
  425.         foreach $msg ( @lines ) {   chomp($msg);    if ($msg=~ m/#/) {$ok=1;} }
  426.  
  427.     } until ($session->timed_out() || ( $ok eq 1 ));
  428.  
  429.  
  430.     $fh = $session->input_log("");
  431.     $session->print("logout");
  432.     sleep 2;
  433.     $session->close;
  434. }
  435.  
  436. sub TelnetDLink
  437. {
  438.     my $ok;
  439.     my @lines;
  440.     my $line;
  441.     my $fh;
  442.  
  443.     my ($switch_ip,$login_user,$login_pass, $prompt_sym) = @_;
  444.  
  445.  
  446.     $session->errmode("return");
  447. $ok =    $session->open($switch_ip);
  448.  
  449. if ($session->timed_out() || ($ok ne 1) )
  450. {
  451. print "No connection_1 .\n";
  452.     $session->print("logout");
  453.     sleep 2;
  454.     $session->close;
  455. return;
  456. }
  457.  
  458.     $session->waitfor("/ame:/");
  459. if ($session->timed_out() )
  460. {
  461. print "No connection_2 .\n";
  462.     $session->print("logout");
  463.     sleep 2;
  464.     $session->close;
  465. return;
  466. }
  467.     $session->print($login_user);
  468.     $session->waitfor("/ord:/");
  469.     $session->print($login_pass);
  470.     $session->waitfor("/".$prompt_sym."/");
  471.  
  472. sleep 1;
  473.     $session->print(" ");
  474.     $session->waitfor("/".$prompt_sym."/");
  475.     $line = $session->lastline;
  476.     $session->print(" ");
  477.     $session->waitfor("/".$prompt_sym."/");
  478.     $line = $session->lastline;
  479.     $session->print(" ");
  480.     $session->waitfor("/".$prompt_sym."/");
  481.     $line = $session->lastline;
  482.     $session->print(" ");
  483.     $session->waitfor("/".$prompt_sym."/");
  484.     $line = $session->lastline;
  485.  
  486.     $fh = $session->input_log($filename_dlink);
  487.     $session->print("show fdb");
  488.     $ok = 0;
  489.     do {
  490.         $session->waitfor(Match      => "/Next/",Timeout    => 1);
  491.         $session->print("n");
  492.         @lines = $session->getlines(All => 0);
  493.         foreach $msg ( @lines ) {   chomp($msg);    if ($msg=~ m/Total/) {$ok=1;} }
  494.     } until ($session->timed_out() || ( $ok eq 1 ));
  495.  
  496.  
  497.     $fh = $session->input_log("");
  498.     $session->print("logout");
  499.     sleep 2;
  500.     $session->close;
  501. }
  502.  
  503. sub TelnetZyxel
  504. {
  505.     my $ok;
  506.     my @lines;
  507.     my $line;
  508.     my $fh;
  509.  
  510.     my ($switch_ip,$login_user,$login_pass, $prompt_sym) = @_;
  511.  
  512.  
  513.     $session->errmode("return");
  514.  
  515. $ok =    $session->open($switch_ip);
  516. if ($session->timed_out() || ($ok ne 1) )
  517. {
  518. print "No connection_1 .\n";
  519.     $session->print("logout");
  520.     sleep 2;
  521.     $session->close;
  522. return;
  523. }
  524.  
  525.     $session->waitfor("/User name:/");
  526. if ($session->timed_out() )
  527. {
  528. print "No connection_2 .\n";
  529.     $session->print("logout");
  530.     sleep 2;
  531.     $session->close;
  532. return;
  533. }
  534.  
  535.     $session->print($login_user);
  536.     $session->waitfor("/Password:/");
  537.     $session->print($login_pass);
  538.  
  539.     $session->waitfor("/".$prompt_sym."/");
  540.     $fh = $session->input_log($filename);
  541.  
  542.     $session->print("show mac address-table all");
  543.     $session->waitfor("/".$prompt_sym."/");
  544.  
  545.     $ok = 0;
  546.     do {
  547.         @lines = $session->getlines(All => 0);
  548.         foreach $msg ( @lines ) {   chomp($msg);    if ($msg=~ m/#/) {$ok=1;} }
  549.     } until ($session->timed_out() || ( $ok eq 1 ));
  550.  
  551.  
  552.     $fh = $session->input_log("");
  553.     $session->print("logout");
  554.     sleep 2;
  555.     $session->close;
  556. }
  557.  
  558. sub SnmpCurrentMACs2
  559. {
  560.     my ($switch_ip,$comm) = @_;
  561.     use Net::SNMP;
  562. #print $switch_ip. " " .$comm."\n";
  563. # requires a hostname and a community string as its arguments
  564. ($se,$error) = Net::SNMP->session(Hostname => $switch_ip,
  565.                                        Community => $comm);
  566.  
  567. die "session error: $error" unless ($se);
  568.  
  569. # iso.org.dod.internet.mgmt.mib-2.interfaces.ifNumber.0 =
  570. #   1.3.6.1.2.1.2.1.0
  571. $result = $se->get_request(".2.17.4.3.1.2.0.8.199.187.207.220");
  572.  
  573. die "request error: ".$se->error unless (defined $result);
  574.  
  575. $se->close;
  576.  
  577. #print $result->{".2.17.4.3.1.2.0.8.199.187.207.220"}."\n";
  578. }
  579.  
  580. sub SnmpCurrentMACs
  581. {
  582.     my ($switch_ip,$comm) = @_;
  583.  
  584.     my $sess = new SNMP::Session(DestHost => $switch_ip, Community => $comm);
  585.  
  586. #    my $var = new SNMP::Varbind([ipNetToMediaPhysAddress]);
  587.     my $var = new SNMP::Varbind(["SNMPv2-SMI::mib-2"]);
  588.  
  589.     my %newtable;
  590.     my $val;
  591.     my $vlan;
  592.     my $ip;
  593.     my $ip1;
  594.     my $ip2;
  595.     my $ip3;
  596.     my $ip4;
  597.     my $m1;
  598.     my $m2;
  599.     my $m3;
  600.     my $m4;
  601.     my $m5;
  602.     my $m6;
  603.     my $mac_addr;
  604.  
  605.     do {
  606.         $val = $sess->getnext($var);
  607. #print $var->[$SNMP::Varbind::tag_f]." - ".$var->[$SNMP::Varbind::iid_f]."\n";
  608.         if ("$var->[$SNMP::Varbind::tag_f].$var->[$SNMP::Varbind::iid_f]" =~ m/ipNetToMediaPhysAddress/)
  609.         {
  610.             ($vlan,$ip1,$ip2,$ip3,$ip4) = split (/\./ , "$var->[$SNMP::Varbind::iid_f]\n");
  611.             $ip = $ip1.".".$ip2.".".$ip3.".".$ip4;
  612.  
  613.             chomp($ip);
  614.  
  615.             ($m1,$m2,$m3,$m4,$m5,$m6) = split (/:/ , "$var->[$SNMP::Varbind::val_f]");
  616.  
  617.             $mac_addr =  sprintf("%02s:%02s:%02s:%02s:%02s:%02s", $m1, $m2, $m3, $m4, $m5, $m6 );
  618.  
  619.             $newtable{$ip} = $mac_addr;
  620.  
  621. #print $ip," ",$mac_addr,"\n";
  622.         }
  623.     } until ($sess->{ErrorStr});
  624.  
  625.     return %newtable;
  626. }
  627.  
  628.  
  629. sub CurrentMACs
  630. {
  631.     my %newtable;
  632.     my $mac;
  633.     my $port;
  634.     my $vlan;
  635.     my $tmp;
  636.     my $rec;
  637.     my ($filename,$switch_ip) = @_;
  638.  
  639.     open(FILE, "<$filename") or die "Error 1 \n";
  640.     while (<FILE>)
  641.     {
  642.             $tmp = trim($_);
  643.  
  644.             ($mac,$port,$vlan,$tmp) = split (/\s+/, $tmp);
  645.  
  646.             $mac =~ s/-+/:/g;
  647.             upcase_in($mac);
  648.             $rec = "$switch_ip $vlan $port";
  649.             $newtable{$mac} = $rec;
  650.     }
  651.  
  652.     close(FILE);
  653.  
  654.     return %newtable;
  655. }
  656.  
  657. sub CurrentMACsDL
  658. {
  659.     my %newtable;
  660.     my $mac;
  661.     my $port;
  662.     my $vlan;
  663.     my $tmp;
  664.     my $rec;
  665.     my ($filename,$switch_ip) = @_;
  666.  
  667.     open(FILE, "<$filename") or die "Error 1 \n";
  668.     while (<FILE>)
  669.     {
  670.             $tmp = trim($_);
  671.  
  672.             ($vlan,$tmp,$mac,$port) = split (/\s+/, $tmp);
  673.  
  674.             $mac =~ s/-+/:/g;
  675.             upcase_in($mac);
  676.             $rec = "$switch_ip $vlan $port";
  677. #print $rec."-".$mac."\n";
  678.             $newtable{$mac} = $rec;
  679.     }
  680.  
  681.     close(FILE);
  682.  
  683.     return %newtable;
  684. }
  685. sub CurrentMACsNG
  686. {
  687.     my %newtable;
  688.     my $mac;
  689.     my $port;
  690.     my $vlan;
  691.     my $tmp;
  692.     my $rec;
  693.     my $vlanmac;
  694.     my ($filename,$switch_ip) = @_;
  695.  
  696.     open(FILE, "<$filename") or die "Error 1 \n";
  697.     while (<FILE>)
  698.     {
  699.             $tmp = trim($_);
  700.             ($mac,$tmp,$port,$vlan) = split (/\s+/, $tmp);
  701. #           $vlan = hex(substr($vlanmac,0,1)) * 256  + hex(substr($vlanmac,3,4));
  702. #           $mac = substr($vlanmac,6,22);
  703.             $mac =~ s/-+/:/g;
  704.             upcase_in($mac);
  705.             $rec = "$switch_ip $vlan $port";
  706. #print $rec."-".$mac."\n";
  707.             $newtable{$mac} = $rec;
  708.     }
  709.  
  710.     close(FILE);
  711.  
  712.     return %newtable;
  713. }
  714.  
  715. sub CurrentMACsZy
  716. {
  717.     my %newtable;
  718.     my $mac;
  719.     my $port;
  720.     my $vlan;
  721.     my $tmp;
  722.     my $rec;
  723.     my $vlanmac;
  724.     my ($filename,$switch_ip) = @_;
  725.  
  726.     open(FILE, "<$filename") or die "Error 1 \n";
  727.     while (<FILE>)
  728.     {
  729.             $tmp = trim($_);
  730.             ($port,$vlan,$mac,$tmp) = split (/\s+/, $tmp);
  731.             $mac =~ s/-+/:/g;
  732.             upcase_in($mac);
  733.             $rec = "$switch_ip $vlan $port";
  734. #print $rec."-".$mac."\n";
  735.             $newtable{$mac} = $rec;
  736.     }
  737.  
  738.     close(FILE);
  739.  
  740.     return %newtable;
  741. }
  742.  
  743. sub upcase_in {  for (@_) { tr/a-z/A-Z/ }   }
  744.  
  745. sub trim
  746. {
  747.     my @out = @_;
  748.     for (@out)
  749.     {
  750.         s/^\s+//;
  751.         s/\s+$//;
  752.     }
  753.     return wantarray ? @out : $out[0];
  754. }
  755.  
  756. sub error_loging(){
  757.    if ( $logging > 1)
  758.     {
  759.     print "\n Saving error.... \n";
  760.     $sth4 = $dbh->prepare("INSERT INTO errors(ip, \"type\", date) VALUES ('$switch_ip', $error_type, now());");
  761.     $sth4->execute() or die $sth4->errstr;
  762.     $sth4->finish;
  763.     }
  764. }
Add Comment
Please, Sign In to add comment