def load_config(self, nipap_config): """ Fetch connection parameters from provided dict and build xmlrpc_url to connect to Nipap API :param nipap_config: :return: """ # Required parameters in config file required = ['host', 'username', 'password', 'port'] # Check if all params are present in config and build URL if all([param in nipap_config for param in required]): self.host = nipap_config['host'] nipap_url = "http://%s:%s@%s:%d/XMLRPC" % ( nipap_config['username'], nipap_config['password'], nipap_config['host'], int(nipap_config['port']), ) pynipap.xmlrpc_uri = nipap_url pynipap.AuthOptions({ 'authoritative_source': 'nipap-gui' }) else: raise Exception("No nipap host defined in config file")
def freeprefix(nipap_deamon_ip, account_cb_alias, account_iam_alias, vpc_network, vpc_prefix): # Lookup nipap daemon password cipher nipapCfn = dynamodb.Table(os.environ['TAILOR_TABLENAME_NIPAPCFN']) getNipapCfn = nipapCfn.get_item( Key={ 'nipapAlias': account_cb_alias } ) # Decrypt nipap daemon password nipapDaemonPasswordCipherBlob = getNipapCfn['Item']['nipapDaemonPasswordCipherBlob'] nipapDeamonPassword = bytes(kms.decrypt(CiphertextBlob=b64decode(nipapDaemonPasswordCipherBlob))['Plaintext']) # Look up free CIDR block pynipap.xmlrpc_uri = "http://*****:*****@" + nipap_deamon_ip + ":1337" a = pynipap.AuthOptions({ 'authoritative_source': 'tailor_nipap_client' }) # Allocate first available new_prefix = Prefix() new_prefix.description = account_iam_alias new_prefix.type = "assignment" # Save will communicate with the backend and ask for the next available desired prefix size new_prefix.save({'from-prefix': [vpc_network], 'prefix_length': vpc_prefix}) # Read the assigned prefix from the new_prefix object print("VPC Cidr is: ", new_prefix.prefix) return new_prefix.prefix
def write(self, output_file, query): """ """ f = open(output_file, "w+") writer = csv.writer(f, quoting=csv.QUOTE_MINIMAL) pynipap.xmlrpc_uri = xmlrpc_uri ao = pynipap.AuthOptions({'authoritative_source': 'nipap'}) import socket, xmlrpclib try: res = pynipap.Prefix.smart_search(query, {}) except socket.error: print >> sys.stderr, "Connection refused, please check hostname & port" sys.exit(1) except xmlrpclib.ProtocolError: print >> sys.stderr, "Authentication failed, please check your username / password" sys.exit(1) for p in res['result']: writer.writerow([ p.vrf.rt, p.display_prefix, p.type, p.node, p.order_id, p.customer_id, p.description ])
from pynipap import Prefix, Pool, VRF import pynipap # # Fill in your code here # if __name__ == '__main__': # read configuration cfg = ConfigParser.ConfigParser() cfg.read(os.path.expanduser('~/.nipaprc')) import optparse parser = optparse.OptionParser() parser.add_option('--username', default='', help="Username") parser.add_option('--password', default='', help="Password") parser.add_option('--host', help="NIPAP backend host") parser.add_option('--port', default=1337, help="NIPAP backend port") (options, args) = parser.parse_args() auth_uri = "%s:%s@" % (options.username or cfg.get('global', 'username'), options.password or cfg.get('global', 'password')) xmlrpc_uri = "http://%(auth_uri)s%(host)s:%(port)s" % { 'auth_uri': auth_uri, 'host': options.host or cfg.get('global', 'hostname'), 'port': options.port or cfg.get('global', 'port') } pynipap.AuthOptions({'authoritative_source': 'nipap'}) pynipap.xmlrpc_uri = xmlrpc_uri
import pynipap from pynipap import Prefix a = pynipap.AuthOptions({ 'authoritative_source': 'NIPAP-Sync/1.0' }) pynipap.xmlrpc_uri = "{nipapurl}" query = { 'operator': 'equals', 'val1': 'type', 'val2': 'host' } search_options = { 'max_result': 1000 } search_result = Prefix.search(query, search_options) file = open("/tmp/nipap.hosts", "w") for p in search_result['result']: ip = p.prefix.split('/')[0] host = p.description.replace(" ", "_") file.write(ip + " " + host + "\n") file.close()
from pynipap import Prefix from datetime import date, timedelta ##################### # Output file names # ##################### today = date.today() ymdToday = today.strftime('%Y-%m-%d') outFilenameToday = 'nipap_prefixes_' + ymdToday outFilenameRemovedPrefixes = 'nipap_removed_prefixes_' + ymdToday yesterday = date.today() - timedelta(days=1) outFilenameYesterday = 'nipap_prefixes_' + yesterday.strftime('%Y-%m-%d') ######################## # Nipap authentication # ######################## pynipap.xmlrpc_uri = "http://*****:*****@nipap.local:1337/XMLRPC" a = pynipap.AuthOptions({'authoritative_source': 'nlogin_nipap_client'}) customerQuery = { 'val1': 'customer_id', 'operator': 'regex_match', 'val2': '^CUST-\d{5,}$' } prefixSearch = Prefix.smart_search('', {}, customerQuery) nipapPrefixObjectsList = prefixSearch['result'] ######################################## # Functions: # # 1. Group Nipap prefix data by 'custId' # # 2. Print variable contents to a file # # 3. Write file contents to a variable #