网络设备配置批量修改

1、直接上代码:

#-*- coding: utf-8 -*-
import re

def config_replace(target_file,new_config):
    f_write=open(target_file,"a+",encoding='utf-8')
    f_write.write(new_config)

    
def main():
    f=open("D:/config_replace/old_paramater","r").read()
    old_paramater=f.split('\n')
    f=open("D:/config_replace/new_paramater","r").read()
    new_paramater=f.split('\n')
    #print(old_paramater)
    #print(new_paramater)
    target="D:/config_replace/"
    f=open("D:/config_replace/canshu","r").read()
    variable=f.split('\n')
    for k in variable:
        directory=target+k
        old_config=open(directory,"r",encoding='utf-8').read()
        config_r=re.split(r"([ \n_])",old_config)
        #print(config_r)
        cc=len(old_paramater)
        for i in config_r:
            for j in range(cc):
                if i==old_paramater[j]:
                    m=config_r.index(i)
                    config_r[m]=new_paramater[j]
        new_config=''.join(config_r)
    #print(new_config)
        target_file="D:/config_replace/new_config/"+k
        config_replace(target_file,new_config)
    #print(old_config)
    #new_config=old_config
    #for i in range(cc):
    #    new_config=new_config.replace(old_paramater[i],new_paramater[i])
    #print(new_config)
    
main()

2、要修改的变量,修改后的变量,修改的配置列表清单

canshu的文本:

m1-202-ma02-ilo-asw-h5130-01.bj.txt
m1-202-ma05-ilo-asw-h5130-01.bj.txt
m1-202-ma08-ilo-asw-h5130-01.bj.txt
m1-202-ma11-ilo-asw-h5130-01.bj.txt
m1-202-ma14-ilo-asw-h5130-01.bj.txt
m1-202-ma17-ilo-asw-h5130-01.bj.txt
m1-202-ma38-ilo-asw-h5130-01.bj.txt
m1-202-ma21-ilo-asw-h5130-01.bj.txt
m1-202-ma24-ilo-asw-h5130-01.bj.txt
m1-202-ma27-ilo-asw-h5130-01.bj.txt
m1-202-ma30-ilo-asw-h5130-01.bj.txt
m1-202-ma33-ilo-asw-h5130-01.bj.txt
m1-202-ma36-ilo-asw-h5130-01.bj.txt

修改前变量:

 service-type telnet ssh

 修改后的变量:

 service-type ssh

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值