Reply
Highlighted

Get all static hosts with name and extattr with a python script over Rest API

denpiepe
Techie
Posts: 6
5305     0

Hej all,

I want to use the Rest API to get a List of all static hosts with a python script. Then I want to parse the host and get the extattr of every host with a "n" at the fourth space in the name.

Actually i go to every network and then I pull every ip_v4_address to get further information. But in large networks I get an error "Client.Ibap.Proto [...] Result set too large (>1000)".

 

Do you have any idea to improve the method?

 

My Code Snippet:

for network in networks:
    req_params={'network': network['network'],
                'network_view': network['network_view'],
                'status': 'USED',
                '_return_fields': '',
                '_max_results': str(100000)}
    req_cookies = {'ibapauth': ibapauth_cookie}
	
    networkcount = (networkcount+1);
    sys.stdout.write("Analyze Network: #%d \r" %networkcount )
    sys.stdout.flush()
	
    r = requests.get(url + 'ipv4address',
                     params=req_params,
                     cookies=req_cookies,
                     verify=valid_cert)

    if r.status_code != requests.codes.ok:
        print r.text
        #exit_msg = 'Error {} finding ipv4address objects: {}'
        #sys.exit(exit_msg.format(r.status_code, r.reason))


    # Convert the network CIDR into network address and mask.
    netaddr, prefix = network['network'].split('/')
    prefix = int(prefix)

    # Get values of the desired extensible attributes, if present.
    country = site = region = zone = ''
    if 'Country' in network['extattrs']:
        country = network['extattrs']['Country']['value']
    if 'Site' in network['extattrs']:
        site = network['extattrs']['Site']['value']
    if 'Region' in network['extattrs']:
        region = network['extattrs']['Region']['value']
    if 'Zone' in network['extattrs']:
        zone = network['extattrs']['Zone']['value']

		
    req_params2={'network': network['network']}		
    d = requests.get(url + 'ipv4address',
                     params=req_params2,
                     cookies=req_cookies,
                     verify=valid_cert)

    if d.status_code != requests.codes.ok:
        print d.text
       # exit_msg = 'Error {} finding ipv4address objects: {}'
       # sys.exit(exit_msg.format(d.status_code, d.reason))

    count =0;
    nettxt = d.json();
    size = len(nettxt);
    nettxtip = '';

    if prefix <= 22:	
        stats.append(("Network is too large for parsing with REST-API",
					  "no_name",
					  netaddr,
					  prefix,
					  country,
					  site,
					  region,
					  zone))
					  
    else:   
        while count < size:
            nettxtip = ("%s" %(nettxt[count]['ip_address']));
            cname = ("%s" %(nettxt[count]['names']));
            cname = cname[3:-2];
            count = count +1;

            if len(cname) > 4:
                if cname[3].lower() == ('n'):
				# Append desired info as a tuple to stats list for easy output.
	            stats.append((nettxtip,
						  cname,
						  netaddr,  
                          prefix,
                          country,
         		          site,
				          region,
				          zone))

 

Re: Get all static hosts with name and extattr with a python script over Rest API

Adviser
Posts: 267
5306     0

Thanks for your message, we appreciate you providing details.  It's a quiet week around here, but there's a few people I've asked to take a look at your question....stay tuned.

If you appreciate my efforts, please give me a kudo ↓ or Accept as solution to help others find it faster.

Re: Get all static hosts with name and extattr with a python script over Rest API

Adviser
Posts: 43
5306     0

The Client.Ibap.Proto [...] Result set too large (>1000) error is an indicator that _max_results is not being parsed properly as a parameter in the URL.

 

Can you double check to make sure that this is being set properly in the actual HTTPS call or test this manually on one of the networks where you are hitting the error?  

 

E.g.  

 

curl -k1 -u ibuser -X GET 'https://10.10.100.51/wapi/v2.2/ipv4address?network=10.20.0.0/18&_max_results=100000'

 

returns all the records I have in my test system under that network (which is >10,000).

Re: Get all static hosts with name and extattr with a python script over Rest API

GHorne Community Manager
Community Manager
Posts: 254
5306     0

Generally, doing something like 'getting every IP address' is a recipie for hurt, and it not recommended. (If you're doing queries to get all the records or one type then you may want to ask 'why?', and re-craft your queries for a more specific set of results)

 

To get "every host with a "n" at the fourth space in the name", do a host search, with a wildcard in the query:

 

 

record:host?name~=^...n

 

 

Re: Get all static hosts with name and extattr with a python script over Rest API

denpiepe
Techie
Posts: 6
5306     0

Thanks for the answers.

 

The special case I want to use the API is to get all Hosts with a "n" at the fourth space and then I need the extattr from that host/network.

 

If I use

record:host?name~=^...n

I don't get the extattrs. Is this right?

 

Re: Get all static hosts with name and extattr with a python script over Rest API

GHorne Community Manager
Community Manager
Posts: 254
5306     0
record:host?name~=^...n&_return_fields%2B=extattrs

note that you need to url encode the '+' symbol, hence the '%2B' instead of a '+' 

Re: Get all static hosts with name and extattr with a python script over Rest API

denpiepe
Techie
Posts: 6
5306     0

Do you have a small example snippet in python code?

Showing results for 
Search instead for 
Do you mean 

Recommended for You