网络设备配置对比

#!/usr/bin
# _*_ coding: UTF-8 _*_
# Copyright (c) 2021 GengYu.All rights reserved
# @Create by gengyu
# @Create Time :2021/12/12
# @File Name : diff
# 打包命令 pyinstaller -F package\diff
"""

"""
__author__ = 'Administrator'
import paramiko
import time
import re,os,sys
import difflib
import doctest

# 设备信息
ip = '192.168.56.177'
username = 'python'
password = 'Huawei@123'

# 定义函数获取当前配置
def get_config(ip,username,password):
    ssh = paramiko.client.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
    ssh.connect(hostname=ip, port=22, username=username, password=password)
    print(ip + 'login successfully')

    cli = ssh.invoke_shell()
    cli.send('N\n')
    time.sleep(0.5)
    cli.send('screen-length 0 temporary\n')
    time.sleep(0.5)
    cli.send('display cu\n')
    time.sleep(2)
    dis_cu = cli.recv(999999).decode()
    ssh.close()
    return dis_cu


#定义函数ssh_config,将脚本写入设备
def ssh_config(file,ip,username,password):
    ssh = paramiko.client.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
    ssh.connect(hostname=ip,port=22,username=username,password=password)
    print(ip + 'ssh config login successfully')

    cli = ssh.invoke_shell()
    cli.send('N\n')
    time.sleep(0.5)
    cli.send('screen-length 0 temporary\n')
    time.sleep(0.5)

    f = open(file,'r')
    config_list = f.readlines()
    for line in config_list:
        cli.send(line)
        time.sleep(0.5)

    dis_this = cli.recv(999999).decode()
    # print(dis_this)
    ssh.close()
# 调用get_config赋值给output
output = get_config(ip,username,password)

#数据处理,使用正则表达式仅获取配置信息
config = re.findall(r'(<HUAWEI>display cu[\d\D]+<HUAWEI>$)',output)

# 保存配置到本地文件file1
base_path = os.getcwd()
with open(base_path + r'\file1','w') as f:
    f.writelines(config[0])

# 调用ssh_config,将netconf.txt配置写入设备
ssh_config('netconf.txt',ip,username,password)

# 再次读取配置,保存到本地为file2
output = get_config(ip,username,password)
config = re.findall(r'(<HUAWEI>display cu[\d\D]+<HUAWEI>$)',output)

with open(base_path + r'\file2','w') as f:
    f.writelines(config[0])

# 配置对比
d = difflib.HtmlDiff()
#定义函数读取文件
def read_file(filename):
    try:
        with open(filename,'r') as f:
            return f.readlines()
    except IOError:
        print('%s未找到该文件' % filename)
        sys.exit(1)

# 定义函数compare_files,做配置对比,并保存文件为result.html
def compare_files(file1,file2,out_file):
    file1_content = read_file(file1)
    file2_content = read_file(file2)
    d = difflib.HtmlDiff()
    result = d.make_file(file1_content,file2_content)
    with open(base_path + r'\result.html','w') as f:
        f.writelines(result)
    print()
# 调用compare_files
compare_files(base_path + r'\file1',base_path + r'\file2',base_path + r'\result.html')





# if __name__ == "__main__":
#     doctest.testmod()
#netconf.txt
sys
aaa
local-user netconf password irreversible-cipher Huawei@123
local-user netconf service-type ssh
local-user netconf level 3
q
ssh user netconf authentication-type password
ssh user netconf service-type snetconf
snetconf server enable
netconf
protocol inbound ssh port 830
commit
quit

交换机配置:

int g1/0/0

un sh

int vlani 1

ip add 192.168.56.177

q

stel s e

user-i v 4

auth aaa

pro in ssh

u p l 3

 

ssh user python

ssh user python auth password

ssh user python ser stel

 

aaa

local-user python password irreversible-cipher Huawei@123

local-user python service-type ssh

local-user python user-group manage-ug

commit

 

posted @ 2021-12-15 22:43  火山的爱  阅读(151)  评论(0)    收藏  举报