Network Automation Using Python
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:
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:
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. {
16. "interfaces": {
21. }
22. },
23. {
28. "interfaces": {
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
end
write mem
R20.cfg
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:
Solutions:
Lab1.py
Lab2.py
ei = 40000
sp = 10000
sec = 15000
dc = 5000
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
Lab3.py
old_ip_address1 = "192.168.100.100"
old_ip_address2 = "192.168.200.200"
new_ip_address1 = temp_ip_address1.split(".")
new_ip_address2 = temp_ip_address2.split(".")
Lab4.py
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.
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(r19_configs)
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()
output = dev.send_command(fw_cmd)
print(output)
dev.disconnect()
def xr_get_cmd(dev):
dev.enable()
output = dev.send_command(xr_rtr_cmd)
print(output)
dev.disconnect()
def xe_get_cmd(dev):
dev.enable()
output = dev.send_command(xe_rtr_cmd)
print(output)
dev.disconnect()
if item.device_type == 'cisco_asa':
fw_get_cmd(item)
print("\n")
xr_get_cmd(item)
print("\n")
else:
xe_get_cmd(item)
print("\n")
Lab7.py
import os
import getpass
import readline
os.chdir("/root/Scripts")
def devices_configs(dev):
if dev.device_type == "cisco_xr":
else:
if dev.device_type == "cisco_asa":
else:
dev.send_command("wr mem")
dev_file.write(temp)
dev.disconnect()
devices_configs(item)