当前位置:   article > 正文

python 解析网络设备配置文件_python获取交换机信息

python获取交换机信息

适用于华为华三交换机防火墙配置文件格式

增加解析汇总 ACL ,方便查看 ACL应用在哪些接口及ACL规则信息

用于定时备份设备配置文件后,对配置文件的变化做出分析。

对运行状态配置和保存配置进行对比,发现差异配置。

支持

文本文件配置文件:vrpcfg.cfg

压缩包配置文件:vrpcfg.zip(自动下载交换机/防火墙配置文件可参考其他文章)

dis cu 命令查询结果配置(自动登录并执行命令,保存回显结果可参考其他文章)

  1. #!/usr/local/python3/bin/python3
  2. # -*- coding: utf8 -*-
  3. import sys, time, os, zipfile
  4. ## 终端显示颜色
  5. if os.name == 'nt': # Windows
  6. import ctypes,sys
  7. STD_OUTPUT_HANDLE = -11
  8. # Windows CMD命令行 字体颜色定义 text colors
  9. 黑字 = 0x00 # black.
  10. FOREGROUND_DARKBLUE = 0x01 # dark blue.
  11. FOREGROUND_DARKGREEN = 0x02 # dark green.
  12. FOREGROUND_DARKSKYBLUE = 0x03 # dark skyblue.
  13. FOREGROUND_DARKRED = 0x04 # dark red.
  14. FOREGROUND_DARKPINK = 0x05 # dark pink.
  15. FOREGROUND_DARKYELLOW = 0x06 # dark yellow.
  16. FOREGROUND_DARKWHITE = 0x07 # dark white.
  17. FOREGROUND_DARKGRAY = 0x08 # dark gray.
  18. FOREGROUND_BLUE = 0x09 # blue.
  19. FOREGROUND_GREEN = 0x0a # green.
  20. FOREGROUND_SKYBLUE = 0x0b # skyblue.
  21. FOREGROUND_RED = 0x0c # red.
  22. FOREGROUND_PINK = 0x0d # pink.
  23. FOREGROUND_YELLOW = 0x0e # yellow.
  24. FOREGROUND_WHITE = 0x0f # white.
  25. std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
  26. def set_cmd_text_color(color, handle=std_out_handle):
  27. Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
  28. return Bool
  29. def resetColor():
  30. set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
  31. def 打印_黑(TEXT, SHOW=1):
  32. if SHOW == 1:
  33. set_cmd_text_color(黑字)
  34. sys.stdout.write(TEXT+'\n')
  35. resetColor()
  36. def 打印_红(TEXT, SHOW=1):
  37. if SHOW == 1:
  38. set_cmd_text_color(FOREGROUND_RED)
  39. sys.stdout.write(TEXT+'\n')
  40. resetColor()
  41. def 打印_绿(TEXT, SHOW=1):
  42. if SHOW == 1:
  43. set_cmd_text_color(FOREGROUND_GREEN)
  44. sys.stdout.write(TEXT+'\n')
  45. resetColor()
  46. def 打印_黄(TEXT, SHOW=1):
  47. if SHOW == 1:
  48. set_cmd_text_color(FOREGROUND_YELLOW)
  49. sys.stdout.write(TEXT+'\n')
  50. resetColor()
  51. def 打印_蓝(TEXT, SHOW=1):
  52. if SHOW == 1:
  53. set_cmd_text_color(FOREGROUND_BLUE)
  54. sys.stdout.write(TEXT+'\n')
  55. resetColor()
  56. def 打印_紫(TEXT, SHOW=1):
  57. if SHOW == 1:
  58. set_cmd_text_color(FOREGROUND_PINK)
  59. sys.stdout.write(TEXT+'\n')
  60. resetColor()
  61. def 打印_青(TEXT, SHOW=1):
  62. if SHOW == 1:
  63. set_cmd_text_color(FOREGROUND_SKYBLUE)
  64. sys.stdout.write(TEXT+'\n')
  65. resetColor()
  66. elif os.name == 'posix': # Linux
  67. def 打印_黑(TEXT, SHOW=1):
  68. if SHOW == 1:
  69. print(f"\033[0;30;1m{TEXT}\033[0m")
  70. def 打印_红(TEXT, SHOW=1):
  71. if SHOW == 1:
  72. print(f"\033[0;31;1m{TEXT}\033[0m")
  73. def 打印_绿(TEXT, SHOW=1):
  74. if SHOW == 1:
  75. print(f"\033[0;32;1m{TEXT}\033[0m")
  76. def 打印_黄(TEXT, SHOW=1):
  77. if SHOW == 1:
  78. print(f"\033[0;33;1m{TEXT}\033[0m")
  79. def 打印_蓝(TEXT, SHOW=1):
  80. if SHOW == 1:
  81. print(f"\033[0;34;1m{TEXT}\033[0m")
  82. def 打印_紫(TEXT, SHOW=1):
  83. if SHOW == 1:
  84. print(f"\033[0;35;1m{TEXT}\033[0m")
  85. def 打印_青(TEXT, SHOW=1):
  86. if SHOW == 1:
  87. print(f"\033[0;36;1m{TEXT}\033[0m")
  88. def 打印_白(TEXT, SHOW=1):
  89. if SHOW == 1:
  90. print(f"\033[0;37;1m{TEXT}\033[0m")
  91. ## 读取网络设备上下载的 startup.cfg 配置文件(文本文件)
  92. ## 读取 dis cu 查询配置保存下来的文本文件
  93. def 读取文本文件内容(FILE_PATH):
  94. L_编码 = ['UTF8', 'GB2312', 'GBK', 'BIG5']
  95. TEXT = None
  96. if os.path.isfile(FILE_PATH):
  97. for 编码 in L_编码:
  98. try:
  99. #print(f"尝试 {编码}")
  100. f = open(FILE_PATH, 'r', encoding=编码)
  101. TEXT = f.read()
  102. except Exception as e:
  103. f.close()
  104. #打印_红(f"{编码}编码读取{FILE_PATH}失败{e}")
  105. else:
  106. f.close()
  107. #print(f"成功 TEXT={TEXT}")
  108. return(TEXT)
  109. else:
  110. 打印_红(f"ERROR 文件 {FILE_PATH} 不存在")
  111. return(TEXT)
  112. 打印_红(f"ERROR 文件 {FILE_PATH} 字符编码解码失败,超出{L_编码}范围")
  113. return(TEXT)
  114. ## 网络设备上下载的配置文件有压缩包类型,如:vrpcfg.zip 里面就一个配置文本文件 vrpcfg.cfg
  115. def 读取ZIP中第一个文件(FILE_PATH):
  116. TEXT = None
  117. if os.path.isfile(FILE_PATH):
  118. if zipfile.is_zipfile(FILE_PATH):
  119. try:
  120. f = zipfile.ZipFile(FILE_PATH)
  121. except Exception as e:
  122. ERROR = f'ERROR zipfile 打开 {FILE_PATH} 失败 {e}'
  123. 打印_红(ERROR)
  124. return()
  125. else:
  126. for i in f.namelist():
  127. #print("ZIP内文件", i)
  128. 文件名, 扩展名 = os.path.splitext(i)
  129. #print(文件名, 扩展名)
  130. if i.endswith('/'):
  131. #print(f"目录 {i}")
  132. pass
  133. else:
  134. #print(f"文件 {i}")
  135. try:
  136. DATA = f.open(i)
  137. except Exception as e:
  138. ERROR = f'ERROR 打开 {i} 失败 {e}'
  139. print(ERROR)
  140. else:
  141. TEXT = DATA.read().decode('GB2312') # 一般是 GB2312
  142. #print("TEXT", TEXT)
  143. break
  144. else:
  145. 打印_红(f"ERROR {FILE_PATH} 不是ZIP格式文件")
  146. else:
  147. 打印_红(f"ERROR {FILE_PATH} 不存在或不是文件")
  148. return(TEXT)
  149. def 前置空格数(TEXT):
  150. N = 0
  151. for i in TEXT:
  152. if i == ' ':
  153. N += 1
  154. else:
  155. break
  156. return(N)
  157. def 配置文本分段分行(TEXT_CONF, SHOW=0):
  158. LL_CONF = []
  159. LL_配置行 = [[j for j in i.split('\r')] for i in TEXT_CONF.split('\n')] # 以换行符分段
  160. #打印_蓝(f"换行符分段 LL_配置行={LL_配置行}", SHOW)
  161. L_配置段 = []
  162. for L_配置行 in LL_配置行:
  163. #打印_红(f"{L_配置行}", SHOW)
  164. for 配置行 in L_配置行:
  165. if 配置行.strip() != '': # 忽略空配置行
  166. if 配置行 == '#': # 遇到分段符号'#'进行分段保存
  167. if L_配置段 != []:
  168. LL_CONF.append(L_配置段)
  169. L_配置段 = [] # 重置为空列表
  170. else:
  171. L_配置段.append(配置行)
  172. #打印_绿(f"{LL_CONF}", SHOW)
  173. #for i in LL_CONF:
  174. # 打印_蓝(f"{i}\n", SHOW)
  175. return(LL_CONF)
  176. def 配置行转配置信息字典(LL_CONF, SHOW=0):
  177. 打印_红("[配置行转配置信息字典]", SHOW)
  178. D_NET_CONF = {'GLOBAL':[]}
  179. for L_配置段 in LL_CONF:
  180. 打印_红(f"L_配置段={L_配置段}", SHOW)
  181. KEY = 'GLOBAL'
  182. for TEXT_配置行 in L_配置段:
  183. 配置行前置空格数 = 前置空格数(TEXT_配置行)
  184. 打印_蓝(f" 前置空格数={配置行前置空格数} TEXT_配置行={TEXT_配置行}", SHOW)
  185. if 配置行前置空格数 == 0:
  186. KEY = TEXT_配置行 # 更新KEY
  187. if KEY not in D_NET_CONF:
  188. D_NET_CONF[KEY] = []
  189. 打印_绿(f" KEY={KEY}", SHOW)
  190. else:
  191. D_NET_CONF[KEY].append(TEXT_配置行)
  192. 打印_绿(f" D_NET_CONF[{KEY}].append({TEXT_配置行})", SHOW)
  193. return(D_NET_CONF)
  194. def 转换网络TXT配置文件(FILE_CONF_PATH, SHOW=0):
  195. D_NET_CONF = {}
  196. TEXT_CONF = 读取文本文件内容(FILE_CONF_PATH)
  197. 打印_黄(f"TEXT_CONF={TEXT_CONF}", SHOW)
  198. if TEXT_CONF != None:
  199. LL_CONF = 配置文本分段分行(TEXT_CONF, SHOW)
  200. D_NET_CONF = 配置行转配置信息字典(LL_CONF, SHOW)
  201. return(D_NET_CONF)
  202. def 转换网络ZIP配置文件(FILE_CONF_PATH, SHOW=0):
  203. D_NET_CONF = {}
  204. TEXT_CONF = 读取ZIP中第一个文件(FILE_CONF_PATH)
  205. 打印_黄(f"TEXT_CONF={TEXT_CONF}", SHOW)
  206. if TEXT_CONF != None:
  207. LL_CONF = 配置文本分段分行(TEXT_CONF, SHOW)
  208. D_NET_CONF = 配置行转配置信息字典(LL_CONF, SHOW)
  209. return(D_NET_CONF)
  210. def 网络配置文件转配置字典(FILE_CONF_PATH, SHOW=0):
  211. FILE_NAME = os.path.basename(FILE_CONF_PATH)
  212. SP = FILE_NAME.split('.')
  213. if SP != [] and SP[-1].lower() == 'zip':
  214. D_NET_CONF = 转换网络ZIP配置文件(FILE_CONF_PATH, SHOW)
  215. else:
  216. D_NET_CONF = 转换网络TXT配置文件(FILE_CONF_PATH, SHOW)
  217. 配置字典再加工(D_NET_CONF, SHOW)
  218. return(D_NET_CONF)
  219. def SAVE_TEXT(TEXT, SAVE_PATH, SHOW=1):
  220. try:
  221. f = open(SAVE_PATH, 'w')
  222. BYTES = f.write(TEXT)
  223. f.close()
  224. except Exception as e:
  225. 打印_红(f"{SAVE_PATH} X", SHOW)
  226. else:
  227. 打印_绿(f"{SAVE_PATH} V {BYTES}", SHOW)
  228. def 对比配置字典(D_OLD, D_NEW, SHOW=1):
  229. TEXT_对比结果 = ''
  230. if D_OLD != {} and D_NEW != {}:
  231. ## 对比新旧配置项是否有新增或删除
  232. P_KEY_D_OLD = set([K for K in D_OLD]) # 旧配置项
  233. P_KEY_D_NEW = set([K for K in D_NEW]) # 新配置项
  234. #print(f"P_KEY_D_OLD={P_KEY_D_OLD}")
  235. #print(f"P_KEY_D_NEW={P_KEY_D_NEW}")
  236. P_KEY_OLD_独有 = P_KEY_D_OLD - P_KEY_D_NEW
  237. P_KEY_NEW_独有 = P_KEY_D_NEW - P_KEY_D_OLD
  238. if P_KEY_OLD_独有 == P_KEY_NEW_独有 == set():
  239. P_KEY_共有 = P_KEY_D_OLD
  240. INFO = f" [新旧配置项] 一致"
  241. TEXT_对比结果 += INFO+'\n'
  242. 打印_绿(INFO, SHOW)
  243. else:
  244. P_KEY_共有 = P_KEY_D_OLD & P_KEY_D_NEW
  245. INFO = f" [新旧配置项] 不同\n P_KEY_OLD_独有 {P_KEY_OLD_独有}\n P_KEY_NEW_独有 {P_KEY_NEW_独有}"
  246. 打印_黄(INFO, SHOW)
  247. TEXT_对比结果 += INFO+'\n'
  248. for KEY in P_KEY_OLD_独有:
  249. INFO = f" [删除配置项] {KEY} 【配置项内容】 {D_OLD[KEY]}"
  250. TEXT_对比结果 += INFO+'\n'
  251. 打印_蓝(INFO, SHOW)
  252. for KEY in P_KEY_NEW_独有:
  253. INFO = f" [新增配置项] {KEY} 【配置项内容】 {D_NEW[KEY]}"
  254. TEXT_对比结果 += INFO+'\n'
  255. 打印_青(INFO, SHOW)
  256. ## 相同配置项对比配置内容是否有变化
  257. for KEY in P_KEY_共有:
  258. if D_OLD[KEY] != D_NEW[KEY]:
  259. P_OLD_配置行 = set(D_OLD[KEY])
  260. P_NEW_配置行 = set(D_NEW[KEY])
  261. if P_OLD_配置行 == P_NEW_配置行:
  262. INFO = f" [配置项] {KEY} 配置行内容有重复"
  263. TEXT_对比结果 += INFO+'\n'
  264. 打印_红(INFO, SHOW)
  265. else:
  266. INFO = f" [变化配置项] {KEY}"
  267. TEXT_对比结果 += INFO+'\n'
  268. 打印_红(INFO, SHOW)
  269. for i in P_OLD_配置行-P_NEW_配置行:
  270. INFO = f" [DEL] {i}"
  271. TEXT_对比结果 += INFO+'\n'
  272. 打印_蓝(INFO, SHOW)
  273. for i in P_NEW_配置行-P_OLD_配置行:
  274. INFO = f" [ADD] {i}"
  275. TEXT_对比结果 += INFO+'\n'
  276. 打印_青(INFO, SHOW)
  277. else:
  278. if D_OLD == {}:
  279. INFO = " D_OLD 无有效内容"
  280. TEXT_对比结果 += INFO+'\n'
  281. 打印_红(INFO)
  282. if D_NEW == {}:
  283. INFO = " D_NEW 无有效内容"
  284. TEXT_对比结果 += INFO+'\n'
  285. 打印_红(INFO)
  286. return(TEXT_对比结果)
  287. def 载入字典形式文本文件(FILE_PATH):
  288. D = {}
  289. if os.path.isfile(FILE_PATH):
  290. try:
  291. f = open(FILE_PATH, 'r')
  292. TEXT = f.read()
  293. f.close()
  294. except Exception as e:
  295. 打印_红(f"读取文件 {FILE_PATH} 失败 {e}")
  296. else:
  297. D = eval(TEXT)
  298. else:
  299. 打印_红(f" ERROR 文件{FILE_PATH}不存在")
  300. return(D)
  301. def 防火墙安全规则转字典(LL_CONF, SHOW=0):
  302. 打印_红("[防火墙安全规则转字典]", SHOW)
  303. D_security_policy = {'security-policy':[]}
  304. KEY = 'security-policy'
  305. for TEXT_配置行 in LL_CONF:
  306. 配置行前置空格数 = 前置空格数(TEXT_配置行)
  307. 打印_蓝(f" 前置空格数={配置行前置空格数} TEXT_配置行={TEXT_配置行}", SHOW)
  308. if 配置行前置空格数 == 1:
  309. KEY = ('security-policy', TEXT_配置行) # 更新KEY
  310. if KEY not in D_security_policy:
  311. D_security_policy[KEY] = []
  312. 打印_绿(f" KEY={KEY}", SHOW)
  313. else:
  314. D_security_policy[KEY].append(TEXT_配置行)
  315. 打印_绿(f" D_security_policy[{KEY}].append({TEXT_配置行})", SHOW)
  316. return(D_security_policy)
  317. def AAA用户信息转字典(LL_CONF, SHOW=0):
  318. 打印_红("[AAA用户信息转字典]", SHOW)
  319. D_AAA = {'aaa':[]}
  320. KEY = 'aaa'
  321. for TEXT_配置行 in LL_CONF:
  322. 配置行前置空格数 = 前置空格数(TEXT_配置行)
  323. 打印_蓝(f" 前置空格数={配置行前置空格数} TEXT_配置行={TEXT_配置行}", SHOW)
  324. if 配置行前置空格数 == 1:
  325. KEY = ('aaa', TEXT_配置行) # 更新KEY
  326. if KEY not in D_AAA:
  327. D_AAA[KEY] = []
  328. 打印_绿(f" KEY={KEY}", SHOW)
  329. else:
  330. D_AAA[KEY].append(TEXT_配置行)
  331. 打印_绿(f" D_AAA[{KEY}].append({TEXT_配置行})", SHOW)
  332. return(D_AAA)
  333. def 配置字典再加工(D_NET_CONF, SHOW=0):
  334. # 删除一些无用信息(注释、查询命令等)
  335. for K in [i for i in D_NET_CONF]:
  336. if K == 'dis cu' or K[0] in ('*', '<', '[', '\x00'):
  337. del D_NET_CONF[K]
  338. 打印_黄(f" 删除 {K}", SHOW)
  339. #else:
  340. # 打印_绿(f" 保留 {K}", SHOW)
  341. # 删除空全局配置
  342. if 'GLOBAL' in D_NET_CONF and D_NET_CONF['GLOBAL'] == []:
  343. del D_NET_CONF['GLOBAL']
  344. 打印_黄(f" 删除 空 D_NET_CONF['GLOBAL']", SHOW)
  345. # 进一步细分,整理防火墙安全策略信息,方便对比策略变化
  346. if 'security-policy' in D_NET_CONF:
  347. #print(D_NET_CONF['security-policy'])
  348. LL_CONF = D_NET_CONF['security-policy']
  349. D_security_policy = 防火墙安全规则转字典(LL_CONF, SHOW)
  350. del D_NET_CONF['security-policy']
  351. for K in D_security_policy:
  352. D_NET_CONF[K] = D_security_policy[K]
  353. # 进一步细分,整理aaa用户信息,方便对比用户信息变化
  354. if 'aaa' in D_NET_CONF:
  355. LL_CONF = D_NET_CONF['aaa']
  356. D_AAA = AAA用户信息转字典(LL_CONF, SHOW)
  357. del D_NET_CONF['aaa']
  358. for K in D_AAA:
  359. D_NET_CONF[K] = D_AAA[K]
  360. def TEST_换行符分段():
  361. TEXT = 'aa\r\nbb\ncc\rdd\n\ne\r\rf\n g g \n'
  362. print(TEXT)
  363. print("\\r ", TEXT.split('\r'))
  364. print("\\n ", TEXT.split('\n'))
  365. print("\\r\\n", TEXT.split('\r\n'))
  366. print("分段 先\\r 再\\n")
  367. Lr = [i for i in TEXT.split('\r')]
  368. print("Lr", Lr)
  369. LL = [[j for j in i.split('\n')] for i in TEXT.split('\r')]
  370. print("LL", LL)
  371. print()
  372. print("分段 先\\n 再\\r")
  373. Ln = [i for i in TEXT.split('\n')]
  374. print("Ln", Ln)
  375. LL = [[j for j in i.split('\r')] for i in TEXT.split('\n')]
  376. print("LL", LL)
  377. for i in LL:
  378. for j in i:
  379. print(j)
  380. for i in LL:
  381. for j in i:
  382. if j != '':
  383. print(j)
  384. ## ACL 解析
  385. def 配置字典转ACL信息字典(D_NET_CONF, SHOW=0):
  386. D_ACL_INFO = {}
  387. #{
  388. # (ACL_ID, ACL_NAME):{
  389. # 'RULE':['RULE 明细', ],
  390. # '规则应用信息:[{
  391. # 'TYPE': '接口或其他'
  392. # 'CONF': '规则应用配置,进出方向等'
  393. # 'DESC': '备注',
  394. # }, {}]
  395. # },
  396. # (ACL_ID, ACL_NAME):{},
  397. # },
  398. #}
  399. for KEY in D_NET_CONF:
  400. if type(KEY) == str:
  401. SP_KEY = KEY.split()
  402. if SP_KEY != [] and SP_KEY[0] == 'acl':
  403. 打印_紫(f"ACL : {KEY}", SHOW)
  404. if 'number' in SP_KEY:
  405. ACL_ID = SP_KEY[SP_KEY.index('number')+1]
  406. else:
  407. ACL_ID = ''
  408. if 'name' in SP_KEY:
  409. ACL_NAME = SP_KEY[SP_KEY.index('name')+1]
  410. else:
  411. ACL_NAME = ''
  412. D_ACL_INFO[(ACL_ID,ACL_NAME)] = {'RULE':D_NET_CONF[KEY], '规则应用信息':[]}
  413. #打印_红(f"D_ACL_INFO={D_ACL_INFO}")
  414. ## 解析ACL在接口上的应用信息
  415. for KEY in D_NET_CONF:
  416. 打印_紫(f"KEY={KEY}", SHOW)
  417. if type(KEY) == str:
  418. SP_KEY = KEY.split()
  419. if SP_KEY != [] and SP_KEY[0] == 'interface':
  420. 打印_蓝(f"interface : {KEY}", SHOW)
  421. for 接口配置行 in D_NET_CONF[KEY]:
  422. L_SP_接口配置 = 接口配置行.split()
  423. if 'packet-filter' in L_SP_接口配置 or 'traffic-filter' in L_SP_接口配置:
  424. for ACL_ID,ACL_NAME in D_ACL_INFO:
  425. if ACL_ID in L_SP_接口配置 or ACL_NAME in L_SP_接口配置:
  426. 打印_蓝(f"{(ACL_ID,ACL_NAME)}", SHOW)
  427. L_接口备注 = [i for i in D_NET_CONF[KEY] if 'description' in i.split()]
  428. if L_接口备注 != []:
  429. 接口备注 = L_接口备注[0].strip()
  430. else:
  431. 接口备注 = '无'
  432. 打印_绿(f" {KEY:22s} {接口配置行:33s} | {接口备注}", SHOW)
  433. D_ACL_INFO[(ACL_ID,ACL_NAME)]['规则应用信息'].append({'TYPE':KEY, 'CONF':接口配置行.strip(), 'DESC':接口备注})
  434. for RULE_TEXT in D_ACL_INFO[(ACL_ID,ACL_NAME)]['RULE']:
  435. 打印_青(f" {RULE_TEXT}", SHOW)
  436. elif SP_KEY != [] and SP_KEY[0] == 'acl':
  437. 打印_红(f"ACL : {KEY} PASS", SHOW)
  438. else:
  439. ## ACL 应用在顶级配置上
  440. 打印_黄(f"Other : {KEY}", SHOW)
  441. for ACL_ID,ACL_NAME in D_ACL_INFO:
  442. if ACL_ID in SP_KEY or ACL_NAME in SP_KEY:
  443. if D_NET_CONF[KEY] == []:
  444. CONF = ''
  445. else:
  446. CONF = str(D_NET_CONF[KEY])
  447. DESC = ''
  448. 打印_蓝(f"{(ACL_ID,ACL_NAME)}", SHOW)
  449. 打印_绿(f" {KEY:22s} {CONF:33s} |", SHOW)
  450. D_ACL_INFO[(ACL_ID,ACL_NAME)]['规则应用信息'].append({'TYPE':KEY, 'CONF':CONF, 'DESC':DESC})
  451. else:
  452. for 配置行 in D_NET_CONF[KEY]:
  453. L_SP_配置行 = 配置行.split()
  454. if ACL_ID in L_SP_配置行 or ACL_NAME in L_SP_配置行:
  455. CONF = 配置行.strip()
  456. DESC = ''
  457. 打印_蓝(f"{(ACL_ID,ACL_NAME)}", SHOW)
  458. 打印_绿(f" {KEY:22s} {CONF:33s} |", SHOW)
  459. D_ACL_INFO[(ACL_ID,ACL_NAME)]['规则应用信息'].append({'TYPE':KEY, 'CONF':CONF, 'DESC':DESC})
  460. return(D_ACL_INFO)
  461. def 配置文件转ACL信息字典(FILE_CONF_PATH, SHOW=0):
  462. D_NET_CONF = 网络配置文件转配置字典(FILE_CONF_PATH, SHOW)
  463. D_ACL_INFO = 配置字典转ACL信息字典(D_NET_CONF, SHOW)
  464. return(D_ACL_INFO)
  465. def ACL信息字典转文本(D_ACL_INFO):
  466. 换行符 = '\n'
  467. TEXT = ''
  468. for K in D_ACL_INFO:
  469. TEXT += f" ACL={K}{换行符}"
  470. TEXT += f" 规则应用信息{换行符}"
  471. for 规则应用信息 in D_ACL_INFO[K]['规则应用信息']:
  472. TEXT += f" {规则应用信息}{换行符}"
  473. TEXT += f" ACL规则{换行符}"
  474. for RULE in D_ACL_INFO[K]['RULE']:
  475. TEXT += f" {RULE}{换行符}"
  476. TEXT += 换行符
  477. return(TEXT)
  478. ## 测试
  479. if __name__ == '__main__':
  480. def 测试_文本配置文件(FILE_CONF_PATH):
  481. D_NET_CONF = 转换网络TXT配置文件(FILE_CONF_PATH, SHOW=1)
  482. for K in D_NET_CONF:
  483. 打印_红(f"{K}")
  484. 打印_蓝(f" {D_NET_CONF[K]}")
  485. #FILE_CONF_PATH = 'E:\\TEST\\A.down_run_txt'
  486. #测试_文本配置文件(FILE_CONF_PATH)
  487. def 测试_压缩包配置文件(FILE_CONF_PATH):
  488. D_NET_CONF = 转换网络ZIP配置文件(FILE_CONF_PATH, SHOW=1)
  489. for K in D_NET_CONF:
  490. 打印_红(f"{K}")
  491. 打印_蓝(f" {D_NET_CONF[K]}")
  492. #FILE_CONF_PATH = 'E:\\TEST\\A_vrpcfg.zip'
  493. #测试_压缩包配置文件(FILE_CONF_PATH)
  494. def 测试_对比两个配置文件差异(FILE_CONF_OLD_PATH, FILE_CONF_NEW_PATH):
  495. D_NET_CONF_OLD = 网络配置文件转配置字典(FILE_CONF_OLD_PATH, SHOW=0)
  496. D_NET_CONF_NEW = 网络配置文件转配置字典(FILE_CONF_NEW_PATH, SHOW=0)
  497. 打印_蓝(f"OLD {FILE_CONF_OLD_PATH}")
  498. 打印_青(f"NEW {FILE_CONF_NEW_PATH}")
  499. TEXT_对比结果 = 对比配置字典(D_NET_CONF_OLD, D_NET_CONF_NEW, SHOW=1)
  500. 打印_紫(TEXT_对比结果)
  501. #FILE_CONF_OLD_PATH = 'E:\\TEST\\A_vrpcfg.zip'
  502. #FILE_CONF_NEW_PATH = 'E:\\TEST\\A.down_run_txt'
  503. #测试_对比两个配置文件差异(FILE_CONF_OLD_PATH, FILE_CONF_NEW_PATH)
  504. def 测试_解析配置后保存为字典类型文本文件(FILE_CONF_PATH, SAVE_PATH):
  505. D_NET_CONF = 网络配置文件转配置字典(FILE_CONF_PATH, SHOW=1)
  506. SAVE_TEXT(f"{D_NET_CONF}", SAVE_PATH, SHOW=1)
  507. #FILE_CONF_PATH = 'E:\\TEST\\A_vrpcfg.zip'
  508. #SAVE_PATH = 'E:\\TEST\\A_vrpcfg.zip.cfg_dict_txt'
  509. #测试_解析配置后保存为字典类型文本文件(FILE_CONF_PATH, SAVE_PATH)
  510. def 测试_读取字典形式保存的配置文件(FILE_PATH):
  511. D_NET_CONF = 载入字典形式文本文件(FILE_PATH)
  512. for K in D_NET_CONF:
  513. 打印_红(f"{K}")
  514. 打印_蓝(f" {D_NET_CONF[K]}")
  515. #FILE_PATH = 'E:\\TEST\\A_vrpcfg.zip.cfg_dict_txt'
  516. #测试_读取字典形式保存的配置文件(FILE_PATH)
  517. ## 交换机 ACL 信息整理
  518. #FILE_CONF_PATH = f'E:\\TEST\\A_vrpcfg.zip'
  519. #D_ACL_INFO = 配置文件转ACL信息字典(FILE_CONF_PATH, SHOW=0)
  520. #TEXT = ACL信息字典转文本(D_ACL_INFO)
  521. #打印_黄(TEXT)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/156771
推荐阅读
相关标签
  

闽ICP备14008679号