Skip to content

Instantly share code, notes, and snippets.

@kinjo kinjo/inventory.cgi
Created Jun 19, 2016

Embed
What would you like to do?
Obtain inventory by JSON. Cisco Catalyst is supported.
#!/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
You can’t perform that action at this time.