4.根据CMDB 接口提供json 数据,进行拆分处理ip地址,正则匹配出现 环境对应环境的网段 如下: 54 docker-dev 58 docker-test 46 虚拟机-dev 47 虚拟机-test import json import re import urllib class Operation_IP_Private(object): def VirtualMachine_Dev(self, url, parameter01, parameter02, parameter03): ####初始化4个参数传入### with open('/root/VirtualMachine_Dev.txt', 'w') as f: #####打开本地文件,已 ip_list = [] ip_rege = re.compile(r"(?<![\d.])%s\.%s\.%s[6-6]\.(.*)" % (parameter01, parameter02, parameter03)) weatherHtml = urllib.urlopen(url) weatherHtml1 = weatherHtml.read() weatherJSON = json.loads(weatherHtml1) for i in weatherJSON["data"]: abc = i['privateIpAddress'] + "\n" ip_list.append(abc) for b in ip_list: if re.search(ip_rege, b): f.write(b) def VirtualMachine_test(self, url, parameter01, parameter02, parameter03): with open('/root/VirtualMachine_test.txt', 'w') as f: ip_list = [] ip_rege = re.compile(r"(?<![\d.])%s\.%s\.%s[7-7]\.(.*)" % (parameter01, parameter02, parameter03)) weatherHtml = urllib.urlopen(url) weatherHtml1 = weatherHtml.read() weatherJSON = json.loads(weatherHtml1) for i in weatherJSON["data"]: abc = i['privateIpAddress'] + "\n" ip_list.append(abc) for b in ip_list: if re.search(ip_rege, b): f.write(b) def Docker_Dev_IpAddress(self, url, parameter01, parameter02, parameter03): with open('/root/Docker_Dev_IpAddress.txt', 'w') as f: ip_list = [] ip_rege = re.compile(r"(?<![\d.])%s\.%s\.%s[4-4]\.(.*)" % (parameter01, parameter02, parameter03)) weatherHtml = urllib.urlopen(url) weatherHtml1 = weatherHtml.read() weatherJSON = json.loads(weatherHtml1) for i in weatherJSON["data"]: abc = i['privateIpAddress'] + "\n" ip_list.append(abc) for b in ip_list: if re.search(ip_rege, b): f.write(b) def Docker_Test_IpAddress(self, url, parameter01, parameter02, parameter03): with open('/root/VirtualMachine_test.txt', 'w') as f: ip_list = [] ip_rege = re.compile(r"(?<![\d.])%s\.%s\.%s[8-8]\.(.*)" % (parameter01, parameter02, parameter03)) weatherHtml = urllib.urlopen(url) weatherHtml1 = weatherHtml.read() weatherJSON = json.loads(weatherHtml1) for i in weatherJSON["data"]: abc = i['privateIpAddress'] + "\n" ip_list.append(abc) for b in ip_list: if re.search(ip_rege, b): f.write(b) IP_address = Operation_IP_Private() IP_address.VirtualMachine_Dev("http://172.17.37.93:1949/vserver/select", "192", "168", "4") IP_address.VirtualMachine_test("http://172.17.37.93:1949/vserver/select", "192", "168", "4") IP_address.Docker_Dev_IpAddress("http://172.17.37.93:1949/vserver/select", "192", "168", "5") P_address.Docker_Test_IpAddress("http://172.17.37.93:1949/vserver/select", "192", "168", "5")
原文:http://blog.51cto.com/breaklinux/2147819