1. 程式人生 > 其它 >網路裝置配置對比

網路裝置配置對比

#!/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