當網絡中的設備愈來愈多,以及隨着自動化的到來,咱們就要考慮使用自動化腳原本配置網絡設備。好比網絡中有100臺設備須要配置相同或者相相似的東西(vlan/route)就不適合人工的每臺的去配置。ios
實驗目的:使用Python(netmiko)腳本配置Cisco路由器,提升自動化能力
實驗內容:三臺路由器(R1,R2,R3)給每臺路由器配置三個環回口 IP 1.1.1.1 2.2.2.2 3.3.3.3
實驗拓撲:
使用的GN3搭的環境,R1/R2/R3都橋接到我本地的筆記本上
R1-f0/0:192.168.3.111
R2-f0/0:192.168.3.112
R3-f0/0:192.168.3.113
網絡
實驗步驟:
1, 三臺路由器的預配置,配置f0/0的接口IP以及可以SSHdom
username cisco privilege 15 password 0 cisco line vty 0 15 login local ip domain name cisco.com crypto key generate rsa
2, 在本地PC上寫腳本以下ide
from netmiko import ConnectHandler #從netmiko模塊中導入ConnectHander模塊 #定義三個須要配置的router的字典 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] #把全部設備放到一個列表裏面 count=1 #定義一個起始數方便後面調用 for devices in all_devices: #循環全部設備 net_connect=ConnectHandler(**devices) output1=net_connect.send_command('show ip int bri | in up') #未配置前檢查一下設備的IP print('**************************** configuring ','R'+str(count),'*********************************') print(output1) print('******************************************************************************') for i in range (0,3): #第二層循環,爲設備的三個環回口配置IP config_commands=['int loop'+str(i),'ip add {0}.{0}.{0}.{0} 255.255.255.255'.format(i+1)]#格式化字符串 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') #最後檢查一下配置的狀況 print(output3) print('******************************************************************************') print('\n'*4) #設備舍設備間空了4行 count += 1 #起始數每次循環的增長
3,運行的腳本output以下oop
**************************** 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
現實工做中可能不須要給每一個設備配置多個環回口,原本是想配置多個VLAN的可是GNS的router不支持添加vlan。3d