fix second column parsing
[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                         if ( $v =~ s/\s+(\S+)\s*:\s+(\S+.+)// ) {
62                                 # strip second column
63                                 my ($n2,$v2) = ($1,$2);
64                                 $n2 =~ s/\(.+\)//;
65                                 $hash->{ $section . $n2 } = $2;
66                         }
67                         $hash->{ $section . $n } = $v;
68                         warn "## $n = $v\n";
69                 } elsif ( $line =~ m/^-+$/ ) {
70                         $section = $last_line . '.'
71                 }
72                 $last_line = $line;
73         }
74
75         warn "## hash = ",dump $hash;
76
77         if ( $ENV{DEBUG} ) {
78                 my $path = $command;
79                 $path =~ s{\W+}{_}g;
80                 $path = "/tmp/dump.$path";
81                 open( my $fh, '>', $path ) || die "$path: $!";
82                 print $fh $out;
83                 close $fh;
84                 warn "DEBUG ",$path, " ", -s $path, " bytes\n";
85         }
86
87         return $hash;
88 }
89
90 our ( $row, $hash );
91
92 sub copy {
93         my @what = @_ ? @_ : keys %$hash;
94         foreach my $name (@what) {
95                 warn "# copy $name ", dump( $hash->{$name} ),$/;
96                 $row->{$name} = $hash->{$name};
97         }
98 }
99
100 $hash = command "show adsl port $port";
101 copy;
102
103 $hash = command "show adsl port $port line-config";
104 copy;
105
106 $hash = command "show adsl port $port physical-table";
107 copy;
108
109 warn "# row = ",dump $row;
110
111 warn "logout";
112 $t->print('logout');
113 $t->waitfor('/:/');
114 $t->print('y');
115
116
117 } # sub
118 1;
119