bdcom.pm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_fun_purge_tree => sub
  74. {
  75. my $t = shift;
  76. my $tree = shift;
  77. my $onu_info = shift;
  78. my %onu_info = %{$onu_info};
  79. await $t->cmd("config");
  80. await $t->cmd("interface EPON0/$tree");
  81. # say Dumper %onu_info;
  82. foreach my $i ( @{$onu_info{'inactive'}} ) {
  83. # say Dumper @$i[1];
  84. my $onu_mac = @$i[1];
  85. # say Dumper $onu_mac;
  86. my @tmp = await $t->cmd("no epon bind-onu mac $onu_mac");
  87. # say ADumper @tmp;
  88. }
  89. my @deleted_onu = map { sprintf("%2d %s", $_->[0], $_->[1]) } @{$onu_info{inactive}};
  90. await $t->cmd("exit");
  91. return @deleted_onu;
  92. };
  93. async bdcom_inspect => sub
  94. {
  95. my $ip = shift;
  96. my $login = shift;
  97. my $password = shift;
  98. my $tree = shift;
  99. ############################
  100. my $t = new telnet($ip);
  101. # $t->debug(1);
  102. await bdcom_login($t, $login, $password);
  103. #######################
  104. my %onu_info = await bdcom_get_onu_info($t, $tree);
  105. $t->close;
  106. my $res = "Всего: $onu_info{'total_cnt'};
  107. Количество активных ONU: $onu_info{'active_cnt'};
  108. Количество неактивных ONU: $onu_info{'inactive_cnt'};
  109. \n";
  110. $res = $res . "Неактивные ONU:\n" . join("\n", map( join(" ", @$_), @{$onu_info{'inactive'}} ) ) . "\n\n";
  111. $res = $res . "Активные ONU:\n" . join("\n", map( join(" ", @$_), @{$onu_info{'active'}} ) );
  112. return $res;
  113. };
  114. async bdcom_onu => sub
  115. {
  116. my $ip = shift;
  117. my $login = shift;
  118. my $password = shift;
  119. my $tree = 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. $t->close;
  127. my $res = "Всего: $onu_info{'total_cnt'};
  128. Количество активных ONU: $onu_info{'active_cnt'};
  129. Количество неактивных ONU: $onu_info{'inactive_cnt'};
  130. \n";
  131. return $res;
  132. };
  133. async bdcom_purge => sub
  134. {
  135. my $ip = shift;
  136. my $login = shift;
  137. my $password = shift;
  138. my $tree = shift;
  139. my $save = shift;
  140. ############################
  141. my $t = new telnet($ip);
  142. # $t->debug(1);
  143. await bdcom_login($t, $login, $password);
  144. #######################
  145. my %onu_info = await bdcom_get_onu_info($t, $tree);
  146. if ($onu_info{'inactive_cnt'} == 0) {
  147. $t->close;
  148. return "Нечего чистить";
  149. }
  150. my @deleted_onu = await bdcom_fun_purge_tree($t, $tree, \%onu_info);
  151. %onu_info = await bdcom_get_onu_info($t, $tree);
  152. if ($save) {
  153. await $t->cmd("write all");
  154. }
  155. $t->close;
  156. my $res = "Осталось ONU: $onu_info{'total_cnt'}\n";
  157. if ( $onu_info{'active_cnt'} != $onu_info{'total_cnt'} ) {
  158. $res = $res . "Общее количество ONU и количество активных ONU не совпадает.
  159. Лучше обратиться к Вашему системному администратору\n";
  160. }
  161. $res = $res . "Удалённые ONU:\n" . join("\n", @deleted_onu) if @deleted_onu > 0;
  162. return $res;
  163. };
  164. async bdcom_purgeall => sub
  165. {
  166. my $ip = shift;
  167. my $login = shift;
  168. my $password = shift;
  169. my $tree_count = shift;
  170. my $save = shift;
  171. ############################
  172. my @res;
  173. my $t = new telnet($ip);
  174. # $t->debug(1);
  175. await bdcom_login($t, $login, $password);
  176. #######################
  177. for (my $tree=1; $tree <= $tree_count; $tree++) {
  178. my $res = "Дерево $tree\n";
  179. my %onu_info = await bdcom_get_onu_info($t, $tree);
  180. if ($onu_info{'inactive_cnt'} == 0) {
  181. $res = $res . "Не имеет неактивных ONU\n";
  182. push @res, $res;
  183. next;
  184. }
  185. my @deleted_onu = await bdcom_fun_purge_tree($t, $tree, \%onu_info);
  186. # say Dumper @deleted_onu;
  187. %onu_info = await bdcom_get_onu_info($t, $tree);
  188. $res = $res . "Осталось ONU: $onu_info{'total_cnt'}\n";
  189. $res = $res . "Удалённые ONU:\n" . join("\n", @deleted_onu) if @deleted_onu > 0;
  190. push @res, $res;
  191. }
  192. if ($save) {
  193. await $t->cmd("write all");
  194. }
  195. $t->close;
  196. return \@res;
  197. };
  198. async bdcom_save => sub
  199. {
  200. my $ip = shift;
  201. my $login = shift;
  202. my $password = shift;
  203. ############################
  204. my $t = new telnet($ip);
  205. # $t->debug(1);
  206. await bdcom_login($t, $login, $password);
  207. #######################
  208. await $t->cmd("write all");
  209. $t->close;
  210. my $res = "Сохранено\n";
  211. return $res;
  212. };
  213. 1;