Network engineer's Python practice

When there are more and more devices in the network, and with the advent of automation, we should consider using automat...

When there are more and more devices in the network, and with the advent of automation, we should consider using automation scripts to configure network devices. For example, if there are 100 devices in the network that need to be configured with the same or similar things (vlan/route), it is not suitable for manual configuration of each device.

Experiment purpose: to use Python (netmiko) script to configure Cisco router and improve automation capability
Experiment content: three routers (R1, R2, R3) configure each router with three loopback IP 1.1.1.12.2.2.23.3.3.3
Experimental topology:
The environment of GN3, R1/R2/R3, is bridged to my local notebook
R1-f0/0:192.168.3.111
R2-f0/0:192.168.3.112
R3-f0/0:192.168.3.113

Experimental steps:
1. Pre configuration of three routers, configuration of f0/0 interface IP and SSH capability

username cisco privilege 15 password 0 cisco line vty 0 15 login local ip domain name cisco.com crypto key generate rsa

2. Write the script on the local PC as follows

from netmiko import ConnectHandler #Import connecthandler module from netmiko module #Define three dictionaries of router to be configured R1={ 'device_type':'cisco_ios', 'ip':'192.168.3.111', 'username':'cisco', 'password':'cisco', } R2={ 'device_type':'cisco_ios', 'ip':'192.168.3.112', 'username':'cisco', 'password':'cisco', } R3={ 'device_type':'cisco_ios', 'ip':'192.168.3.113', 'username':'cisco', 'password':'cisco', } all_devices=[R1,R2,R3] #Put all the devices on a list count=1 #Define a starting number for later calls for devices in all_devices: #Cycle all equipment net_connect=ConnectHandler(**devices) output1=net_connect.send_command('show ip int bri | in up') #Check the IP of the device before configuration print('**************************** configuring ','R'+str(count),'*********************************') print(output1) print('******************************************************************************') for i in range (0,3): #The second layer circulates to configure IP for the three loopback ports of the device config_commands=['int loop'+str(i),'ip add ... 255.255.255.255'.format(i+1)]#format string output2=net_connect.send_config_set(config_commands) print(output2) print('*************************** checking ','R'+str(count),'configuration ***********************') output3 = net_connect.send_command('show ip int bri | in up') #Finally, check the configuration print(output3) print('******************************************************************************') print('\n'*4) #4 lines are empty in the equipment room count += 1 #Start number increase per cycle

3. Run the following script output

**************************** configuring R1 ********************************* FastEthernet0/0 192.168.3.111 YES manual up up Loopback0 unassigned YES TFTP up up Loopback1 unassigned YES TFTP up up Loopback2 unassigned YES TFTP up up Loopback3 unassigned YES manual up up ****************************************************************************** config term Enter configuration commands, one per line. End with CNTL/Z. R1(config)#int loop0 R1(config-if)#ip add 1.1.1.1 255.255.255.255 R1(config-if)#end R1# config term Enter configuration commands, one per line. End with CNTL/Z. R1(config)#int loop1 R1(config-if)#ip add 2.2.2.2 255.255.255.255 R1(config-if)#end R1# config term Enter configuration commands, one per line. End with CNTL/Z. R1(config)#int loop2 R1(config-if)#ip add 3.3.3.3 255.255.255.255 R1(config-if)#end R1# *************************** checking R1 configuration *********************** FastEthernet0/0 192.168.3.111 YES manual up up Loopback0 1.1.1.1 YES manual up up Loopback1 2.2.2.2 YES manual up up Loopback2 3.3.3.3 YES manual up up Loopback3 unassigned YES manual up up ****************************************************************************** **************************** configuring R2 ********************************* FastEthernet0/0 192.168.3.112 YES manual up up Loopback0 unassigned YES TFTP up up Loopback1 unassigned YES TFTP up up Loopback2 unassigned YES TFTP up up ****************************************************************************** config term Enter configuration commands, one per line. End with CNTL/Z. R2(config)#int loop0 R2(config-if)#ip add 1.1.1.1 255.255.255.255 R2(config-if)#end R2# config term Enter configuration commands, one per line. End with CNTL/Z. R2(config)#int loop1 R2(config-if)#ip add 2.2.2.2 255.255.255.255 R2(config-if)#end R2# config term Enter configuration commands, one per line. End with CNTL/Z. R2(config)#int loop2 R2(config-if)#ip add 3.3.3.3 255.255.255.255 R2(config-if)#end R2# *************************** checking R2 configuration *********************** FastEthernet0/0 192.168.3.112 YES manual up up Loopback0 1.1.1.1 YES manual up up Loopback1 2.2.2.2 YES manual up up Loopback2 3.3.3.3 YES manual up up ****************************************************************************** **************************** configuring R3 ********************************* FastEthernet0/0 192.168.3.113 YES manual up up Loopback0 unassigned YES TFTP up up Loopback1 unassigned YES TFTP up up Loopback2 unassigned YES TFTP up up ****************************************************************************** config term Enter configuration commands, one per line. End with CNTL/Z. R3(config)#int loop0 R3(config-if)#ip add 1.1.1.1 255.255.255.255 R3(config-if)#end R3# config term Enter configuration commands, one per line. End with CNTL/Z. R3(config)#int loop1 R3(config-if)#ip add 2.2.2.2 255.255.255.255 R3(config-if)#end R3# config term Enter configuration commands, one per line. End with CNTL/Z. R3(config)#int loop2 R3(config-if)#ip add 3.3.3.3 255.255.255.255 R3(config-if)#end R3# *************************** checking R3 configuration *********************** FastEthernet0/0 192.168.3.113 YES manual up up Loopback0 1.1.1.1 YES manual up up Loopback1 2.2.2.2 YES manual up up Loopback2 3.3.3.3 YES manual up up ****************************************************************************** Process finished with exit code 0

In real work, it may not be necessary to configure multiple loopback ports for each device. Originally, it was intended to configure multiple VLANs, but GNS router does not support adding VLANs.

3 February 2020, 11:15 | Views: 3489

Add new comment

For adding a comment, please log in
or create account

0 comments