Prechádzať zdrojové kódy

change managment protocol for mikrotik devices to ssh

rajven 4 rokov pred
rodič
commit
0ac4bacd3a
3 zmenil súbory, kde vykonal 70 pridanie a 51 odobranie
  1. 28 20
      scripts/Rstat/cmd.pm
  2. 13 13
      scripts/Rstat/config.pm
  3. 29 18
      scripts/sync_mikrotik.pl

+ 28 - 20
scripts/Rstat/cmd.pm

@@ -259,7 +259,7 @@ my $device = shift;
 #router
 if ($device->{device_type} eq '2') {
     #mikrotik
-    if ($device->{vendor_id} eq '9') { $device->{port}=$config_ref{router_port}; }
+#    if ($device->{vendor_id} eq '9') { $device->{port}=$config_ref{router_port}; }
     $device->{login}=$config_ref{router_login};
     $device->{password}=$config_ref{router_password};
     }
@@ -280,12 +280,13 @@ my $device = shift;
 #skip unknown vendor
 if (!$switch_auth{$device->{vendor_id}}) { return; }
 if (!$switch_auth{$device->{vendor_id}}{proto}) { $switch_auth{$device->{vendor_id}}{proto} = 'telnet'; }
+if (!$device->{port} and $switch_auth{$device->{vendor_id}}{port}) { $device->{port} = $switch_auth{$device->{vendor_id}}{port}; }
 
 my $t;
 
 if ($switch_auth{$device->{vendor_id}}{proto} eq 'telnet') {
     if (!$device->{port}) { $device->{port} = '23'; }
-    log_info("Try login to $device->{device_name} ip: $device->{ip} by telnet...");
+    log_info("Try login to $device->{device_name} $device->{ip}:$device->{port} by telnet...");
 
     #zyxel patch
     if ($device->{vendor_id} eq '4') {
@@ -338,7 +339,7 @@ if ($switch_auth{$device->{vendor_id}}{proto} eq 'telnet') {
 
 if ($switch_auth{$device->{vendor_id}}{proto} eq 'ssh') {
     if (!$device->{port}) { $device->{port} = '22'; }
-    log_info("Try login to $device->{device_name} ip: $device->{ip} by ssh...");
+    log_info("Try login to $device->{device_name} $device->{ip}:$device->{port} by ssh...");
     eval {
         $t  = Net::SSH::Expect->new (
             host=>$device->{ip},
@@ -371,43 +372,43 @@ if ($switch_auth{$device->{vendor_id}}{proto} eq 'ssh') {
         if ($rc !~ /$switch_auth{$device->{vendor_id}}{prompt}/) { return; }
 
         if (exists $switch_auth{$device->{vendor_id}}{enable}) {
-            $t->send($switch_auth{$device->{vendor_id}}{enable}."\n");
+            $t->send($switch_auth{$device->{vendor_id}}{enable}."\n\r");
 #            $t->print($device->{enable_password});
             $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
             }
 
         if ($device->{vendor_id} eq '2') {
-            $t->send("terminal datadump");
+            $t->send("terminal datadump\n\r");
             $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
-            $t->send("no logging console");
+            $t->send("no logging console\n\r");
             $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
             }
         if ($device->{vendor_id} eq '5') {
-            $t->send("terminal page-break disable");
+            $t->send("terminal page-break disable\n\r");
             $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
             }
         if ($device->{vendor_id} eq '6') {
-            $t->send("terminal length 0");
+            $t->send("terminal length 0\n\r");
             $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
             }
         if ($device->{vendor_id} eq '9') {
-            $t->send("/system note set show-at-login=no");
+            $t->send("/system note set show-at-login=no\n\r");
             $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
             }
         if ($device->{vendor_id} eq '16') {
-            $t->send("terminal width 0");
+            $t->send("terminal width 0\n\r");
             $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
             }
         if ($device->{vendor_id} eq '17') {
-            $t->send("more displine 50");
+            $t->send("more displine 50\n\r");
             $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
-            $t->send("more off");
+            $t->send("more off\n\r");
             $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
             }
         if ($device->{vendor_id} eq '38') {
-            $t->send("disable cli prompting");
+            $t->send("disable cli prompting\n\r");
             $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
-            $t->send("disable clipaging");
+            $t->send("disable clipaging\n\r");
             $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
             }
         };
@@ -445,14 +446,21 @@ if ($proto eq 'ssh') {
             if ($run_cmd =~ /SLEEP\s+(\d+)/i) { log_session('WAIT:'." $1 sec."); sleep($1); } else { log_session('WAIT:'." 10 sec."); sleep(10); };
             next;
             }
-        log_session('Send:'.$cmd);
-        $session->send($cmd."\n");
-        my $line;
-        while ( defined ($line = $session->read_line()) ) { push(@result,$line); }
+        log_session('Send:'.$run_cmd);
+        $session->send($run_cmd."\n\r");
+        my $chunk;
+        while ($chunk = $session->peek(1)) {
+            my $ret =$session->eat($chunk);
+            if (ref($ret) eq 'ARRAY') {
+                push(@result,@{$ret});
+                } else {
+                my @norm_text = split(/\n/,$ret);
+                foreach my $row (@norm_text) { push(@result,trim($row)); }
+                }
+            }
         select(undef, undef, undef, 0.25);
-        $session->waitfor($switch_auth{$device->{vendor_id}}{prompt}, 1);
         }
-    log_session('Get:'.Dumper(\@result));
+#    log_session('Get:'.Dumper(\@result));
     };
     if ($@) { log_error("Abort: $@"); return 0; };
     }

+ 13 - 13
scripts/Rstat/config.pm

@@ -258,19 +258,19 @@ our $tftp_server=$Config->{_}->{tftp_server} || '';
 our $last_refresh_config = time();
 
 our %switch_auth = (
-'8'=>{'vendor'=>'Allied Telesis','enable'=>'en','proto'=>'telnet','login'=> '(login|User Name):','password'=>'Password:','prompt'=>qr/(\010\013){0,5}(([-\w]+|[-\w(config)+])\#|[-\w]+\>)/},
-'3'=>{'vendor'=>'Huawei','proto'=>'ssh','enable'=>'system-view','login'=> 'login as:','password'=>'Password: ','prompt'=>qr/(\<.*\>|\[.*\])/},
-'16'=>{'vendor'=>'Cisco','proto'=>'telnet','enable'=>'en','login'=> 'Username:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
-'5'=>{'vendor'=>'Raisecom','proto'=>'telnet','enable'=>'en','login'=> 'Login:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
-'6'=>{'vendor'=>'SNR','proto'=>'telnet','login'=> 'login:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
-'7'=>{'vendor'=>'Dlink','proto'=>'telnet','login'=> 'UserName:','password'=>'PassWord:','prompt'=>qr/[-\w]+\#$/},
-#'15'=>{'vendor'=>'HP','proto'=>'telnet','enable'=>'system-view','login'=> 'login:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
-'2'=>{'vendor'=>'Eltex','proto'=>'telnet','login'=> 'User Name:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
-'17'=>{'vendor'=>'Maipu','proto'=>'telnet','login'=> 'login:','password'=>'password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
-'4'=>{'vendor'=>'Zyxel','proto'=>'telnet','login'=> 'User name:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+]|[-\w(config-interface)+])\#/},
-'38'=>{'vendor'=>'Qtech','proto'=>'telnet','enable'=>'en','login'=> 'login:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
-'9'=>{'vendor'=>'Mikrotik','proto'=>'telnet','login'=> 'Login:','password'=>'Password:','prompt'=>qr/\[[-\w]+\@[-\w]+\]\s+\>/},
-'39'=>{'vendor'=>'Extreme','proto'=>'telnet','login'=> 'login:','password'=>'password:','prompt'=>qr/[-\w]+\s\#\s/},
+'8'=>{'vendor'=>'Allied Telesis','enable'=>'en','proto'=>'telnet','port'=>'23','login'=> '(login|User Name):','password'=>'Password:','prompt'=>qr/(\010\013){0,5}(([-\w]+|[-\w(config)+])\#|[-\w]+\>)/},
+'3'=>{'vendor'=>'Huawei','proto'=>'ssh','port'=>'22','enable'=>'system-view','login'=> 'login as:','password'=>'Password: ','prompt'=>qr/(\<.*\>|\[.*\])/},
+'16'=>{'vendor'=>'Cisco','proto'=>'telnet','port'=>'23','enable'=>'en','login'=> 'Username:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
+'5'=>{'vendor'=>'Raisecom','proto'=>'telnet','port'=>'23','enable'=>'en','login'=> 'Login:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
+'6'=>{'vendor'=>'SNR','proto'=>'telnet','port'=>'23','login'=> 'login:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
+'7'=>{'vendor'=>'Dlink','proto'=>'telnet','port'=>'23','login'=> 'UserName:','password'=>'PassWord:','prompt'=>qr/[-\w]+\#$/},
+#'15'=>{'vendor'=>'HP','proto'=>'telnet','port'=>'23','enable'=>'system-view','login'=> 'login:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
+'2'=>{'vendor'=>'Eltex','proto'=>'telnet','port'=>'23','login'=> 'User Name:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
+'17'=>{'vendor'=>'Maipu','proto'=>'telnet','port'=>'23','login'=> 'login:','password'=>'password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
+'4'=>{'vendor'=>'Zyxel','proto'=>'telnet','port'=>'23','login'=> 'User name:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+]|[-\w(config-interface)+])\#/},
+'38'=>{'vendor'=>'Qtech','proto'=>'telnet','port'=>'23','enable'=>'en','login'=> 'login:','password'=>'Password:','prompt'=>qr/([-\w]+|[-\w(config)+])\#/},
+'9'=>{'vendor'=>'Mikrotik','proto'=>'ssh','port'=>'60022','login'=> 'login as:','password'=>'password:','prompt'=>qr/\[[-\w]+\@[-\w]+\]\s+\>/},
+'39'=>{'vendor'=>'Extreme','proto'=>'telnet','port'=>'23','login'=> 'login:','password'=>'password:','prompt'=>qr/[-\w]+\s\#\s/},
 );
 
 mkdir $LOG_DIR unless (-d $LOG_DIR);

+ 29 - 18
scripts/sync_mikrotik.pl

@@ -21,8 +21,6 @@ use Date::Parse;
 use Rstat::net_utils;
 use Rstat::mysql;
 use DBI;
-use utf8;
-use open ":encoding(utf8)";
 use Fcntl qw(:flock);
 use Parallel::ForkManager;
 
@@ -91,17 +89,17 @@ if ($l3->{'interface_type'} eq '1') { push(@wan_int,$l3->{'name'}); }
 my @cmd_list=();
 
 $gate = netdev_set_auth($gate);
-
+$gate->{login}.='+ct400w';
 my $t = netdev_login($gate);
 
-log_cmd4($t,"/system note set show-at-login=no");
-
 foreach my $int (@lan_int) { #interface dhcp loop
 next if (!$int);
 $int=trim($int);
 
 #get ip addr at interface
-my @int_addr=log_cmd4($t,'/ip address print terse without-paging where interface='.$int);
+my @int_addr=netdev_cmd($gate,$t,'ssh','/ip address print terse without-paging where interface='.$int,1);
+
+#print Dumper(\@int_addr);
 
 my $found_subnet;
 foreach my $int_str(@int_addr) {
@@ -128,7 +126,9 @@ db_log_verbose($dbh,"Analyze interface $int. Found: ".Dumper($dhcp_conf{$found_s
 if ($gate->{dhcp}) {
 
 #fetch current dhcp records
-my @ret_static_leases=log_cmd4($t,'/ip dhcp-server lease print terse without-paging where server=dhcp-'.$int);
+my @ret_static_leases=netdev_cmd($gate,$t,'ssh','/ip dhcp-server lease print terse without-paging where server=dhcp-'.$int,1);
+
+#print Dumper(\@ret_static_leases);
 
 my @current_static_leases=();
 foreach my $str (@ret_static_leases) {
@@ -361,7 +361,9 @@ $index++;
 my %cur_users;
 
 foreach my $group_name (keys %lists) {
-my @address_lists=log_cmd4($t,'/ip firewall address-list print terse without-paging where list='.$group_name);
+my @address_lists=netdev_cmd($gate,$t,'ssh','/ip firewall address-list print terse without-paging where list='.$group_name,1);
+
+#print Dumper(\@address_lists);
 
 foreach my $row (@address_lists) {
     $row=trim($row);
@@ -398,7 +400,9 @@ timestamp;
 #sync firewall rules
 
 #sync group chains
-my @chain_list=log_cmd4($t,'/ip firewall filter  print terse without-paging where chain=Users and action=jump');
+my @chain_list=netdev_cmd($gate,$t,'ssh','/ip firewall filter  print terse without-paging where chain=Users and action=jump',1);
+
+#print Dumper(\@chain_list);
 
 my %cur_chain;
 foreach my $jump_list (@chain_list) {
@@ -475,7 +479,9 @@ foreach my $group_name (keys %group_filters) {
 
 next if (!$group_name);
 
-my @get_filter=log_cmd4($t,'/ip firewall filter print terse without-paging where chain='.$group_name,1);
+my @get_filter=netdev_cmd($gate,$t,'ssh','/ip firewall filter print terse without-paging where chain='.$group_name,1);
+
+#print Dumper(\@get_filter);
 
 my @cur_filter=();
 my $chain_ok=1;
@@ -534,7 +540,10 @@ my %get_queue_type=();
 my %get_queue_tree=();
 my %get_filter_mangle=();
 
-my @tmp=log_cmd4($t,'/queue type print terse without-paging where name~"pcq_(down|up)load"');
+my @tmp=netdev_cmd($gate,$t,'ssh','/queue type print terse without-paging where name~"pcq_(down|up)load"',1);
+
+#print Dumper(\@tmp);
+
 # 0   name=pcq_upload_3 kind=pcq pcq-rate=102401k pcq-limit=500KiB pcq-classifier=src-address pcq-total-limit=2000KiB pcq-burst-rate=0 pcq-burst-threshold=0 pcq-burst-time=10s 
 #pcq-src-address-mask=32 pcq-dst-address-mask=32 pcq-src-address6-mask=64 pcq-dst-address6-mask=64
 foreach my $row (@tmp) {
@@ -561,7 +570,9 @@ if ($row=~/name=pcq_(down|up)load_(\d){1,3}\s+/i) {
 }
 
 @tmp=();
-@tmp=log_cmd4($t,'/queue tree print terse without-paging where parent~"(download|upload)_root"');
+@tmp=netdev_cmd($gate,$t,'ssh','/queue tree print terse without-paging where parent~"(download|upload)_root"',1);
+
+#print Dumper(\@tmp);
 # 0 I name=queue_3_out parent=upload_root packet-mark=upload_3 limit-at=0 queue=*2A priority=8 max-limit=0 burst-limit=0 burst-threshold=0 burst-time=0s bucket-size=0.1
 # 5 I name=queue_3_vlan2_in parent=download_root_vlan2 packet-mark=download_3_vlan2 limit-at=0 queue=*2B priority=8 max-limit=0 burst-limit=0 burst-threshold=0 burst-time=0s bucket-size=0.1
 foreach my $row (@tmp) {
@@ -596,7 +607,9 @@ if ($row=~/queue=pcq_(down|up)load_(\d){1,3}/i) {
 
 @tmp=();
 
-@tmp=log_cmd4($t,'/ip firewall mangle print terse without-paging where action=mark-packet and new-packet-mark~"(upload|download)_[0-9]{1,3}"');
+@tmp=netdev_cmd($gate,$t,'ssh','/ip firewall mangle print terse without-paging where action=mark-packet and new-packet-mark~"(upload|download)_[0-9]{1,3}"',1);
+
+#print Dumper(\@tmp);
 # 0    chain=forward action=mark-packet new-packet-mark=upload_0 passthrough=yes src-address-list=queue_0 out-interface=sfp-sfpplus1-wan log=no log-prefix=""
 # 0    chain=forward action=mark-packet new-packet-mark=download_3_vlan2 passthrough=yes dst-address-list=queue_3 out-interface=vlan2 in-interface-list=WAN log=no log-prefix=""
 
@@ -730,11 +743,9 @@ if (!$queue_ok) {
 }#end access lists config
 
 if (scalar(@cmd_list)) {
-    foreach my $cmd (@cmd_list) {
-	log_info("$cmd");
-#	print "$cmd\n" if ($debug);
-        log_cmd($t,$cmd);
-        }
+    print "Apply:\n" if ($debug);
+    foreach my $cmd (@cmd_list) { print "$cmd\n" if ($debug); }
+    netdev_cmd($gate,$t,'ssh',\@cmd_list,1);
     }
 
 db_log_verbose($dbh,"Sync user state at router $router_name [".$router_ip."] stopped.");