发布时间:2019-08-06 09:14:09编辑:auto阅读(1475)
起因
历史遗留问题导致CMDB (配置管理数据库) 数据错误,内网机器200多台,逐一核对显然太不现实; (浪费人力);
2.解决问题思路 ;
读取docker 平台ip接口,和cmdb 平台接口生成文件,进行文件对比,)注意:文件对比前需要排序下 linux 平台推荐 (sort命令);
3.读取接口代码;
# -*- coding: utf-8 -*- import requests import json url = " ####cmdb 接口地址 ### docker_data = requests.get(url) data=docker_data.text json_dict = json.loads(data) for i in json_dict.get("data"): cmdb_private_ip = i.get("private_ip") if cmdb_private_ip: try: f = open('/tmp/private_ip', 'ab+') f.write(cmdb_private_ip) finally: if f: f.close()
4.文件对比脚本;
#!/usr/bin/python # -*- coding: utf-8 -*- import sys import difflib try: docker_ipaddress= sys.argv[1] cmdb_ipaddress = sys.argv[2] except IndexError,e: print("文件不存在,%s") % str(e) sys.exit() def readline(filename): try: f = open(filename,"rb") lines = f.read().splitlines() f.close() return lines except ImportError as error: print("文件为空%s") % str(error) sys.exit() docker_lines = readline(docker_ipaddress) cmdb_lines = readline(cmdb_ipaddress) d = difflib.HtmlDiff() data = d.make_file(docker_lines,cmdb_lines) with open("/data/upload/docker_cmdb_file_ip") as file: file.write(data) 脚本执行命令; [root@devops]# python chechk_pas.py docker_new.txt cmdb_docker_new.txt
5.文件结果展示;
上一篇: python中if else流程判断
下一篇: 想学运维,想学python,想踏入IT圈
47494
45794
36793
34324
28968
25598
24443
19611
19111
17632
5466°
6048°
5570°
5638°
6573°
5376°
5378°
5884°
5855°
7170°