from infoblox_client.exceptions import InfobloxBadWAPICredential, InfobloxTimeoutError, InfobloxConnectionError, infobloxConfigException def get_iblox_connection(creds): """Function to create an Infoblox client connector with passed in creds dictionary""" opts = { 'host': creds['api_host'], 'username': creds['username'], 'password': creds['password'], 'max_retries': creds['max_retries'], 'wapi_version': creds['wapi_version']} try: connection = Connector(opts) except InfobloxConfigException as error: print(error) exit() else: return connection def search_dns(conn, search_field: str, search_val: str): """Function to search IPAM for DNS records""" record_dict = {} rec_types = ['net'] for rec in rec_types: records = search_record(conn, rec, search_field, search_val) if records is not None and len(records) != 0: record_dict[rec] = records return record_dict def search_record(connection, rec_type: str, search_field: str, search_val: str): """Function to get a List of records from IPAM""" if 'ptr' in rec_type: kwargs = { 'return_fields': [ 'default', 'name', 'ipv4addr', 'last_queried', ], 'max_results': 2000, } else: kwargs = { 'return_fields': [ 'default', 'last_queried', ], 'max_results': 2000, } if 'ipv4address' in search_field: kwargs = { 'max_results': 2000, } # Connect to grid and search for IP address based on network and broadcast addresses try: # Failes on this get_object result = connection.get_object('ipv4address', {'ip_address>': str(search_val.split(',')[0]), 'ip_address<': str(search_val.split(',')[1])}, **kwargs) except InfobloxConnectionError as con_error: print('\nThere was a timeout error, try searching a smaller network') print(search_val, '\n') # print(con_error) exit() except InfobloxBadWAPICredential as cred_error: print(cred_error) print('InfobloxBadWAPICredential') exit() except InfobloxTimeoutError as timeout_error: print(timeout_error) print('InfobloxTimeoutError') exit() else: result = connection.get_object('record:' + rec_type, {search_field: search_val}, **kwargs) return result login_creds = { 'username': 'user', 'password': 'password', 'api_host': '192.168.1.1', 'max_retries': 1, 'wapi_version': '2.11.3', } def main(): connection = get_iblox_connection(login_creds) results = search_dns(connection, 'ipv4address', '10.83.56.0,10.83.63.255') if __name__ == '__main__': main()