-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCisco.pm
144 lines (137 loc) · 4.06 KB
/
Cisco.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package CFG2JSON::Cisco;
use strict;
use NetAddr::IP;
use Data::Dumper;
my $gbics;
sub new{
$gbics=();
my $class = shift;
my $args = { @_ };
my $config=$args->{config};
my $dev=getinfo($config);
$gbics=buildGbicHash($config);
#print Dumper $gbics;
my $interfaces=getinterfaces($config);
#my $interfaces={};
$dev->{interfaces}=$interfaces;
my $self = bless {device=>$dev}, $class;
}
sub getinfo{
my @config=split("\n",$_[0]);
my $obj={};
for(@config){
push(@{$obj->{syslog}},$2) if $_=~/logging( vrf MGMT)? host ([\d\.]+)/i;
push(@{$obj->{snmp}},$1) if $_=~/snmp-server host ([\d\.]+) version/i;
push(@{$obj->{tacacs}},$1) if $_=~/tacacs-server host ([\d\.]+)/i;
$obj->{serial}=$1 if $_=~/!Serial Number:\s+(.*)/i;
$obj->{model}=$1 if $_=~/!Model number\s+:\s+(.*)/i;
$obj->{version}=$1 if $_=~/!Image: Software:\s+.*,\s(.*),.*/i;
$obj->{taaccounting}=$1 if $_=~/aaa accounting commands (.*)/i;
}
return $obj;
}
sub getinterfaces{
my $c = shift;
my $ints;
$c =~ s/\n/<nl>/g;
$c =~ s/\s+!//ig;
$c =~ s/([\w])!/$1/ig;
my @ints_arr=split("!",$c);
shift @ints_arr;
for(@ints_arr){
my $int=$_;
if($int=~/<nl>interface\s+([fskpmvlgibrtortchanel\d\-\/\.]+[\d])<nl>(.*)/ig){
my $i=$1;
my $rc=$2;
$ints->{$i}{mtu}='1500';
$rc=~s/!<nl>router$//i;
$ints->{$i}{rawconfig}=$rc;
if($i=~/^(vlan|port).*/i){
$ints->{$i}{formfactor}='virtual';
}elsif($gbics->{lc($i)}){
$ints->{$i}{formfactor}=$gbics->{lc($i)}{formfactor};
$ints->{$i}{serial}=$gbics->{lc($i)}{serial};
}elsif($i=~/gigabitethernet/i){
$ints->{$i}{formfactor}='10/100/1000BaseTX';
}elsif($i=~/fast/i){
$ints->{$i}{formfactor}='100BASE-TX';
}else{
$ints->{$i}{formfactor}='physical';
}
for(split/<nl>/,$rc){
my $l=$_;
push(@{$ints->{$i}{ipaddress}},{ip=>$1,type=>'vrrp',version=>'4'}) if $l=~/vrrp\s[\d]+\sip\s([\d]+\..*)/;
push(@{$ints->{$i}{ipaddress}},{ip=>$1,type=>'vrrp',version=>'6'}) if $l=~/vrrp\s[\d]+\sip\s([\w]+:.*)/;
if($l =~ m/\s+switchport\saccess\svlan\s([\d]+.*)$/){
push(@{$ints->{$i}{vlans}},$1) if $l =~ m/\s+switchport\saccess\svlan\s([\d]+.*)$/;
$ints->{$i}{mode}='access';
}
if($l =~ /\sswitchport\strunk\sallowed\svlan\s(add\s)?([-,\d.*]+)$/){
$ints->{$i}=addGroups($ints->{$i},$2);
$ints->{$i}{mode}='tagged';
}
$ints->{$i}{description}=$1 if $l=~/\s+description\s([!\"\'():,\@.&\w\s\/\-]+).*$/i;
$ints->{$i}{vrf}=$1 if $l=~/\s+vrf forwarding (.*)/i;
$ints->{$i}{mtu}=$1 if $l=~/\s+mtu\s([\d]+)/i;
if($l=~/\s+ip(v6)?\saddress\s([:.\w]+)\s([\d\.]+).*$/i){
my $version=$1;
$version=~s/v//;
$version='4' if !$version;
my ($ip,$bits)=_getCIDR($2,$3);
push(@{$ints->{$i}{ipaddress}},{ip=>$ip,bits=>$bits,type=>'interface',version=>$version});
}
}
}
}
$ints->{console}{formfactor}='NONE';
return $ints
}
sub _getCIDR{
my ($ip,$mask)=@_;
my $ninet = new NetAddr::IP "$ip $mask";
my $bits=$ninet->masklen;
return ($ip,$bits);
}
sub buildGbicHash{
my $c=shift;
$c =~ s/\n/<nl>/g;
$c =~ s/<nl>!PID:/PID:/g;
$c =~ s/<nl>!VID:/VID:/g;
$c =~ s/<nl>!SN:/SN:/g;
my $gb;
for(split(/<nl>/,$c)){
#if($_=~/!NAME: \"(.*)\"(\s+)?DESCR:\s\"(.*)\"(\s)?PID:\s(.*)VID:\s(.*)SN:\s(.*)/){
if($_=~/!NAME: \"(.*)\",(\s+)?DESCR:\s\"(.*)\"(\s)?PID:\s(.*)VID:\s(.*)SN:\s(.*)/){
my $int=lc($1);
my $descr=$3;
my $pid=$5;
my $vid=$6;
my $sn=$7;
$gb->{$int}{formfactor}=$descr;
$gb->{$int}{serial}=$sn;
}
}
return $gb;
}
sub addGroups{
my ($int,$info)=@_;
my @p_arr;
for(split(/,/,$info)){
my $pn=$_;
if($pn=~/([\d]+\/)?([\d]+)-([\d]+\/)?([\d]+)/){
my $i1s=$1;
my $i1p=$2;
my $i2s=$3;
my $i2p=$4;
for($i1p...$i2p){
my $p=$i1s.'/'.$_;
$p=$_ if !$i1s;
push(@{$int->{vlans}},$_)
}
}else{
push(@{$int->{vlans}},$pn)
}
}
return $int;
}
1