++++++++++++++++++++++++++++++++++++++++++++++++++++ Using the Netmiko libray for Automation ++++++++++++++++++++++++++++++++++++++++++++++++++++ ************************************************************************* Lab 1 - Using the Netmiko Library - Configuring Initialization Commands ************************************************************************* from netmiko import ConnectHandler ABC = { 'device_type': 'cisco_ios', 'host': '172.25.1.1', 'username': 'khawar', 'password': 'cisco' } myssh = ConnectHandler(**ABC) TTT = [ 'banner motd #Authorized KBITS.LIVE Users Only#', 'no ip domain-lookup', 'line con 0', ' logg sync', 'no exec-timeout' ] myssh.send_config_set(TTT) output1 = myssh.send_command('sh runn | inc banner') output2 = myssh.send_command('show run | sec line') output3 = myssh.send_command('show ip eigrp neighbor') print(output1) print(output2) print(output3) Notes: ------- send_config_set(List of Commands) - Sends multiple global config commands specified in the list variable send_command(Command) - Sends a single Privilege Exec command send_config_from_file(Configfilename) ************************************************************************* Lab 2 - Creating Interfaces using Netmiko - Interactive ************************************************************************* from netmiko import ConnectHandler print(70*'*') print('Connection Information') print(70*'*') HOST = input("Enter IP Address of the Device: ") USER = input("Enter your SSH username: ") PASS = input("Enter your SSH Password: ") print(70*'*') print('Interface Information') print(70*'*') Interface = input('\nWhat Interface would you like to configure [E0/0,Loop99] : ') Ipaddr = input('Specify the IP Address : ') SMask = input('Specify the Subnet mask : ') Interface_cmd = 'Interface ' + Interface Ipaddr_cmd = 'ip address ' + Ipaddr + ' ' + SMask ABC = { 'device_type': 'cisco_ios', 'host': HOST, 'username': USER, 'password': PASS } myssh = ConnectHandler(**ABC) config_commands = [ Interface_cmd , Ipaddr_cmd, 'no shut'] myssh.send_config_set(config_commands) output = myssh.send_command('show ip int brief') print(output) ************************************************************************* Lab 3 - Collecting Interface Information from a list of devices ************************************************************************* from netmiko import ConnectHandler with open('Devices.txt') as devfile: for IP in devfile: ABC = { 'device_type': 'cisco_ios', 'ip': IP, 'username': 'khawar', 'password': 'cisco' } myssh = ConnectHandler(**ABC) print('Connecting to ' + IP + '-' * 79) output = myssh.send_command('sh ip int brief') print(output) print('-' * 79) myssh.disconnect() ************************************************************************* Lab 4 - Backing Up a Router Configuration ************************************************************************* from netmiko import ConnectHandler ABC = { 'device_type': 'cisco_ios', 'ip': '172.25.1.1', 'username': 'khawar', 'password': 'cisco' } myssh = ConnectHandler(**ABC) shhost = myssh.send_command('show run | i hostname') hostname=shhost.split() print("Backing up " + hostname[1]) backupfilename = hostname[1] + '-Backup.txt' shrun = myssh.send_command('show run') backupfile = open(backupfilename, "w") backupfile.write(shrun) backupfile.close() print(hostname[1] + ' Backed up successfully') myssh.disconnect() ************************************************************************* Lab 5 - Backing Up Multiple Router Configurations ************************************************************************* from netmiko import ConnectHandler USER = input('Enter the Username: ') PASS = input('Enter the Password: ') with open('Devices.txt') as devfile: for IP in devfile: ABC = { 'device_type': 'cisco_ios', 'ip': IP, 'username': USER, 'password': PASS } myssh = ConnectHandler(**ABC) shhost = myssh.send_command('show run | i hostname') hostname=shhost.split() print("Backing up " + hostname[1]) backupfilename = hostname[1] + '-Backup.txt' shrun = myssh.send_command('show run') backupfile = open(backupfilename, "w") backupfile.write(shrun) backupfile.close() ************************************************************************* Lab 6 - Configuring Routers using a pre-configured Config file ************************************************************************* from netmiko import ConnectHandler with open('Devices.txt') as devfile: for IP in devfile: ABC = { 'device_type': 'cisco_ios', 'ip': IP, 'username': 'khawar', 'password': 'cisco' } myssh = ConnectHandler(**ABC) shhost = myssh.send_command('show run | i hostname') hostname=shhost.split() print("Configuring " + hostname[1]) Configfilename = hostname[1] + '.txt' myssh.send_config_from_file(Configfilename) print(hostname[1] + ' Configured') myssh.disconnect() ************************************************************************* Lab 7 - Creating Multiple Users ************************************************************************* from netmiko import ConnectHandler HOST = input("Enter IP Address of the Device: ") user = input("Enter your SSH username: ") PASS = input("Enter your SSH Password: ") ABC = { 'device_type': 'cisco_ios', 'host': HOST, 'username': user, 'password': PASS } myssh = ConnectHandler(**ABC) user_num = input('How many users need to be created: ') user_num = int(user_num) while user_num > 0: NEW_USER = input('Specify the Username to be create: ') NEW_PASS = input('Specify the Password for the User: ') USER_CMD = 'Username ' + NEW_USER + ' privileg 15 password ' + NEW_PASS config_commands = [USER_CMD] myssh.send_config_set(config_commands) user_num -=1 print('---------------------------------\n') shuser = myssh.send_command('show running-config | inc username') print(shuser) print('Users created successfully') ***************************************************************** Lab 8 - Configuring EIGRP on Multiple Routers - Interactive ***************************************************************** from netmiko import ConnectHandler router_num = input("How many routers would you like to configure: ") router_num = int(router_num) while router_num > 0: hostip = input('Router IP: ') USER = input('SSH Username: ') PASS = input('SSH Password: ') ABC= { 'device_type': 'cisco_ios', 'ip': hostip, 'username': USER, 'password': PASS } myssh = ConnectHandler(**ABC) shhost = myssh.send_command('show run | i hostname') hostname=shhost.split() print("Configuring " + hostname[1]) eigrpas = input('EIGRP AS #: ') routereigrp = 'router eigrp ' + eigrpas sir=myssh.send_command('show ip route | i C ') print(sir) network_num = input('How many networks would you like to enable in EIGRP: ') network_num = int(network_num) while network_num > 0: network_i = input('Please specify the network to enable: ') network_e = 'network ' + network_i config_commands = [routereigrp, network_e] output = myssh.send_config_set(config_commands) print(output) network_num -=1 print('Router \"' + hostname[1] + '\" configured') print('-'*79) router_num -=1 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Configuring Routers using EEM ++++++++++++++++++++++++++++++++++++++++++++++++++++ **************************************************************************** Lab 1 - Configure an EEM script to bring back an interface from Shutdown **************************************************************************** ----- R1 ----- event manager applet EEM-1 event syslog pattern "Interface Ethernet0/0, changed state to administratively down" action 10 cli command "en" action 20 cli command "config t" action 30 cli command "interface E0/0" action 40 cli command "no shut" action 50 syslog msg "Please don't shut this interface. It is required to be up at all times" **************************************************************************** Lab 2 - Configure an EEM script to send an e-mail if EIGRP goes down **************************************************************************** ----- R1 ----- event manager applet EEM-2 event syslog pattern "EIGRP-IPv4 111: Neighbor 192.1.12.2 (Ethernet0/1)" action 10 cli command "en" action 20 mail server "192.1.12.25" to "kb@kbits.live" from "R1@kbits.live" subject "EIGRP on R1 is down" body "Please take a look at R1 as EIGRP has gone down" **************************************************************************** Lab 3 - Configure an EEM script to skip running a command (Reload) **************************************************************************** ----- R1 ----- event manager applet EEM-3 event cli pattern "reload" sync yes action 10 syslog msg "Please don't reload the device" ++++++++++++++++++++++++++++++++++++++++++++++++++++ Configuring a Guestshell ++++++++++++++++++++++++++++++++++++++++++++++++++++ **************************************************************************** Lab 1 - Creating a Guestshell **************************************************************************** Interface virtualportgroup 0 ip address 192.168.0.1 255.255.255.0 no shut ! iox ! app-hosting appid guestshell app-vnic gateway0 virtualportgroup 0 guest-interface 0 guest-ipaddress 192.168.0.2 netmask 255.255.255.0 exit app-default-gateway 192.168.0.1 guest-interface 0 end ! guestshell enable (in priv. exec.) Verification: --------------- Show app-hosting list **************************************************************************** Lab 2 - Creating a Python Script in Guestshell **************************************************************************** guestshell ! vi EEM.py import cli import sys cmds = ["show ip interface brief", "show ip ospf neighbor", "show ip route"] with open("/bootflash/guest-share/TEST.txt", "a+") as logfile: for cmd in cmds: output = cli.execute(cmd) logfile.write(output) ESC :wq **************************************************************************** Lab 3 - Creating an EEM Script to run a Python Script **************************************************************************** Interface Loopback10 ip address 192.168.1.1 255.255.255.0 ! track 1 interface loopback10 line-protocol event manager applet EEM-4 event track 1 state down ratelimit 5 action 1.0 cli command "en" action 2.0 cli command "guestshell run python3 EEM.py" action 3.0 syslog msg "Data Capture Complete" more bootflash:guest-share/TEST.txt