Created
October 10, 2016 22:17
-
-
Save rbenigno/67706fb20830017d8981cef96b907402 to your computer and use it in GitHub Desktop.
InfoBlox IP Lookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import requests | |
from requests.packages.urllib3.exceptions import InsecureRequestWarning | |
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) | |
from infoblox_client import connector | |
from infoblox_client import objects | |
import logging | |
def parse_args(): | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("ibhost", help="Infoblox Host") | |
parser.add_argument("ibuser", help="Infoblox User") | |
group = parser.add_mutually_exclusive_group(required=True) | |
group.add_argument("--ip", help="IPv4 address to search for") | |
group.add_argument("--ipfile", help="File with a list of IPv4 addresses to search for") | |
return parser.parse_args() | |
# Get password from environmental variable or prompt for input | |
def get_password(): | |
import getpass, os | |
if os.environ.get('IBPASS'): | |
return os.environ.get('IBPASS') | |
else: | |
return getpass.getpass('Password:') | |
# Search Infoblox for a specific IP address | |
def get_host_by_ip(ip): | |
hr = conn.get_object('record:host', {'ipv4addr': ip}) | |
if hr is None: | |
return None | |
else: | |
return hr[0]['name'] | |
### --------------------------------------------------------------------------- | |
### MAIN | |
### --------------------------------------------------------------------------- | |
# Main function (when running as an executable) | |
if __name__ == '__main__': | |
# Retrive the command line arguements | |
args = parse_args() | |
# Connect | |
#logging.basicConfig(level=logging.DEBUG) | |
opts = {'host': args.ibhost, 'username': args.ibuser, 'password': get_password()} | |
conn = connector.Connector(opts) | |
if args.ipfile: | |
with open(args.ipfile) as f: | |
ip_list = f.read().splitlines() | |
else: | |
ip_list = [args.ip] | |
for ipaddr in ip_list: | |
print '{}\t{}'.format(ipaddr, get_host_by_ip(ipaddr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment