Created
June 19, 2016 16:03
-
-
Save takumikinjo/dab934e92d5a3e84ef4ade8d4b14fd23 to your computer and use it in GitHub Desktop.
Obtain inventory by JSON. Cisco Catalyst is supported.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use File::Basename; | |
use JSON 'encode_json'; | |
my $XLOGIN = '/usr/lib/rancid/bin/clogin'; | |
sub usage | |
{ | |
print <<EOS; | |
{'usage': 'q=<IP addr or hostname of device>'} | |
EOS | |
exit; | |
} | |
sub parse | |
{ | |
my $buf = shift, @a; | |
sub tr { shift =~ s/^\s*|\s*$//gr } | |
my $fn = sub { | |
shift =~ s/NAME: "(.*?)", DESCR: "(.*?)"\nPID: (.*?), VID: (.*?), SN: (.*?)$/push @a, {NAME => &tr($1), DESCR => &tr($2), PID => &tr($3), VID => &tr($4), SN => &tr($5)}/mer; | |
}; | |
$buf =~ s/(NAME:.*?\nPID:.*)/$fn->($1)/eg; | |
return \@a; | |
} | |
use CGI; | |
print "Content-type: application/json\n\n"; | |
my $q = CGI->new(); | |
usage unless ($q->param('q')); | |
my $device = $q->param('q'); | |
my $buf; | |
open FH, "HOME=/var/www $XLOGIN -c 'show inventory' $device |" or die $!; | |
while (<FH>) { | |
s/\r//g; | |
$buf .= $_; | |
} | |
close FH; | |
print encode_json({ | |
$device => [ | |
grep { | |
$_->{NAME} =~ /Ethernet|^subslot/ | |
} @${\parse($buf)} | |
] | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment