strip traling spaces
[APKPM.git] / lib / H1 / ZTEMSAN.pm
1 package H1::ZTEMSAN;
2 use warnings;
3 use strict;
4
5 use Net::Telnet;
6 use Data::Dump qw(dump);
7
8 sub hash {
9         my $self = shift;
10         my $args = {@_};
11
12         warn "# info ", dump($args);
13
14 my $t = Net::Telnet->new( Timeout => 20, Port => 1123, Prompt => '/#/' );
15
16 $t->dump_log('/tmp/log') if $ENV{DEBUG};
17
18 warn "open";
19 $t->open( $args->{ip} );
20
21 $t->waitfor('/Login:/');
22 $t->print('root');
23 $t->waitfor('/Password:/');
24 $t->print('root');
25 #$t->waitfor('/>/');
26 #$t->print('en');
27 #$t->waitfor('/Please input password:/');
28 #$t->print('admin');
29 $t->waitfor('/#/');
30
31 warn "login OK";
32
33 my $port = "$args->{shelf}/$args->{slot}/$args->{port}";
34
35 sub command {
36         my $command = shift;
37
38         warn "# command $command\n";
39         $t->print($command);
40
41         my $out;
42         while (1) {
43                 warn "waitfor";
44                 my($prematch, $match) = $t->waitfor('/(Press any key to continue \(Q to quit\)|#)/');
45                 $out .= $prematch;
46                 last if $match eq '#';
47 #               $t->put(" ");
48                 $t->print('');
49         }
50
51         warn "## out = [$out]";
52
53         my $hash;
54         my $section = '';
55         my $last_line;
56         foreach my $line ( split(/[\n\r]+/, $out) ) {
57                 warn "# $line\n";
58                 if ( $line =~ m/^(\S+.*?)\s+:\s+(\S+.*?)\s*$/ ) {
59                         my ($n,$v) = ($1,$2);
60                         $n =~ s/\(.+\)//;
61                         $hash->{ $section . $n } = $v;
62                         warn "## $n = $v\n";
63                 } elsif ( $line =~ m/^-+$/ ) {
64                         $section = $last_line . '.'
65                 }
66                 $last_line = $line;
67         }
68
69         warn "## hash = ",dump $hash;
70
71         if ( $ENV{DEBUG} ) {
72                 my $path = $command;
73                 $path =~ s{\W+}{_}g;
74                 $path = "/tmp/dump.$path";
75                 open( my $fh, '>', $path ) || die "$path: $!";
76                 print $fh $out;
77                 close $fh;
78                 warn "DEBUG ",$path, " ", -s $path, " bytes\n";
79         }
80
81         return $hash;
82 }
83
84 our ( $row, $hash );
85
86 sub copy {
87         my @what = @_ ? @_ : keys %$hash;
88         foreach my $name (@what) {
89                 warn "# copy $name ", dump( $hash->{$name} ),$/;
90                 $row->{$name} = $hash->{$name};
91         }
92 }
93
94 $hash = command "show adsl port $port";
95 copy;
96
97 $hash = command "show adsl port $port line-config";
98 copy;
99
100 $hash = command "show adsl port $port physical-table";
101 copy;
102
103 warn "# row = ",dump $row;
104
105 warn "logout";
106 $t->print('logout');
107 $t->waitfor('/:/');
108 $t->print('y');
109
110
111 } # sub
112 1;
113