1 |
package ip; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use Data::Dump qw/dump/; |
7 |
|
8 |
use html; |
9 |
|
10 |
sub lines { |
11 |
my ( $object ) = @_; |
12 |
|
13 |
$object ||= 'link'; |
14 |
|
15 |
my $i = 0; |
16 |
my @objects; |
17 |
|
18 |
my @headers; |
19 |
my $headers; |
20 |
|
21 |
foreach my $line ( split(/\n/, `ip $object`) ) { |
22 |
warn "# $line\n"; |
23 |
my $add_header; |
24 |
|
25 |
if ( $line =~ s{^(\d+):\s+(\w+):\s+}{} ) { |
26 |
$i = $1; |
27 |
$objects[$i] = { $object => $2 }; |
28 |
$add_header = $object; |
29 |
} elsif ( $line =~ s{^\s+}{} ) { |
30 |
# line continues |
31 |
} else { |
32 |
$i++; |
33 |
$line =~ s{^(\S+)\s+}{}; |
34 |
$objects[$i] = { |
35 |
$object => $1 |
36 |
}; |
37 |
$add_header = $object; |
38 |
} |
39 |
|
40 |
push @headers, $add_header if $add_header && ! $headers->{$add_header}++; |
41 |
|
42 |
if ( $line =~ s{<(\S+)>\s+}{} ) { |
43 |
$add_header = 'flags'; |
44 |
$objects[$i]->{$add_header} = $1; |
45 |
push @headers, $add_header unless $headers->{$add_header}++; |
46 |
} |
47 |
|
48 |
my @p = split(/\s+/, $line); |
49 |
while (@p) { |
50 |
my ($n,$v) = splice @p, 0, 2; |
51 |
if ( ! defined $v ) { |
52 |
$objects[$i]->{''} = $n; |
53 |
$n = ''; |
54 |
} elsif ( $n =~ m{^(.+)/(.+)$} ) { |
55 |
$n = $1; |
56 |
$objects[$i]->{$n} = $2; |
57 |
$objects[$i]->{mac} = $v; |
58 |
} else { |
59 |
$objects[$i]->{$n} = $v; |
60 |
} |
61 |
push @headers, $n unless $headers->{$n}++; |
62 |
} |
63 |
|
64 |
# warn dump( $objects[$i] ); |
65 |
} |
66 |
|
67 |
return unless @objects; |
68 |
|
69 |
$objects[0] = [ @headers ]; |
70 |
|
71 |
return @objects; |
72 |
} |
73 |
|
74 |
sub html { |
75 |
my @objects = ip::lines( @_ ); |
76 |
return '' unless @objects; |
77 |
|
78 |
my @headers = @{ shift @objects }; |
79 |
|
80 |
my @table = ( -( $#headers + 1), @headers ); |
81 |
foreach my $o ( @objects ) { |
82 |
foreach ( 0 .. $#headers ) { |
83 |
my $h = $headers[$_]; |
84 |
push @table, $o->{$h}; |
85 |
} |
86 |
} |
87 |
|
88 |
return html::table( @table ); |
89 |
} |
90 |
|
91 |
1; |