Skip to content

Instantly share code, notes, and snippets.

@rbenigno
Created October 10, 2016 22:17
Show Gist options
  • Save rbenigno/67706fb20830017d8981cef96b907402 to your computer and use it in GitHub Desktop.
Save rbenigno/67706fb20830017d8981cef96b907402 to your computer and use it in GitHub Desktop.
InfoBlox IP Lookup
#!/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