purge_bdcom.pm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. use Modern::Perl;
  2. use utf8;
  3. use telnet;
  4. use Exporter;
  5. our @ISA= qw( Exporter );
  6. our @EXPORT = qw( purge_bdcom );
  7. sub purge_bdcom::_extract_onu_num {
  8. my $onu_name = shift;
  9. $onu_name =~ m|EPON0/\d+:(\d+)|;
  10. return $1;
  11. };
  12. sub purge_bdcom::_extract_onu_info {
  13. my @lines = @_;
  14. return map([substr($_, 0, 11), split(' ', substr($_, 11, -1))], @lines[3..@lines-3]);
  15. };
  16. async purge_bdcom => sub
  17. {
  18. my $ip = shift;
  19. my $login = shift;
  20. my $password = shift;
  21. my $tree = shift;
  22. ############################
  23. my $t = new telnet($ip);
  24. # $t->debug(1);
  25. await $t->connect;
  26. await $t->reply(qr/Username:/, $login);
  27. await $t->reply(qr/Password:/, $password);
  28. my @greeting = await $t->waitfor(qr/>/);
  29. my $version = "C";
  30. for (@greeting)
  31. {
  32. $version = $1 if /Welcome to BDCOM P3310(\w)/;
  33. }
  34. $t->print("enable");
  35. my @next = await $t->waitfor(qr/#|password:/);
  36. if (grep {$_ =~ /password:/} @next)
  37. {
  38. $t->print($password);
  39. await $t->waitfor(qr/#/);
  40. }
  41. $t->prompt(qr/#/);
  42. await $t->cmd("terminal length 0");
  43. await $t->cmd("terminal width 200");
  44. #######################
  45. my @inactive_onu = await $t->cmd("show epon inactive-onu interface ePON 0/$tree");
  46. $inactive_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
  47. my $inactive_count = $1 || "?";
  48. if (@inactive_onu <= 1) {
  49. $inactive_count = 0;
  50. }
  51. my @active_onu = await $t->cmd("show epon active-onu interface ePON 0/$tree");
  52. $active_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
  53. my $active_count = $1 || "?";
  54. if (@active_onu <= 1) {
  55. $active_count = 0;
  56. }
  57. my $total_count = $active_count + $inactive_count;
  58. if ($inactive_count == 0) {
  59. $t->close;
  60. return "Нечего чистить";
  61. }
  62. await $t->cmd("config");
  63. await $t->cmd("interface EPON0/$tree");
  64. my @in_onu_info = purge_bdcom::_extract_onu_info(@inactive_onu);
  65. my @compact_in_onu_info = map( [purge_bdcom::_extract_onu_num(@$_[0]), @$_[1]], @in_onu_info );
  66. foreach my $i ( @compact_in_onu_info ) {
  67. say Dumper @$i[1];
  68. my $onu_mac = @$i[1];
  69. my @tmp = await $t->cmd("no epon bind-onu mac $onu_mac");
  70. say Dumper @tmp;
  71. }
  72. my @deleted_onu = map( join(" ", @$_), @compact_in_onu_info);
  73. await $t->cmd("exit");
  74. @inactive_onu = await $t->cmd("show epon inactive-onu interface ePON 0/$tree");
  75. $inactive_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
  76. $inactive_count = $1 || "?";
  77. if (@inactive_onu <= 1) {
  78. $inactive_count = 0;
  79. }
  80. @active_onu = await $t->cmd("show epon active-onu interface ePON 0/$tree");
  81. $active_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
  82. $active_count = $1 || "?";
  83. if (@active_onu <= 1) {
  84. $active_count = 0;
  85. }
  86. $total_count = $active_count + $inactive_count;
  87. await $t->cmd("write all");
  88. $t->close;
  89. my $res = "После чистки осталось ONU: $total_count
  90. Количество активных ONU: $active_count
  91. Количество неактивных ONU: $inactive_count
  92. \n";
  93. $res = $res . "Удалённые ONU:\n" . join("\n", @deleted_onu) if @deleted_onu > 0;
  94. return $res;
  95. };
  96. 1;