Python Forum

Full Version: hello everyone, I am attempting to extract lldp neighbors from Cisco switches->excel
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good evening everyone
I am attempting to extract the output from multiple cisco switches about 60 of them and put the data into an excel spreadsheet with multiple sheets. I have the code working for a single switch and the output going to excel. I was wondering if anyone would be able to assist or help point me to some full examples of something similar. I am very new to programming so sorry if my code looks bad but I am trying to learn. Any help would be much appreciated.  

from netmiko import ConnectHandler
from datetime import datetime
import os
import json
import pandas as pd
import argparse

SW1 = {
    'device_type': 'cisco_ios',
    'host': '192.168.1.2',
    'username':'cisco',
    'password': 'cisco',
}
SW2 = {
    'device_type': 'cisco_ios',
    'host': '192.168.1.3',
    'username':'cisco',
    'password': 'cisco',
}
def main():
    os.environ["NET_TEXTFSM"] = "./ntc-templates/templates"
# list of all devices
    all_devices = [SW1, SW2]
# commands
    for a_device in all_devices:
        net_connect = ConnectHandler(**a_device)
    lldp_neighbor = net_connect.send_command("show lldp neighbor", use_textfsm=True)
    print(lldp_neighbor)
    print(len(lldp_neighbor))
    for line in lldp_neighbor:
        print(json.dumps(line, indent=6))
    lldp_data = {'NEIGHBOR': [entry['neighbor'] for entry in lldp_neighbor],
                 'LOCAL_INTERFACE': [entry['local_interface'] for entry in lldp_neighbor],
                 'CAPABILITIES': [entry['capabilities'] for entry in lldp_neighbor],
                 'NEIGHBOR_INTERFACE': [entry['neighbor_interface'] for entry in lldp_neighbor],
                 }     
    df = pd.DataFrame(lldp_data, columns=list(lldp_data.keys()))
    print(df.head())
    df.to_excel('LLDP_Neighbor.xlsx')
# Standard call to the main() function.
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Script Description",
                                     epilog="Usage: ' python ccom' ")
    arguments = parser.parse_args()
    main()
I use a csv file for getting device info. filename is devices.csv

myhostname,192.168.1.1,platformID,IOS-XE,username,userpass,enablepass

# read file and get values
    with open("devices.csv","r") as a_file:
        for line in a_file:
          stripped_line = line.strip()
          hostname = stripped_line.split(',')[0]
          ip = stripped_line.split(',')[1]
          platform =  stripped_line.split(',')[2]
          IOS =  stripped_line.split(',')[3]
          username = stripped_line.split(',')[4]
          password = stripped_line.split(',')[5]
          enablepass = stripped_line.split(',')[6]
          print ip
          print platform
          print IOS

          # Create instance of SSHClient object
          remote_conn_pre = paramiko.SSHClient()

          # Automatically add untrusted hosts (make sure okay for security policy in your environment)
          remote_conn_pre.set_missing_host_key_policy(
               paramiko.AutoAddPolicy())

          # initiate SSH connection
          remote_conn_pre.connect(ip, username=username, password=password, look_for_keys=False, allow_agent=False)
          print "SSH connection established to %s" % ip