Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Task: Network Automation using Python

All routers have been pre-configured with IPv4 addressing and OSPFv2. To access the python interpreter,
minimize the GNS3 application and open the terminal from the desktop. You may use ATOM as an editor for
creating your scripts. All scripts should be stored in the pre-created folder "/root/Scripts". As a network
engineer for INE, you have been assigned to automate the following tasks:

1. Create a script with the file name as Lab1.py which prints the following:

2. show ip interface brief

3. show ip route | include 192.168.1.0

4. The password is "cisco"

This lab belongs to Rohit's class

5. Create a script with the file name as Lab2.py. Create a string variables named ccie1 which has the string
value as CCIE EI. Create a string variables named ccie2 which has the string value as CCIE SP. Create a
string variables named ccie3 which has the string value as CCIE SEC. Create a string variables
named ccie4 which has the string value as CCIE DC. Create a integer type variables named ei which has
the integer value as 40000. Create a integer type variables named sp which has the integer value
as 10000. Create a integer type variables named sec which has the integer value as 15000. Create a
integer type variables named dc which has the integer value as 5000. Print "There are 40000 CCIE EI
certified people in the world". Print "There are 10000 CCIE SP certified people in the world". Print
"There are 15000 CCIE SEC certified people in the world". Print "There are 5000 CCIE DC certified
people in the world". Print "There are 70000 CCIE's in the world". Use the Format string feature to
achieve this.

6. Create a script with the file name as Lab3.py. Create a string variables named old_ip_address1 which
has the string value as 192.168.100.100. Create a string variables named old_ip_address2 which has
the string value as 192.168.200.200. Use the split and replace built-in methods and replace all
occurrences of .100 with .254. Use the split and replace built-in methods and replace the first
occurrence of 200 with .254. Once the IP addresses are modified, print the following:

7. The old IP addresses are 192.168.1.100 and 192.168.2.200

The new IP addresses are ['192', '168', 254, '254'] and ['192', '168', 254, '200']

8. Create a script with the file name as Lab4.py. Create a dictionary named dictionary1 with the following
keys and values hostname": "R1", "mgmt-ip": "10.1.1.1", "username": "rohit", "password": "cisco".
Create another dictionary named dictionary2 with the following keys and values hostname": "R2",
"mgmt-ip": "10.1.1.2", "username": "rohit", "password": "cisco". Create another dictionary
named interfaces_r1 with the following keys and values "interface1": "G1", "int1_ip_address":
"192.168.1.1", "interface2": "G2", "int2_ip_address": "192.168.2.1". Create another dictionary
named interfaces_r2 with the following keys and values "interface1": "G1", "int1_ip_address":
"192.168.3.1", "interface2": "G2", "int2_ip_address": "192.168.4.1". Ensure the output below
matches:

9. print(json.dumps(data_center, indent=10))

