Reply

Export network hierarchy including extensible attributes

Adviser
Posts: 132
2628     0

Today on an Infoblox-internal mailing list a question came up regarding how to use the Web API to export network information including any extensible attribute values defined on those networks. I already had a Python script that did something similar, and so I decided to update it to add support for EAs. I've attached the resulting script to this message. (You'll need to rename the file to have a .py suffix.)

 

The script will create a CSV output file containing one row for each IPv4 network and IPv4 network container. The output is sorted by network address, with containers sorted before the networks they contain. Each row of the output contains as the first two columns the network (or network container) address in CIDR format followed by the value of the comments fields (if any) for that network or container. The remaining columns contain extensible attribute values, one per column; if a particular EA is not defined for a given network or container then that column will be blank. The script automatically discovers all EAs defined on networks or containers, and exports the columns sorted by EA name. The script also handles multi-valued EAs.

 

Here's some sample output from the script:

 

Network,Comment,Building,Contacts,Country,Department,Department Number,State
10.0.0.0/8,,"The ""New"" Office",jsmith@example.com,,,,
10.0.0.0/16,QA/test,,"rroe@example.com,jdoe@example.com",,,,
10.0.0.0/24,Demo lab network,,,,,,
10.1.0.0/16,RelEng,"The New, Improved Office",,,Engineering,65,
192.168.0.0/23,Main home network,,,US,,,Maryland

 

The output file can be opened in Microsoft Excel and will produce a spreadsheet with eight columns. In this example the Contacts EA (fourth column) is multi-valued, with only one contact email address defined for 10.0.0.0/8 but two contact email addresses defined for 10.0.0.0/16. For the network container 10.0.0.0/8 the (single) value of the Building EA (third column) contains quotation marks, while for the network 10.1.0.0/16 its (single) value contains a comma. In both cases the CSV output is quoted so that it will be properly imported into Excel.

 

I hope this script proves useful, especially for those of you who want to do WAPI scripts involving extensible attributes.

Re: Export network hierarchy including extensible attributes

gusmcarvalho
Techie
Posts: 1
2628     0

Hi Sr. 

 

I saw your script, and it was useful for me, but I would like to know if you have a version that shows IP addressess inside each network, including its EAs.

 

If yes, would you mind to share ?

 

Thank you 

 

Gustavo

Re: Export network hierarchy including extensible attributes

Adviser
Posts: 132
2628     0

 


I saw your script, and it was useful for me, but I would like to know if you have a version that shows IP addressess inside each network, including its EAs.

 

I have a script that exports all used addresses, but it does not include extensible attributes. Here's the script:

 

# Import the required Python modules.
import requests
import json
import csv
import getpass
import sys

def ipv4addr_key(address_tuple):
    """Return a character string to use for sorting IPv4 addresses."""

    # Split dotted decimal address into octets, convert to an integer.
    addr_str = address_tuple[0]
    addr_octets = [int(octet) for octet in addr_str.split('.')]
    addr_value = (256*256*256 * addr_octets[0] +
                  256*256 * addr_octets[1] +
                  256 * addr_octets[2] +
                  addr_octets[3])

    # Format address value to form sort key.
    key_str = '{:0>12d}'.format(addr_value)
    return key_str


# Set parameters to access the NIOS WAPI.
url = 'https://gm.example.com/wapi/v1.7/'
id = 'api'  # Userid with WAPI access
valid_cert = False  # True if GM uses certificate from commercial CA

# Prompt for the API user password.
pw = getpass.getpass('Password for user ' + id + ': ')

# If running on Windows avoid error due to a self-signed cert.
if sys.platform.startswith('win') and not valid_cert:
    requests.packages.urllib3.disable_warnings()

# Retrieve all network objects (up to a max of 5000) in the default
# network view.
network_view = 'default'
max_results = -5000
r = requests.get(url + 'network' +
                   '?network_view=' + network_view +
                   '&_max_results=' + str(max_results),
                 auth=(id, pw),
                 verify=valid_cert)
if r.status_code != requests.codes.ok:
    print r.text
    exit_msg = 'Error {} finding networks: {}'
    sys.exit(exit_msg.format(r.status_code, r.reason))
network_results = r.json()

# Save the authentication cookie for use in the next request.
ibapauth_cookie = r.cookies['ibapauth']

# Print networks found.
print 'Matching networks:'
for network_result in network_results:
    print network_result['network'], network_result['network_view']

# For each matching network retrieve all used addresses (up to a max
# of 5000).
print 'Finding used addresses...'
used_addresses = []
for network_result in network_results:
    network = network_result['network']
    network_view = network_result['network_view']

    # Skip any network larger than a /24.
    addr_str, prefix_str = network.split('/')
    prefix = int(prefix_str)
    if prefix < 24:
        continue

    # Use the ibapauth cookie to authenticate instead of userid/password.
    request_cookies = {'ibapauth': ibapauth_cookie}
    max_results = -5000

    req_params = {'network': network,
                  'network_view': network_view,
                  'status': 'USED',
                  '_max_results': str(max_results)}

    r = requests.get(url + 'ipv4address',
                     params=req_params,
                     cookies=request_cookies,
                     verify=valid_cert)
    if r.status_code != requests.codes.ok:
        print r.text
        exit_msg = 'Error {} finding addresses: {}'
        sys.exit(exit_msg.format(r.status_code, r.reason))

    address_results = r.json()

    for address_result in address_results:
        address = address_result['ip_address']
        names = ','.join(address_result['names'])
        type = ''
        if 'lease_state' in address_result:
            if address_result['lease_state'] == 'ACTIVE':
                type = 'dynamic'
            else:
                type = 'free'
        elif 'FA' in address_result['types']:
            type = 'fixed'
        else:
            type = 'static'
        used_addresses.append((address, names, type))
print '...done'

# Sort the resulting used address list by address.
used_addresses.sort(key=ipv4addr_key)

# Export the results in CSV format.
print 'Exporting used addresses...'
with open('export-used-addresses.csv', 'wb') as out_file:
    out_csv = csv.writer(out_file,
                         delimiter=',',
                         quotechar='"',
                         quoting=csv.QUOTE_MINIMAL)
    # Export these columns.
    header_row = ['Address', 'Names', 'Type']
    out_csv.writerow(header_row)
    # Export one row for each used address.
    for used_address in used_addresses:
        out_csv.writerow(used_address)
print '...done'

 

Note that this script is not well-tested. It breaks on my own lab grid in some cases, but I'm not sure exactly why. (Maybe a WAPI call is returning too much data for the requests module?)

Re: Export network hierarchy including extensible attributes

[ Edited ]
geertn444
Techie
Posts: 1
2628     0

I tried the original script in our environment, but the script crashes on some unicode processing: File "./export-network-hierarchy-with-ea-values.py", line 200, in out_csv.writerow(network) UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 1: ordinal not in range(128) I guess it needs some more unicode protection.

 

ok, this seems to be because of some special characters,like ö

Highlighted

Re: Export network hierarchy including extensible attributes

Adviser
Posts: 132
2628     0

@geertn444 wrote:

ok, this seems to be because of some special characters,like ö


Yes, this is likely the dreaded Python 2.x problems with Unicode string handling. As far as I know the Infoblox system handles Unicode with no issue -- all the stuff returned from the WAPI is Unicode strings, I think.

 

Maybe this could be best addressed by porting the script to use Python 3 instead, in which all strings are automatically Unicode-compatible. Unfortunately I don't have time to look at this now, but I'd be glad to assist if you want to try to do this.

 

Frank

 

 

 

Showing results for 
Search instead for 
Do you mean 

Recommended for You