Yuriy Zhilovets 2 years ago
parent
commit
2db4e42f08
3 changed files with 120 additions and 4 deletions
  1. 26 4
      modules/commands.pm
  2. 56 0
      modules/purge_bdcom.pm
  3. 38 0
      modules/purge_cdata.pm

+ 26 - 4
modules/commands.pm

@@ -3,7 +3,8 @@ use utf8;
 
 use Mojo::AsyncAwait;
 use Data::Dumper;
-use Sub::Install;
+use purge_cdata;
+use purge_bdcom;
 
 our $client;
 
@@ -38,14 +39,35 @@ async command_purge => sub
 
   my $res;
   eval {
-    $res = await $client->get_p("device", "/pon?query=ip=$ip");
+    $res = await $client->get_p("device", "/pon?query=ip=\"$ip\"");
   };
   return notify($chatid, "Ошибка: " . parse_error($@)) if $@;
-  return notify($chatid, "Устройство с IP=$ip не найдено") if @$res<1;
+  return notify($chatid, "Устройство с ip=$ip не найдено") if @$res<1;
   
   my $dev = $res->[0];
+
+  my $sub;  
+  if ($dev->{type} =~ /^BDCOM/)
+  {
+    $sub = reference("purge_bdcom");
+  }
+  elsif ($dev->{type} =~ /^C-DATA/)
+  {
+    $sub = reference("purge_cdata");
+  }
+  
+  return notify($chatid, "Устройство $ip '$dev->{name}' неизвестного типа $dev->{type}") unless $sub;
+  
+  notify($chatid, "Очищаю дерево $tree на устройстве $ip '$dev->{name}'");
+
+  my $count;
+  eval {
+    $count = await $sub->($ip, $dev->{login}, $dev->{password}, $tree);
+  };
+  
+  return notify($chatid, "$ip '$dev->{name}': $@") if $@;
   
-  notify($chatid, "Очищаю дерево $tree на устройстве $ip '$dev->{name}'...");
+  notify($chatid, "$ip '$dev->{name}': работает $count ONU");
 };
 
 ###################################

+ 56 - 0
modules/purge_bdcom.pm

@@ -0,0 +1,56 @@
+use Modern::Perl;
+use utf8;
+
+use telnet;
+async purge_bdcom => sub
+{
+  my $ip = shift;
+  my $login = shift;
+  my $password = shift;
+  my $tree = shift;
+  
+  ############################
+  
+  my $t = new telnet($ip);
+#  $t->debug(1);
+
+  await $t->connect;
+
+  await $t->reply(qr/Username:/, $login);
+  await $t->reply(qr/Password:/, $password);
+
+  my @greeting = await $t->waitfor(qr/>/);
+  my $version = "C";
+
+  for (@greeting)
+  {
+    $version = $1 if /Welcome to BDCOM P3310(\w)/;
+  }
+
+  $t->print("enable");
+
+  my @next = await $t->waitfor(qr/#|password:/);
+
+  if (grep {$_ =~ /password:/} @next)
+  {
+    $t->print($password);
+    await $t->waitfor(qr/#/);
+  }
+
+  $t->prompt(qr/#/);
+  
+  await $t->cmd("terminal length 0");
+  await $t->cmd("terminal width 200");
+  
+  #######################
+  
+  my @lines = await $t->cmd("show epon active-onu interface ePON 0/$tree");
+  $lines[0] =~ m|Interface EPON0/\d+ has bound (\d+) active ONUs|;
+  my $count = $1 || "?";
+
+  $t->close;
+  
+  return $count;
+};
+
+1;

+ 38 - 0
modules/purge_cdata.pm

@@ -0,0 +1,38 @@
+use Modern::Perl;
+use utf8;
+
+use telnet;
+
+async purge_cdata => sub
+{
+  my $ip = shift;
+  my $login = shift;
+  my $password = shift;
+  my $tree = shift;
+  
+  my $t = new telnet($ip);
+  $t->debug(1);
+
+  await $t->connect;
+
+  await $t->reply(qr/User name:/, $login);
+  await $t->reply(qr/User password:/, $password);
+  
+  $t->print("enable");
+  await $t->waitfor("#");
+  $t->prompt(qr/#\s?$/);
+  
+  await $t->cmd("config");
+  await $t->cmd("vty output show-all");
+  
+  $t->print("Interface epon 0/$tree");
+  my @lines = await $t->cmd("show ont info $tree  all");
+  
+  say Dumper @lines;
+  
+  $t->close;
+  
+  return 5;
+};
+
+1;