浏览代码

compact bdcom

alakiza 2 年之前
父节点
当前提交
344f319bb2
共有 3 个文件被更改,包括 297 次插入6 次删除
  1. 288 0
      modules/bdcom.pm
  2. 0 0
      modules/cdata.pm
  3. 9 6
      modules/commands.pm

+ 288 - 0
modules/bdcom.pm

@@ -0,0 +1,288 @@
+use Modern::Perl;
+use utf8;
+
+use Mojo::AsyncAwait;
+use Data::Dumper;
+
+sub bdcom_extract_onu_num {
+    my $onu_name = shift;
+
+    $onu_name =~ m|EPON0/\d+:(\d+)|;
+
+    return $1;
+}
+
+sub bdcom_extract_onu_info {
+    my @lines = @_;
+
+    return map([substr($_, 0, 11), split(' ', substr($_, 11, -1))], @lines[3..@lines-3]);
+}
+
+sub bdcom_login {
+
+}
+
+async bdcom_inspect => 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 @inactive_onu = await $t->cmd("show epon inactive-onu interface ePON 0/$tree");
+    $inactive_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
+    my $inactive_count = $1 || "?";
+    if (@inactive_onu <= 1) {
+        $inactive_count = 0;
+    }
+
+    my @active_onu = await $t->cmd("show epon active-onu interface ePON 0/$tree");
+    $active_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
+    my $active_count = $1 || "?";
+    if (@active_onu <= 1) {
+        $active_count = 0;
+    }
+
+    my $total_count = $active_count + $inactive_count;
+
+    $t->close;
+
+    my $res = "Всего: $total_count
+Количество активных ONU: $active_count
+Количество неактивных ONU: $inactive_count
+\n";
+
+    my @in_onu_info = bdcom_extract_onu_info(@inactive_onu);
+    my @compact_in_onu_info = map( [bdcom_extract_onu_num(@$_[0]), @$_[1]], @in_onu_info );
+    @compact_in_onu_info = map( join(" ", @$_), @compact_in_onu_info);
+
+    say Dumper @active_onu;
+    my @a_onu_info = bdcom_extract_onu_info(@active_onu);
+    say "test2";
+    my @compact_a_onu_info = map( [bdcom_extract_onu_num(@$_[0]), @$_[1]], @a_onu_info );
+    @compact_a_onu_info = map( join(" ", @$_), @compact_a_onu_info);
+
+    $res = $res . "Неактивные ONU:\n" . join("\n", @compact_in_onu_info) . "\n\n";
+
+    $res = $res . "Активные ONU:\n" . join("\n", @compact_a_onu_info);
+
+    return $res;
+};
+
+async bdcom_onu => 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 @inactive_onu = await $t->cmd("show epon inactive-onu interface ePON 0/$tree");
+    $inactive_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
+    my $inactive_count = $1 || "?";
+    if (@inactive_onu <= 1) {
+        $inactive_count = 0;
+    }
+
+    $t->prompt(qr/#/);
+
+    my @active_onu = await $t->cmd("show epon active-onu interface ePON 0/$tree");
+    $active_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
+    my $active_count = $1 || "?";
+    if (@active_onu <= 1) {
+        $active_count = 0;
+    }
+
+
+    my $total_count = $active_count + $inactive_count;
+
+    $t->close;
+
+    my $res = "Всего: $total_count
+Количество активных ONU: $active_count
+Количество неактивных ONU: $inactive_count
+\n";
+
+    return $res;
+};
+
+async bdcom_purge => 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 @inactive_onu = await $t->cmd("show epon inactive-onu interface ePON 0/$tree");
+    $inactive_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
+    my $inactive_count = $1 || "?";
+    if (@inactive_onu <= 1) {
+        $inactive_count = 0;
+    }
+
+    my @active_onu = await $t->cmd("show epon active-onu interface ePON 0/$tree");
+    $active_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
+    my $active_count = $1 || "?";
+    if (@active_onu <= 1) {
+        $active_count = 0;
+    }
+
+    my $total_count = $active_count + $inactive_count;
+
+    if ($inactive_count == 0) {
+        $t->close;
+        return "Нечего чистить";
+    }
+
+    await $t->cmd("config");
+    await $t->cmd("interface EPON0/$tree");
+
+    my @in_onu_info = bdcom_extract_onu_info(@inactive_onu);
+    my @compact_in_onu_info = map( [bdcom_extract_onu_num(@$_[0]), @$_[1]], @in_onu_info );
+
+    foreach my $i ( @compact_in_onu_info ) {
+        say Dumper @$i[1];
+        my $onu_mac = @$i[1];
+        my @tmp = await $t->cmd("no epon bind-onu mac $onu_mac");
+        say Dumper @tmp;
+    }
+
+    my @deleted_onu = map( join(" ", @$_), @compact_in_onu_info);
+
+    await $t->cmd("exit");
+
+    @inactive_onu = await $t->cmd("show epon inactive-onu interface ePON 0/$tree");
+    $inactive_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
+    $inactive_count = $1 || "?";
+    if (@inactive_onu <= 1) {
+        $inactive_count = 0;
+    }
+
+    @active_onu = await $t->cmd("show epon active-onu interface ePON 0/$tree");
+    $active_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
+    $active_count = $1 || "?";
+    if (@active_onu <= 1) {
+        $active_count = 0;
+    }
+
+    $total_count = $active_count + $inactive_count;
+
+    await $t->cmd("write all");
+
+    $t->close;
+
+    my $res = "После чистки осталось ONU: $total_count
+Количество активных ONU: $active_count
+Количество неактивных ONU: $inactive_count
+\n";
+
+    $res = $res . "Удалённые ONU:\n" . join("\n", @deleted_onu) if @deleted_onu > 0;
+
+    return $res;
+};
+
+
+1;

+ 0 - 0
modules/cdata.pm


+ 9 - 6
modules/commands.pm

@@ -4,12 +4,14 @@ use utf8;
 use Mojo::AsyncAwait;
 use Data::Dumper;
 use purge_cdata qw(purge_cdata);
-use purge_bdcom qw(purge_bdcom);
-use inspect_bdcom qw(inspect_bdcom);
+#use purge_bdcom qw(purge_bdcom);
+#use inspect_bdcom qw(inspect_bdcom);
 use inspect_cdata qw(inspect_cdata);
-use onu_bdcom qw(onu_bdcom);
+#use onu_bdcom qw(onu_bdcom);
 use onu_cdata qw(onu_cdata);
 
+use bdcom;
+
 our $client;
 
 ##############################################
@@ -55,7 +57,8 @@ async command_inspect => sub
   my $sub;  
   if ($dev->{type} =~ /^BDCOM/)
   {
-    $sub = reference("inspect_bdcom");
+    $sub = reference("bdcom_inspect");
+#    $sub = sub { bdcom->inspect(@_) };
   }
   elsif ($dev->{type} =~ /^C-DATA/)
   {
@@ -108,7 +111,7 @@ async command_onu => sub
   my $sub;  
   if ($dev->{type} =~ /^BDCOM/)
   {
-    $sub = reference("onu_bdcom");
+    $sub = reference("bdcom_onu");
   }
   elsif ($dev->{type} =~ /^C-DATA/)
   {
@@ -164,7 +167,7 @@ async command_purge => sub
   my $sub;  
   if ($dev->{type} =~ /^BDCOM/)
   {
-    $sub = reference("purge_bdcom");
+    $sub = reference("bdcom_purge");
   }
   elsif ($dev->{type} =~ /^C-DATA/)
   {