-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcisco_info.py
154 lines (131 loc) · 4.99 KB
/
cisco_info.py
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
145
146
147
148
149
150
151
152
153
154
"""
Copyright (c) 2019 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
Filename: cisco_info.py
Version: Python 3.7.2
Authors: Aaron Warner ([email protected])
Description: This program is designed to retrieve the serial number
and license entitlement for Cisco Catalyst switches running
IOS-XE 16.8 and below.
"""
from datetime import datetime
import csv
from multiprocessing.dummy import Pool as ThreadPool
import netmiko
import sys
import re
def getLoginInfo(csvFile):
"""
Function to create list of dictionaries from CSV file.
"""
with open(csvFile, "r") as swfile:
reader = csv.DictReader(swfile)
data = []
for line in reader:
data.append(line)
return data
def convertLoginDict(data):
"""
Converts list of dictionaries to list of lists
"""
try:
swlist = [[row["ipaddr"], row["username"], row["password"]] for row in data]
return swlist
except KeyError:
print("\nInvalid header in CSV file. Please modify to the format below: "
"\nipaddr,username,password"
"\n10.10.10.10,admin,cisco"
"\n10.10.10.11,admin,cisco\n"
)
sys.exit()
def getDevInfo(ip, user, pwd):
"""
Function logs in to Cisco Switch and executes
'show inventory' and 'show license right-to-use summary' on each device.
Parses out serial number & technology package & writes them to CSV file
device_info.csv.
"""
session = {
"device_type": "cisco_ios",
"ip": ip,
"username": user,
"password": pwd,
"verbose": False,
}
try:
print("Connecting to switch {switch}".format(switch=ip))
conn = netmiko.ConnectHandler(**session)
info = conn.send_command("show version")
sn = re.search(r"(?=.*\d{3})[A-Z]{3}[A-Za-z0-9]{8}", info)
sn = sn.group(0)
pid = re.search(r"C9[356]00.+?(?=\s)|[A-Z]{2}.[A-Z][0-9]{4}.+?(?=\s)", info)
pid = pid.group(0)
tech = re.search(r"network-.+?(?=\s)|ipservicesk9|ipbasek9|lanbasek9", info)
tech = tech.group(0)
devdata = [pid, sn, tech]
print("Collection successful from switch {switch}".format(switch=ip))
with open("device_info.csv", "a") as devFile:
devwriter = csv.writer(devFile)
devwriter.writerow(devdata)
print(
"Data for switch {switch} successfully written to device_info.csv".format(
switch=ip
)
)
except (
netmiko.NetMikoTimeoutException,
netmiko.NetMikoAuthenticationException,
) as e:
print(e)
except AttributeError:
print("Regex match error. Missing info for device {switch}".format(switch=ip))
def main(args):
try:
title = ["Product ID", "Serial Number", "License Entitlement"]
with open("device_info.csv", "w+") as devFile:
devwriter = csv.writer(devFile)
devwriter.writerow(title)
# Define start time
start_time = datetime.now()
# Define the number of threads
num_threads = int(sys.argv[2])
pool = ThreadPool(num_threads)
# Import CSV file and generate list of dictionaries
csvFile = sys.argv[1]
data = getLoginInfo(csvFile)
# Convert list of dictionaries to list of lists
swlist = convertLoginDict(data)
# Start threads
pool.starmap(getDevInfo, swlist)
pool.close()
pool.join()
print("\nReview collected information in device_info.csv")
print("\nElapsed time: " + str(datetime.now() - start_time))
except ValueError:
print("Invalid entry for number of threads. Please enter an integer.")
if __name__ == "__main__":
if len(sys.argv) == 3:
main(sys.argv)
else:
print(
"\nThis program is designed to retrieve the serial number\n"
"and license entitlement for Cisco Catalyst switches running\n"
"IOS-XE 16.8 and below.\n"
"\nThe program accepts two arguments. The name of a CSV file and the number of desired threads.\n "
"\nThe CSV should be in the format below:\n"
"\nipaddr,username,password"
"\n10.10.10.10,admin,cisco"
"\n10.10.10.11,admin,cisco\n"
"\nUsage: python get_info.py DeviceDetails.csv X\n"
"\nThe 'X' represents the number of threads to execute"
)
sys.exit()