当网络中的设备越来越多,以及随着自动化的到来,我们就要考虑使用自动化脚本来配置网络设备。比如网络中有100台设备需要配置相同或者相类似的东西(vlan/route)就不适合人工的每台的去配置。

实验目的:使用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
网络工程师的Pyhon实战

实验步骤:
1, 三台路由器的预配置,配置f0/0的接口IP以及能够SSH

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

2, 在本地PC上写脚本如下

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如下

**************************** 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。

另外一个案例,我们需要一次查寻网络中多个设备的版本号
运行的输出结果如下:

******************************************************************************
show ns version
    NetScaler NS11.1: Build 56.120.nc, Date: Feb 21 2018, 23:25:02  
 Done
Primary-pvg1-extlb-a> 
******************************************************************************

******************************************************************************
show ns version
    NetScaler NS11.1: Build 56.120.nc, Date: Feb 21 2018, 23:25:02  
 Done
Primary-pvg1-extlb-e> 
******************************************************************************

******************************************************************************
show ns version
    NetScaler NS11.1: Build 56.120.nc, Date: Feb 21 2018, 23:25:02  
 Done
Primary-pvg1-extlb-g> 
******************************************************************************

There are 3 active NetScalers

实现的代码如下:

from netmiko import ConnectHandler
pvg1_extlb_a={
    'device_type':'netscaler',
    'host':'pvg1-extlb-a',
    'username':'test',
    'password':'',
}
pvg1_extlb_e={
    'device_type':'netscaler',
    'host':'pvg1-extlb-e',
    'username':'test',
    'password':'',
}
pvg1_extlb_g={
    'device_type':'netscaler',
    'host':'pvg1-extlb-g',
    'username':'test',
    'password':'',
}

all_devices=[pvg1_extlb_a,pvg1_extlb_e,pvg1_extlb_g]
for devices in all_devices:
    net_connect = ConnectHandler(**devices)
    config_commands = ['show ns version']
    output1 = net_connect.send_config_set(config_commands)
    print('******************************************************************************')
    print(output1)
    print('******************************************************************************')
    print('\n' * 2)
print('There are',len(all_devices),'active NetScalers')