bdcom.pm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. use Modern::Perl;
  2. use utf8;
  3. use telnet;
  4. use Mojo::AsyncAwait;
  5. use Data::Dumper;
  6. sub bdcom_extract_onu_num {
  7. my $onu_name = shift;
  8. $onu_name =~ m|EPON0/\d+:(\d+)|;
  9. return $1;
  10. }
  11. sub bdcom_extract_onu_info {
  12. my @lines = @_;
  13. return map([substr($_, 0, 11), split(' ', substr($_, 11, -1))], @lines[3..@lines-3]);
  14. }
  15. async bdcom_login => sub {
  16. my $t = shift;
  17. my $login = shift;
  18. my $password = shift;
  19. await $t->connect;
  20. await $t->reply(qr/Username:/, $login);
  21. await $t->reply(qr/Password:/, $password);
  22. my @greeting = await $t->waitfor(qr/>/);
  23. my $version = "C";
  24. for (@greeting)
  25. {
  26. $version = $1 if /Welcome to BDCOM P3310(\w)/;
  27. }
  28. $t->print("enable");
  29. my @next = await $t->waitfor(qr/#|password:/);
  30. if (grep {$_ =~ /password:/} @next)
  31. {
  32. $t->print($password);
  33. await $t->waitfor(qr/#/);
  34. }
  35. $t->prompt(qr/#/);
  36. await $t->cmd("terminal length 0");
  37. await $t->cmd("terminal width 200");
  38. };
  39. async bdcom_get_onu_info => sub
  40. {
  41. my $t = shift;
  42. my $tree = shift;
  43. my @inactive_onu = await $t->cmd("show epon inactive-onu interface ePON 0/$tree");
  44. $inactive_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
  45. my $inactive_count = $1 || "?";
  46. if (@inactive_onu <= 1) {
  47. $inactive_count = 0;
  48. }
  49. my @active_onu = await $t->cmd("show epon active-onu interface ePON 0/$tree");
  50. $active_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
  51. my $active_count = $1 || "?";
  52. if (@active_onu <= 1) {
  53. $active_count = 0;
  54. }
  55. my $total_count = $active_count + $inactive_count;
  56. my @in_onu_info = bdcom_extract_onu_info(@inactive_onu);
  57. my @compact_in_onu_info = map( [bdcom_extract_onu_num(@$_[0]), @$_[1]], @in_onu_info );
  58. #@compact_in_onu_info = map( join(" ", @$_), @compact_in_onu_info);
  59. # say Dumper @active_onu;
  60. my @a_onu_info = bdcom_extract_onu_info(@active_onu);
  61. # say "test2";
  62. my @compact_a_onu_info = map( [bdcom_extract_onu_num(@$_[0]), @$_[1]], @a_onu_info );
  63. #@compact_a_onu_info = map( join(" ", @$_), @compact_a_onu_info);
  64. my %res = (
  65. 'total_cnt' => $total_count,
  66. 'active_cnt' => $active_count,
  67. 'inactive_cnt' => $inactive_count,
  68. 'active' => [ @compact_a_onu_info ],
  69. 'inactive' => [ @compact_in_onu_info ],
  70. );
  71. return %res;
  72. };
  73. async bdcom_inspect => sub
  74. {
  75. my $ip = shift;
  76. my $login = shift;
  77. my $password = shift;
  78. my $tree = shift;
  79. ############################
  80. my $t = new telnet($ip);
  81. # $t->debug(1);
  82. await bdcom_login($t, $login, $password);
  83. #######################
  84. my %onu_info = await bdcom_get_onu_info($t, $tree);
  85. $t->close;
  86. my $res = "Всего: $onu_info{'total_cnt'};
  87. Количество активных ONU: $onu_info{'active_cnt'};
  88. Количество неактивных ONU: $onu_info{'inactive_cnt'};
  89. \n";
  90. $res = $res . "Неактивные ONU:\n" . join("\n", map( join(" ", @$_), @{$onu_info{'inactive'}} ) ) . "\n\n";
  91. $res = $res . "Активные ONU:\n" . join("\n", map( join(" ", @$_), @{$onu_info{'active'}} ) );
  92. return $res;
  93. };
  94. async bdcom_onu => sub
  95. {
  96. my $ip = shift;
  97. my $login = shift;
  98. my $password = shift;
  99. my $tree = shift;
  100. ############################
  101. my $t = new telnet($ip);
  102. # $t->debug(1);
  103. await bdcom_login($t, $login, $password);
  104. #######################
  105. my %onu_info = await bdcom_get_onu_info($t, $tree);
  106. $t->close;
  107. my $res = "Всего: $onu_info{'total_cnt'};
  108. Количество активных ONU: $onu_info{'active_cnt'};
  109. Количество неактивных ONU: $onu_info{'inactive_cnt'};
  110. \n";
  111. return $res;
  112. };
  113. async bdcom_purge => sub
  114. {
  115. my $ip = shift;
  116. my $login = shift;
  117. my $password = shift;
  118. my $tree = shift;
  119. my $save = shift;
  120. ############################
  121. my $t = new telnet($ip);
  122. # $t->debug(1);
  123. await bdcom_login($t, $login, $password);
  124. #######################
  125. my %onu_info = await bdcom_get_onu_info($t, $tree);
  126. if ($onu_info{'inactive_cnt'} == 0) {
  127. $t->close;
  128. return "Нечего чистить";
  129. }
  130. await $t->cmd("config");
  131. await $t->cmd("interface EPON0/$tree");
  132. foreach my $i ( @{$onu_info{'inactive'}} ) {
  133. # say Dumper @$i[1];
  134. my $onu_mac = @$i[1];
  135. my @tmp = await $t->cmd("no epon bind-onu mac $onu_mac");
  136. # say Dumper @tmp;
  137. }
  138. my @deleted_onu = map { sprintf("%2d %s", $_[0], $_[1]) } @{$onu_info{inactive}};
  139. await $t->cmd("exit");
  140. %onu_info = await bdcom_get_onu_info($t, $tree);
  141. if ($save) {
  142. await $t->cmd("write all");
  143. }
  144. $t->close;
  145. my $res = "Осталось ONU: $onu_info{'total_cnt'}\n";
  146. if ( $onu_info{'active_cnt'} != $onu_info{'total_cnt'} ) {
  147. $res = $res . "Общее количество ONU и количество активных ONU не совпадает.
  148. Лучше обратиться к Вашему системному администратору\n";
  149. }
  150. $res = $res . "Удалённые ONU:\n" . join("\n", @deleted_onu) if @deleted_onu > 0;
  151. return $res;
  152. };
  153. 1;