Created
December 9, 2016 14:44
-
-
Save diegogslomp/9cb642d2ca74a4719cb291f8398b6c64 to your computer and use it in GitHub Desktop.
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 python | |
# -*- coding: utf-8 -*- | |
"""Ask Switches | |
Usage: | |
ask-switches.py <csvfile> [-u <user>] [-p <password>] [-o <output_file>] | |
ask-switches.py -h | --help | |
Identify enabled|disabled vlan authorization and disabled radius fe*.* ports | |
Arguments: | |
<csvfile> NetSight auto export csv file | |
Options: | |
-h --help Show this screen | |
-u <user> --user=<user> Telnet user | |
-p <password> --password=<password> Telnet password | |
-o <output_file> --output=<output_file> File to export csv info | |
""" | |
import sys | |
import telnetlib | |
import getpass | |
try: | |
import tablib | |
from docopt import docopt | |
except ImportError: | |
if int(sys.version[0]) < 3: | |
print("Error: Python version >= 3 needed! Exiting..") | |
else: | |
print("Error: tablib and docopt modules needed. Install it with pip:") | |
print("pip install docopt tablib") | |
sys.exit(1) | |
def get_user(user): | |
""" | |
Get telnet user to loggin in switches | |
""" | |
if user is None: | |
user = input('Telnet user: ') | |
return user | |
def get_password(password): | |
""" | |
Get telnet password to loggin in switches | |
""" | |
if password is None: | |
password = getpass.getpass() | |
return password | |
def get_data(csvfile, last_index=12): | |
""" | |
Returns data from NetSight csv exported file | |
""" | |
data = [] | |
try: | |
with open(csvfile, 'r') as f: | |
headers = f.readline().replace('"', '').replace('\n', '')\ | |
.split(',')[:last_index] | |
for line in f: | |
item = line.replace('"', '').replace('\n', '')\ | |
.split(',')[:last_index] | |
data.append(item) | |
data = tablib.Dataset(*data, headers=headers) | |
except FileNotFoundError: | |
print("Error: File not found! Exiting..") | |
sys.exit(1) | |
except PermissionError: | |
print("Error: No permission to read file! Exiting..") | |
sys.exit(1) | |
return data | |
def get_telnet_response(ip, user, password, tn_command, tn_filter=None): | |
""" | |
Returns telnet response string | |
""" | |
data = [] | |
try: | |
found = False | |
tn = telnetlib.Telnet(ip) | |
tn.read_until(b"Username:") | |
tn.write(user.encode('ascii') + b"\n") | |
tn.read_until(b"Password:") | |
tn.write(password.encode('ascii') + b"\n") | |
tn.write(tn_command.encode('ascii') + b"\n") | |
tn.write(b"exit\n") | |
tn_response = tn.read_all().decode('ascii') | |
for item in tn_response.replace('\r', '').split('\n'): | |
if tn_filter.lower() in item.lower(): | |
found = True | |
data.append(item) | |
if not found: | |
data.append('Filter or Login Error') | |
except Exception: | |
data.append('No connection') | |
return data | |
def get_telnet_results(ips, user, password, | |
tn_command, tn_filter=None, exclude=[]): | |
""" | |
Return telnet result from ips list | |
""" | |
data = [] | |
for ip in ips: | |
result = 'No selected' | |
if ip not in exclude: | |
result = get_telnet_response(ip, user, password, | |
tn_command, tn_filter) | |
data.append(';'.join(result)) | |
return data | |
def get_vlan_authorization(ips, user, password): | |
""" | |
Returns vlan authorization from ips | |
""" | |
data = [] | |
tn_command = 'show vlanauthorization' | |
tn_filter = 'Vlan Authorization' | |
tn_data = get_telnet_results(ips, user, password, tn_command, tn_filter) | |
for item in tn_data: | |
split_it = item.split() | |
try: | |
if split_it[3].lower() in ('enabled', 'disabled'): | |
data.append(split_it[3].capitalize()) | |
else: | |
data.append('Unknown') | |
except IndexError: | |
data.append(item) | |
return data | |
def get_vlan_from_ports(ips, user, password, exclude=[]): | |
""" | |
Returns vlan from ports | |
""" | |
data = [] | |
tn_command = 'show vlan portinfo' | |
tn_filter = 'tagged' | |
tn_data = get_telnet_results(ips, user, password, | |
tn_command, tn_filter, exclude) | |
for item_list in tn_data: | |
ports = [] | |
split_list = item_list.split(';') | |
for item in split_list: | |
split_it = str(item).split() | |
try: | |
if split_it[1] != '1' and 'fe.' in split_it[0]: | |
ports.append('{port}:{vlan}'.format(port=split_it[0], | |
vlan=split_it[1])) | |
except IndexError: | |
pass | |
data.append(';'.join(ports)) | |
return data | |
def exclude_in_get_vlan_from_ports(data): | |
""" | |
Returns list of ips with vlanautoriozation disabled | |
""" | |
exclude = [] | |
for item in data: | |
if item[12] == 'Disabled': | |
exclude.append(item[0]) | |
return exclude | |
def get_ips(data): | |
""" | |
Returns ip list from data | |
""" | |
try: | |
ips = data['IP Address'] | |
except KeyError: | |
print("Error: 'IP Address' column not found in csvfile! Exiting..") | |
sys.exit(1) | |
return ips | |
def output_info(output, data): | |
""" | |
Prints on screen or output file | |
""" | |
if output is None: | |
print(data.csv) | |
else: | |
try: | |
with open(output, 'w') as f: | |
f.write(data.csv) | |
except PermissionError: | |
print("Error: Can't write in {0} file! Exiting..".format(output)) | |
sys.exit(1) | |
def check_python_version(): | |
if int(sys.version[0]) < 3: | |
print("Error: Python version >= 3 needed! Exiting..") | |
sys.exit(1) | |
if __name__ == '__main__': | |
check_python_version() | |
arguments = docopt(__doc__) | |
csvfile = arguments['<csvfile>'] | |
data = get_data(csvfile) | |
ips = get_ips(data) | |
user = get_user(arguments['--user']) | |
password = get_password(arguments['--password']) | |
vlan_authorization = get_vlan_authorization(ips, user, password) | |
data.append_col(vlan_authorization, header='Vlan Authorization') | |
exclude = exclude_in_get_vlan_from_ports(data) | |
vlan_from_ports = get_vlan_from_ports(ips, user, password, exclude) | |
data.append_col(vlan_from_ports, header='No MacAuth fe.*.* Ports') | |
output_info(arguments['--output'], data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment