Entradas

Mostrando entradas de octubre, 2021

Python | Crear archivo .csv con datos obtenidos mediante libreria NAPALM

import napalm # Create a hosts file to include all the target IPs/hostnames that will be SSH'd. f = open ("/home/eve/hosts") # Create a .csv file and define all of the columns matching with NAPALM's "get_facts" output. csv_columns=("FQDN;HOSTNAME;MODEL;OS VERSION;SERIAL NUMBER;UPTIME;VENDOR\n") file= open("napalm.csv","w") file.write(csv_columns) # Create a python loop where it perform the following actions: # 1. Create the "host" variable where we will extract each line of code on every iteration using strip() # 2. Define the NAPALM's driver + SSH Username and Password # 3. Connect to each target host (via SSH) using device.open() # 4. Perform NAPAM's "get_facts" on the target host in order to get general data from the Cisco device in an "structured" way. This data is stored as a "Dictionary". # 5. Take each key data (fqdn, hostname, serial_number...) from "g

Python + NAPALM | Script para mostrar inventario de Hosts con su serial number

ARCHIVO DE HOSTS nano /home/eve/hosts 192.168.1.100 192.168.1.17 SCRIPT import napalm import json f = open ("/home/eve/hosts") for line in f: host = line.strip() driver = napalm.get_network_driver('ios') device = driver(hostname=host, username='cisco', password='cisco') device.open() getfacts = device.get_facts() get_serialnumber = getfacts["serial_number"] print (host + " " + get_serialnumber) device.close() RESULTADO (OUTPUT)  eve@Linux-Desktop:~$ python3 test1_6.py 192.168.1.100         FTX0945W0MY 192.168.1.17         FTX0945W0MY

Python + NAPALM | Imprimir en pantalla el estado de una interfaz de un Router Cisco

Normalmente cuando ejecutamos un script básico usando la libreria NAPALM y con la función "get_interfaces" obtendriamos un output del equivalente al comando "show ip int brief" de una forma más prográmatica como podemos observar a continuación: eve@Linux-Desktop:~$ python3 test1.py {'FastEthernet0/0': {'description': 'Fa0/0-Test',                      'is_enabled': True,                      'is_up': True,                      'last_flapped': -1.0,                      'mac_address': 'C2:02:0F:F7:00:00',                      'mtu': 1500,                      'speed': 10},  'FastEthernet0/1': {'description': 'Fa0/1-Test',                      'is_enabled': False,                      'is_up': False,                      'last_flapped': -1.0,                      'mac_address': 'C2:02:0F:F7:00:01',                      '

EEM Script en Cisco IOS

EEM es una funcionalidad dentro del código Cisco IOS que podriamos definirlo como "Old School network automation". Nos permite realizar acciones tales como ejecutar comando en base a un event log o condición. Ejemplo básico de EEM Script en un dispostivo Cisco IOS El script de a continuación realizará lo siguiente -> cada dia a las 20:00 de la tarde ejecutará el comando "shutdown" en la interfaz Fa0/0 event manager applet shut_port trap   <-- nombre del script "shut_port" event timer cron cron-entry "0 20 * * *"   <-- ejecutar script a las 20:00h de la tarde action 0001 cli command "enable" action 0002 cli command "term exec prompt timestamp" action 0003 cli command "term length 0" action 0010 syslog msg "shutting port FastEthernet0/0 down" action 0030 cli command "config t" action 0040 cli command "int Fa0/0" action 0050 cli command "shutdown" <-