10. [

11. {

12. "hostname": "R1",

13. "mgmt-ip": "10.1.1.1",

14. "username": "rohit",

15. "password": "cisco",

16. "interfaces": {

17. "interface1": "G1",

18. "int1_ip_address": "192.168.1.1",

19. "interface2": "G2",

20. "int2_ip_address": "192.168.2.1"

21. }
22. },

23. {

24. "hostname": "R2",

25. "mgmt-ip": "10.1.1.2",

26. "username": "rohit",

27. "password": "cisco",

28. "interfaces": {

29. "interface1": "G1",

30. "int1_ip_address": "192.168.3.1",

31. "interface2": "G2",

32. "int2_ip_address": "192.168.4.1"

33. }

34. }

35. Create a script with the file name as Lab5.py that loads the config from a file on R19 and R20. Create the
configuration file and save it in the /root/Scripts folder. Router username is rohit and the password
is admin. The file name and configuration to be loaded are given below:

R19.cfg

router bgp 100

neighbor 150.20.20.20 remote-as 100

neighbor 150.20.20.20 update-source Loopback0

end

write mem

R20.cfg

router bgp 100

neighbor 150.20.20.20 remote-as 100

neighbor 150.20.20.20 update-source Loopback0


end

write mem

36. Create a python script called as Lab6.py in the /root/Scripts. This script must accomplish the
following:

o Ask for an input from the user, for example, “Enter the xe router show command you want to
display?”
o Ask for an input from the user, for example, “Enter the xr router show command you want to
display?”
o Ask for an input from the user, for example, “Enter the firewall show command you want to
display?”
o Connect to each device (R19, R20, R21, XR11, ASA)
o Print the routing table
o Ensure the script asks for a username and password to connect to the devices.
o Router username is rohit and the password is admin.
o You must use a For Loop and a Function to achieve this task.
37. Create a python script called as Lab7.py in the /root/Scripts. This script must accomplish the
following:

o Ask for the Username


o Ask for the Password
o Connect to each device in the topology and backup the configurations in the /root/Scripts folder.
o The backup must be stored in the format of hostname.cfg

Solutions:

Lab1.py

print("show ip interface brief")

print("show ip route | include 192.168.1.0")

print('The password is "cisco"')

print("This lab belongs to Rohit's class")

Lab2.py

ccie1 = "CCIE EI"

ccie2 = "CCIE SP"

ccie3 = "CCIE SEC"

ccie4 = "CCIE DC"

ei = 40000
sp = 10000

sec = 15000

dc = 5000

total_ei = f"There are {ei} {ccie1} certified people in the world"

total_sp = f"There are {sp} {ccie2} certified people in the world"

total_sec = f"There are {sec} {ccie3} certified people in the world"

total_dc = f"There are {dc} {ccie4} certified people in the world"

print(total_ei)

print("\n")

print(total_sp)

print("\n")

print(total_sec)

print("\n")

print(total_dc)

print("\n")

total_ccies = ei + sp + sec + dc

print(f"There are {total_ccies} CCIE's in the world")

Lab3.py

old_ip_address1 = "192.168.100.100"

old_ip_address2 = "192.168.200.200"

print(f"The old IP addresses are {old_ip_address1} and {old_ip_address2}")

temp_ip_address1 = old_ip_address1.replace("100", "254", 2)

temp_ip_address2 = old_ip_address2.replace("200", "254", 1)

new_ip_address1 = temp_ip_address1.split(".")

new_ip_address2 = temp_ip_address2.split(".")

print(f"The new IP addresses are {new_ip_address1} and {new_ip_address2}")

Lab4.py

dict1 = {"hostname": "R1", "mgmt-ip": "10.1.1.1", "username": "rohit", "password": "cisco"}

dict2 = {"hostname": "R2", "mgmt-ip": "10.1.1.2", "username": "rohit", "password": "cisco"}


interfaces_r1 = {"interface1": "G1", "int1_ip_address": "192.168.1.1", "interface2": "G2", "int2_ip_address": "192.168.2.1"}

interfaces_r2 = {"interface1": "G1", "int1_ip_address": "192.168.3.1", "interface2": "G2", "int2_ip_address": "192.168.4.1"}

data_center = [dict1, dict2]

data_center[0]["interfaces"] = interfaces_r1

data_center[1]["interfaces"] = interfaces_r2

import json

print(json.dumps(data_center, indent=10))

Lab5.py

Go to the terminal on the ubuntu machine and create the configuration files named R19.cfg and R20.cfg and paste the
respective configs in them. You may use atom or vim to create the file. Save it in the /roots/Scripts folder.

from netmiko import ConnectHandler

r19 = ConnectHandler(ip="192.168.122.19", username="rohit", password="admin", secret="admin", device_type="cisco_xe")

r20 = ConnectHandler(ip="192.168.122.20", username="rohit", password="admin", secret="admin", device_type="cisco_xe")

r19.is_alive()

r20.is_alive()

r19.find_prompt()

r20.find_prompt()

r19.enable()

r20.enable()

r19_configs = r19.send_config_from_file("R19.cfg")

r20_configs = r20.send_config_from_file("R20.cfg")

print("*****Loading Configs on R19*****\n")

print(r19_configs)

print("*****Loading Configs on R20*****\n")

print(r20_configs)

r19.disconnect()

r20.disconnect()

Lab6.py
from netmiko import ConnectHandler

import getpass

import readline

xe_rtr_cmd = input("Enter the xe router show command that you want to display? ")

xr_rtr_cmd = input("Enter the xr router show command that you want to display? ")

fw_cmd = input("Enter the firewall show command that you want to display? ")

def fw_get_cmd(dev):

dev.enable()

dev.send_command("terminal page 0")

output = dev.send_command(fw_cmd)

hostname = dev.send_command("show run | in hostname").split()[1]

print(f"*****This is the output of {hostname}*****\n")

print(output)

dev.disconnect()

def xr_get_cmd(dev):

dev.enable()

dev.send_command("terminal page 0")

output = dev.send_command(xr_rtr_cmd)

hostname = dev.send_command("show run hostname").split()[6]

print(f"*****This is the output of {hostname}*****\n")

print(output)

dev.disconnect()

def xe_get_cmd(dev):

dev.enable()

dev.send_command("terminal length 0")

output = dev.send_command(xe_rtr_cmd)

hostname = dev.send_command("show run | in hostname").split()[1]

print(f"*****This is the output of {hostname}*****\n")

print(output)

dev.disconnect()

uname = input("Username: ")


passwd = getpass.getpass("Password: ")

r19 = ConnectHandler(ip="192.168.122.19", username=uname, password=passwd, device_type="cisco_xe")

r20 = ConnectHandler(ip="192.168.122.20", username=uname, password=passwd, device_type="cisco_xe")

r21 = ConnectHandler(ip="192.168.122.21", username=uname, password=passwd, device_type="cisco_xe")

xr11 = ConnectHandler(ip="192.168.122.11", username=uname, password=passwd, device_type="cisco_xr")

fw = ConnectHandler(ip="192.168.122.2", username=uname, password=passwd, device_type="cisco_asa")

devices = [r19, r20, r21, xr11, fw]

for item in devices:

if item.device_type == 'cisco_asa':

fw_get_cmd(item)

print("\n")

elif item.device_type == 'cisco_xr':

xr_get_cmd(item)

print("\n")

else:

xe_get_cmd(item)

print("\n")

Lab7.py

from netmiko import ConnectHandler

import os

import getpass

import readline

os.chdir("/root/Scripts")

uname = input("Username: ")

passwd = getpass.getpass("Password: ")

r19 = ConnectHandler(ip="192.168.122.19", username=uname, password=passwd, device_type="cisco_xe")

r20 = ConnectHandler(ip="192.168.122.20", username=uname, password=passwd, device_type="cisco_xe")

r21 = ConnectHandler(ip="192.168.122.21", username=uname, password=passwd, device_type="cisco_xe")

xr11 = ConnectHandler(ip='192.168.122.11', username=uname, password=passwd, device_type='cisco_xr')

fw1 = ConnectHandler(ip="192.168.122.2", username=uname, password=passwd, device_type="cisco_asa")


devices = [r19, r20, r21, xr11, fw1]

def devices_configs(dev):

if dev.device_type == "cisco_xr":

hostname = dev.send_command("sh run hostname").split()[6]

else:

hostname = dev.send_command("show run | in hostname").split()[1]

if dev.device_type == "cisco_asa":

dev.send_command("terminal page 0")

else:

dev.send_command("terminal length 0")

print(f"*****Connecting to device: {hostname}*****\n")

dev.send_command("wr mem")

print(f"*****Backing configs of device: {hostname}*****\n")

temp = dev.send_command("show run")

with open(f"{hostname}.cfg", "w") as dev_file:

dev_file.write(temp)

dev.disconnect()

for item in devices:

devices_configs(item)

You might also like