1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| import requests import json
CINameValue = [] url = "http://192.168.10.5:8080/api/cmdb/ci" tocken = "470CD3BC-31AB-4044-A022-BCD53C3E4CC7" input_data = """<?xml version="1.0" encoding="UTF-8"?> <API version="1.0" locale="en"> <citype> <name>Switch</name> <criterias> <criteria> <parameter> <name compOperator="CONTAINS">CI Name</name> <value>*</value> </parameter> </criteria> </criterias> <returnFields> <name>CI Name</name> </returnFields> <range> <startindex>1</startindex> <limit>100000</limit> </range> </citype> </API>"""
argsCIName = { 'OPERATION_NAME': 'read', 'format': 'json', "TECHNICIAN_KEY": tocken, "INPUT_DATA": input_data } response = requests.post(url, params=argsCIName)
response = json.loads(response.text)
for CINamevalueData in response['API']['response']['operation']['Details']['field-values']['record']: CINameDataList = CINamevalueData.values() CINameValue.append(CINameDataList[0])
for CINAME in CINameValue: input_data = '''<?xml version="1.0" encoding="UTF-8"?> <API version="1.0"> <citype> <name>Switch</name> <criterias> <criteria> <parameter> <name compOperator="IS">CI Name</name> <value>%s</value> </parameter> </criteria> </criterias> <returnFields> <name>IP Address</name> <name>CI Name</name> <name>Mac Address</name> <name>Department</name> <name>Business Impact</name> <name>Site</name> <name>CI Type</name> </returnFields> <sortFields sortOrder="desc"> <name>Product Name</name> </sortFields> </citype> </API>''' % CINAME
argsDetailed = { 'OPERATION_NAME': 'read', 'format': 'json', "TECHNICIAN_KEY": tocken, "INPUT_DATA": input_data } response = requests.post(url=url, params=argsDetailed) print response.text response = json.loads(response.text) IP = "" Mac = "" try: totalSubRecords = response['API']['response']['operation']['Details']['field-values']['record']['value'][5]['totalSubRecords'] SubRecord = response['API']['response']['operation']['Details']['field-values']['record']['value'][5]['SubRecord']
except TypeError: SubRecord = None totalSubRecords = None DataKey = ["CI Name", "CI Type", "Site", "Business Impact", "Department", "Mac Address", "IP Address"] DataValue = response['API']['response']['operation']['Details']['field-values']['record']['value'][0:5]
if type(SubRecord) == list: for SubRecordValue in SubRecord: IP = IP + "|" + SubRecordValue['value'][1] Mac = Mac + "|" + SubRecordValue['value'][0] DataValue.append(Mac.strip("|")) DataValue.append(IP.strip("|")) elif type(SubRecord) == dict: IP = SubRecord['value'][1] Mac = SubRecord['value'][0] DataValue.append(Mac) DataValue.append(IP) else: IP = None Mac = None DataValue.append(Mac) DataValue.append(IP) i = 0 Dict = {} while i < 7: Dict[DataKey[i]] = DataValue[i] i += 1 DataValueJSON = json.dumps(Dict, ensure_ascii=False, encoding='utf-8') print DataValueJSON
|