bdcom.pm 6.6 KB

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