网络工程师的Pyhont实战

当网络中的设备愈来愈多,以及随着自动化的到来,咱们就要考虑使用自动化脚原本配置网络设备。好比网络中有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
网络工程师的Pyhont实战网络

实验步骤:
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

相关文章
相关标签/搜索