├── network_IP.csv ├── requirements.txt ├── Readme.md ├── cdp_diagram.png ├── netmiko_connect.py ├── Output └── cdp_diagram.drawio └── NetDia.py /network_IP.csv: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | ttp 2 | netmiko -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Redderik/NetworkDiagram/HEAD/Readme.md -------------------------------------------------------------------------------- /cdp_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Redderik/NetworkDiagram/HEAD/cdp_diagram.png -------------------------------------------------------------------------------- /netmiko_connect.py: -------------------------------------------------------------------------------- 1 | import getpass 2 | import csv 3 | 4 | def netmiko_connect(): 5 | #Ask input for username. 6 | username = input('Username: ') 7 | #Ask input for user password. Getpass obscures input. 8 | password = getpass.getpass(prompt='Password: ') 9 | #If your switch secret is different that your connection password, uncomment the secret = getpass line. 10 | secret = password 11 | #secret = getpass.getpass(prompt='Secret: ') 12 | #Establish list to hold all IPs for network equipment. 13 | networklist = [] 14 | #Opens network_IP.csv which containes all IPs to connect to and build Flowchart. 15 | with open('network_IP.csv', newline='') as csvfile: 16 | networkcsv = csv.reader(csvfile, delimiter=' ', quotechar='|') 17 | #This is basic validation to ensure it is actually an iPv4 address. It watches for less than 12 integers in the list, and counts for 3 decimal points. 18 | for row in networkcsv: 19 | decimalcount = ''.join(row).count('.') 20 | digitcount = sum(c.isdigit() for c in ''.join(row)) 21 | if decimalcount == 3 and digitcount <= 12: 22 | networklist.append(''.join(row)) 23 | #Establishes list of switches that will be connected to by Netmiko. 24 | switchlist = [] 25 | for i in range(len(networklist)): 26 | switch = { 27 | 'device_type': 'cisco_ios', 28 | 'host': networklist[i], 29 | 'username': username, 30 | 'password': password, 31 | 'secret': password, 32 | } 33 | switchlist.append(switch) 34 | #Returns all our switches 35 | return switchlist -------------------------------------------------------------------------------- /Output/cdp_diagram.drawio: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /NetDia.py: -------------------------------------------------------------------------------- 1 | from ttp import ttp 2 | from netmiko import ConnectHandler 3 | from netmiko_connect import netmiko_connect 4 | 5 | #The TTP Template that spits out our .drawio file after collecting the switch data. 6 | ttptemplate = """ 7 | 8 | hostname='gethostname' 9 | IfsNormalize = { 10 | 'Ge': ['^GigabitEthernet'], 11 | 'Te': ['^TenGigabitEthernet'] 12 | } 13 | 14 | 15 | 16 | Device ID: {{ target.id }} 17 | IP address: {{ target.top_label }} 18 | Platform: {{ target.bottom_label | ORPHRASE }}, Capabilities: {{ ignore(ORPHRASE) }} 19 | Interface: {{ src_label | resuball(IfsNormalize) }}, Port ID (outgoing port): {{ trgt_label | ORPHRASE | resuball(IfsNormalize) }} 20 | {{ source | set("hostname") }} 21 | 22 | 23 | 24 | path = "cdp" 25 | module = "drawio" 26 | node_duplicates = "update" 27 | method = "from_list" 28 | algo = "kk" 29 | 30 | 31 | 32 | """ 33 | switchlist = netmiko_connect() 34 | 35 | #Establishes cdp_neigh_list, which will hold the string data for each switch IP targeted in the CSV file. 36 | cdp_neigh_list = [] 37 | for i in range(len(switchlist)): 38 | connect = switchlist[i] 39 | #Uses netmiko to connect to switch using template assigned in "switchlist" 40 | net_connect = ConnectHandler(**connect) 41 | #Collects the CDP neighbor from the switch. Also adds the hostname/command entered into the switch. This allows 'gethostname' in the ttp_template to work properly. 42 | output = "\n" + net_connect.find_prompt() + "show cdp neigh det \n" + net_connect.send_command("show cdp neigh det") + "\n\n" 43 | cdp_neigh_list.append(output) 44 | #This appends all of our collected data from the switches into ttptemplate. 45 | # 46 | ttp_template = "".join(cdp_neigh_list) + ttptemplate 47 | print(ttp_template) 48 | parser = ttp(template=ttp_template) 49 | parser.parse() 50 | print("Please check output folder for cdp_diagram.drawio file.") 51 | --------------------------------------------------------------------------------