Python Forum
hello everyone, I am attempting to extract lldp neighbors from Cisco switches->excel
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hello everyone, I am attempting to extract lldp neighbors from Cisco switches->excel
#1
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()
Larz60+ write Nov-13-2020, 05:19 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Thank You
Reply
#2
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
buran write Nov-14-2020, 08:26 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  netmiko: print out specific line matches with 'Cisco IOS Software' in sh ver output sabrina 1 3,364 Nov-28-2022, 10:05 AM
Last Post: carstenlp
  Python script for HP switches bcroombs 1 2,418 Jan-25-2022, 08:04 PM
Last Post: Larz60+
  identify un-managed switches in network c5244714 0 1,716 Apr-12-2020, 01:12 PM
Last Post: c5244714
  GNS3 telnet from bash terminal to virtual cisco router marienbad 2 3,693 Feb-24-2019, 07:55 AM
Last Post: marienbad
  safe output in a file for all switches in a network henry1077 0 2,720 Jul-26-2018, 01:05 PM
Last Post: henry1077
  Python script for show commands-CISCO Devices babbarsher 1 12,935 Dec-13-2017, 11:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020