Tag: Python

  • Python script to check if VDB databases are updated in Cisco FMC

    To check if VDB databases are updated in Cisco FMC and print the results to an Excel file using Python, you can use the following approach:

    1.Access Cisco FMC through API: Use the Cisco FMC API to retrieve the VDB database information. You can find more details about the API in the Cisco FMC API documentation.

    import requests
    import json

    fmc_feeds_url = “https://10.10.10.10/api/fmc_feeds/access”
    fmc_headers = {‘content-type’: ‘application/json’}
    fmc_auth = (‘admin’, ‘YourPassword’) # replace ‘YourPassword’ with your actual password

    response = requests.get(fmc_feeds_url, headers=fmc_headers, auth=fmc_auth, verify=False)
    feeds = json.loads(response.text)

    2.Parse the API response: Extract the VDB database information from the API response.

    vdb_databases = [feed for feed in feeds[‘items’] if feed[‘name’].startswith(‘VDB’)]

    3.Create an Excel file and print the results: Use a library like pandas to create an Excel file and print the results.

    import pandas as pd

    df = pd.DataFrame(vdb_databases)
    df.to_excel(‘VDB_databases.xlsx’, index=False)

  • Python Script to Configure Multiple Cisco Devices

    This script to configure multiple cisco devices , we need to put all IP addresses in the IPAdrresslist.txt file and change cmd1 and cmd2 of the script with required config commands

    from future import print_function
    from netmiko import ConnectHandler

    import sys
    import time
    import select
    import paramiko
    import re
    platform = ‘cisco_ios’
    username = ‘XXXX’
    password = ‘XXXX’

    ip_add_file = open(‘ips.txt’,’r’)

    for host in ip_add_file:
    try:
    device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
    output = device.send_config_set([cmd1′,’cmd2′])
    print(output)

    except Exception:
    print(“Unable to connect”)

    
    

  • Python Script to Execute Show Commands in Multiple Devices and Save the Output to a Text File

    The script requires two text files, put your device IP addresses in IPAddressList.txt and create another blank file named Command_Output.txt in the application directory .

    Prerequisites

    • Python 3
    • Paramiko

    from future import print_function
    from netmiko import ConnectHandler
    import os
    import sys
    import time
    import select
    import paramiko
    import re

    fd = open(‘r’\home\user\Command_Output.txt’,’w’)
    old_stdout = sys.stdout
    sys.stdout = fd
    platform = ‘cisco_ios’
    username = ‘XXXX’
    password = ‘XXXX’
    ip_add_file = open(r’\home\user\IPAddressList.txt’,’r’)

    for host in ip_add_file:
    #host = host.strip()
    device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
    output = device.send_command(‘sh int trunk’)
    print(output)

    fd.close()