bdcom.pm 7.0 KB

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