def main(argv): # Set default domain for LPL new_host_args = {'domain': 'lpl.arizona.edu'} try: opts, args = getopt.getopt(argv, "", [ 'replace', 'help', 'hostname=', 'domain=', 'a=', 'aaaa=', 'cname=', 'mx=', 'description=', 'macAddress=' ]) replace_records = False except getopt.GetoptError: usage(2) p_opts = [e for e, trash in opts] if not '--hostname' in p_opts and not '--help' in p_opts: print "Need one primary option" usage(2) # Initialize state from XML Document state = stateInit() # Parse in some arguments to keep track of record types we are dealing with for opt, arg in opts: opt = re.sub('^[-]+', '', opt) if opt in ('hostname', 'domain', 'a', 'aaaa', 'cname', 'mx', 'description', 'macAddress'): new_host_args[opt] = arg if opt == 'replace': replace_records = True # Then look for an existing host entry that matches the same name filter = lambda host, input: host.shortname == new_host_args[ 'hostname'] and host.domainname == input searchedHostList = state.getHostList().filteredList( filter, new_host_args['domain']) if len(searchedHostList.getList()) > 0: print "Selecting existing host with same name/domain..." newHost = searchedHostList.getList()[0] else: # Let's create a host with the desired hostname/domain newHost = xmldns.XMLHost(new_host_args['domain'], new_host_args['hostname']) state.getHostList().addHost(newHost) # Set the current host state.setCurrentHost(newHost) dnsRecords = [ 'a', 'aaaa', 'cname', 'loc', 'mx', 'ns', 'ptr', 'rp', 'srv', 'txt' ] ## WE ARE HERE... # TODO: replace current record types with specified record types # DEBUG OUTPUT # state.getCurrentHost().printHost("| ") # Remove old records that we are replacing (or err on replace) for recordType in new_host_args.keys(): filter = lambda rec: rec.recordType == recordType if recordType in dnsRecords: oldRecords = state.getCurrentHost().getDNSRecords(filter) for record in oldRecords: if not replace_records: usage( 1, "Need to specify --replace to replace %s: %s" % (record.recordType, record.recordData)) state.getCurrentHost().removeDNSRecord(record.recordType, record.recordData, record.recordNet) state.appendLog( "Removed: %s [%s]: %s" % (record.recordType, record.recordNet, record.recordData)) else: oldRecords, oldRecords2, oldRecords3 = state.getCurrentHost( ).getRecords(filter) for record in oldRecords: if not replace_records: usage( 1, "Need to specify --replace to replace %s: %s" % (record.recordType, record.recordData)) state.getCurrentHost().removeRecord(record.recordType, record.recordData) state.appendLog("Removed: %s: %s" % (record.recordType, record.recordData)) for record in oldRecords2: if not replace_records: usage( 1, "Need to specify --replace to replace %s: %s" % (record.recordType, record.recordData)) state.getCurrentHost().removeRecord(record.recordType, record.recordData) state.appendLog("Removed: %s: %s" % (record.recordType, record.recordData)) # initiate a record validator validator = xmldns.validator.RecordValidator(state) # Add records into host for opt, arg in opts: ## Remove -- from beginning of opt opt = re.sub('^[-]+', '', opt) if opt == 'replace': continue # Make sure record is syntactically valid if not validator.validRecord(opt, arg): print "Error: " + arg + " is not a valid " + opt + " record." sys.exit(0) # Add the record (DNS or otherwise) if opt in dnsRecords: # 'a' records need to be placed in the correct network if opt == 'a': network = getNetworkFromIP4(state, arg) if not network: print "Error: IP does not belong to any registered network: %s" % arg sys.exit(-1) nets = [network] elif opt == 'aaaa': network = getNetworkFromIP6(state, arg) if not network: print "Error: IP does not belong to any registered network: %s" % arg sys.exit(-1) nets = [network] elif opt == 'cname': # Magic value! network = 'globalnet' if not network: print "Error: IP does not belong to any registered network: %s" % arg sys.exit(-1) nets = [network] else: # otherwise punt and add to all current networks nets = state.getCurrentHost().getNetworks() if len(nets) < 1: nets = ('globalnet') # Add record to all valid networks for net in nets: state.getCurrentHost().addDNSRecord(opt, arg, net) state.appendLog("Added: %s [%s]: %s" % (opt, net, arg)) # Add a NON-DNS record (much easier) else: if opt not in ('hostname', 'domain'): state.getCurrentHost().addRecord(opt, arg) state.appendLog("Added: %s: %s" % (opt, arg)) # Show new host description state.getCurrentHost().printHost("| ") # Save the configuration saveChanges(state) # Print session log print "Session Log:" print state.getLog() print "--------------------------------------------------------------------------------"
def main(argv): # Set default domain for LPL new_host_args = { 'domain': 'lpl.arizona.edu' } try: opts, args = getopt.getopt(argv, "", ['replace', 'help', 'hostname=', 'domain=', 'a=', 'aaaa=', 'cname=', 'mx=', 'description=', 'macAddress=']) replace_records = False except getopt.GetoptError: usage(2) p_opts = [e for e,trash in opts] if not '--hostname' in p_opts and not '--help' in p_opts: print "Need one primary option" usage(2) # Initialize state from XML Document state = stateInit() # Parse in some arguments to keep track of record types we are dealing with for opt, arg in opts: opt = re.sub('^[-]+', '', opt) if opt in ( 'hostname', 'domain', 'a', 'aaaa', 'cname', 'mx', 'description', 'macAddress' ): new_host_args[ opt ] = arg if opt == 'replace': replace_records = True # Then look for an existing host entry that matches the same name filter = lambda host, input: host.shortname == new_host_args[ 'hostname' ] and host.domainname == input searchedHostList = state.getHostList().filteredList(filter,new_host_args[ 'domain' ]) if len( searchedHostList.getList() ) > 0: print "Selecting existing host with same name/domain..." newHost = searchedHostList.getList()[0] else: # Let's create a host with the desired hostname/domain newHost = xmldns.XMLHost(new_host_args['domain'], new_host_args['hostname']) state.getHostList().addHost(newHost) # Set the current host state.setCurrentHost( newHost ) dnsRecords = [ 'a', 'aaaa', 'cname', 'loc', 'mx', 'ns', 'ptr', 'rp', 'srv', 'txt' ] ## WE ARE HERE... # TODO: replace current record types with specified record types # DEBUG OUTPUT # state.getCurrentHost().printHost("| ") # Remove old records that we are replacing (or err on replace) for recordType in new_host_args.keys(): filter = lambda rec: rec.recordType == recordType if recordType in dnsRecords: oldRecords = state.getCurrentHost().getDNSRecords( filter ) for record in oldRecords: if not replace_records: usage(1, "Need to specify --replace to replace %s: %s" % (record.recordType, record.recordData)) state.getCurrentHost().removeDNSRecord( record.recordType, record.recordData, record.recordNet ) state.appendLog("Removed: %s [%s]: %s" % ( record.recordType, record.recordNet, record.recordData )) else: oldRecords, oldRecords2, oldRecords3 = state.getCurrentHost().getRecords( filter ) for record in oldRecords: if not replace_records: usage(1, "Need to specify --replace to replace %s: %s" % (record.recordType, record.recordData)) state.getCurrentHost().removeRecord( record.recordType, record.recordData ) state.appendLog("Removed: %s: %s" % ( record.recordType, record.recordData )) for record in oldRecords2: if not replace_records: usage(1, "Need to specify --replace to replace %s: %s" % (record.recordType, record.recordData)) state.getCurrentHost().removeRecord( record.recordType, record.recordData ) state.appendLog("Removed: %s: %s" % ( record.recordType, record.recordData )) # initiate a record validator validator = xmldns.validator.RecordValidator( state ) # Add records into host for opt, arg in opts: ## Remove -- from beginning of opt opt = re.sub('^[-]+', '', opt) if opt == 'replace': continue # Make sure record is syntactically valid if not validator.validRecord( opt, arg ): print "Error: " + arg + " is not a valid " + opt + " record." sys.exit(0) # Add the record (DNS or otherwise) if opt in dnsRecords: # 'a' records need to be placed in the correct network if opt == 'a': network = getNetworkFromIP4( state, arg ) if not network: print "Error: IP does not belong to any registered network: %s" % arg sys.exit(-1) nets = [ network ] elif opt == 'aaaa': network = getNetworkFromIP6( state, arg ) if not network: print "Error: IP does not belong to any registered network: %s" % arg sys.exit(-1) nets = [ network ] elif opt == 'cname': # Magic value! network = 'globalnet' if not network: print "Error: IP does not belong to any registered network: %s" % arg sys.exit(-1) nets = [ network ] else: # otherwise punt and add to all current networks nets = state.getCurrentHost().getNetworks() if len(nets) < 1: nets = ('globalnet') # Add record to all valid networks for net in nets: state.getCurrentHost().addDNSRecord( opt, arg, net ) state.appendLog("Added: %s [%s]: %s" % (opt, net, arg)) # Add a NON-DNS record (much easier) else: if opt not in ('hostname', 'domain'): state.getCurrentHost().addRecord( opt, arg ) state.appendLog("Added: %s: %s" % (opt, arg)) # Show new host description state.getCurrentHost().printHost("| ") # Save the configuration saveChanges( state ) # Print session log print "Session Log:" print state.getLog() print "--------------------------------------------------------------------------------"
def doAddRecordMenuAction(option, state): option_actions = { 'a': (''), 'aaaa': (''), 'cname': (''), 'loc': (''), 'mx': (''), 'ns': (''), 'ptr': (''), 'rp': (''), 'srv': (''), 'txt': (''), 'dnsrr': (''), 'action': (''), 'description': (''), 'known-duplicate': (''), 'macAddress': (''), 'manager': (''), 'notes': (''), } ## We need to know when to ask for a network... dnsRecords = [ 'a', 'aaaa', 'cname', 'loc', 'mx', 'ns', 'ptr', 'rp', 'srv', 'txt' ] networkName = None if option in dnsRecords: networkName = chooseNetwork( state, "XML Host Editor > Modify Host > Add Record (" + option + ")") # Find the next IPv4 address and report it if option == 'a': filter = lambda rec: rec.recordType == 'a' and rec.recordNet == networkName netRecords = state.getHostList().getDNSRecords(filter) network = state.getNetworkList().getNetwork(networkName) prefices = network.getRecordsOfType('prefix') ## Really Ugly... oh well for prefix in prefices: if re.match("^\d+\.\d+\.\d+\.\d+/24$", prefix.recordData): print " Free IPv4 records in " + prefix.recordData ipPieces = prefix.recordData.split(".") foundCount = 0 # iterate through IP's for num in range(1, 255): freeIP = "%s.%s.%s.%d" % (ipPieces[0], ipPieces[1], ipPieces[2], num) found = 0 # Iterate through records (should really just be a hash) for record in netRecords: if record.recordData == freeIP: found = 1 if not (found) and foundCount < 5: foundCount = foundCount + 1 print " " + freeIP validator = xmldns.validator.RecordValidator(state) recordData = None userPrompt = option + " record data" if option == 'dnsrr': userPrompt = 'dnsrr hostname' while not (recordData): recordData = getInput(userPrompt) recordData = validator.validRecord(option, recordData) currentHost = state.getCurrentHost() # Add record appropriately if option in dnsRecords: state.appendLog("Added: %s [%s]: %s" % (option, networkName, recordData)) return currentHost.addDNSRecord(option, recordData, networkName) if option == 'dnsrr': state.appendLog("Added: %s: %s" % (option, recordData)) return currentHost.addDNSRR('hostname', recordData) state.appendLog("Added: %s: %s" % (option, recordData)) return currentHost.addRecord(option, recordData)
def doAddRecordMenuAction( option, state ): option_actions = { 'a': (''), 'aaaa': (''), 'cname': (''), 'loc': (''), 'mx': (''), 'ns': (''), 'ptr': (''), 'rp': (''), 'srv': (''), 'txt': (''), 'dnsrr': (''), 'action': (''), 'description': (''), 'known-duplicate': (''), 'macAddress': (''), 'manager': (''), 'notes': (''), } ## We need to know when to ask for a network... dnsRecords = [ 'a', 'aaaa', 'cname', 'loc', 'mx', 'ns', 'ptr', 'rp', 'srv', 'txt' ] networkName = None if option in dnsRecords: networkName = chooseNetwork( state, "XML Host Editor > Modify Host > Add Record (" + option + ")" ) # Find the next IPv4 address and report it if option == 'a': filter = lambda rec: rec.recordType == 'a' and rec.recordNet == networkName netRecords = state.getHostList().getDNSRecords( filter ) network = state.getNetworkList().getNetwork( networkName ) prefices = network.getRecordsOfType( 'prefix' ) ## Really Ugly... oh well for prefix in prefices: if re.match("^\d+\.\d+\.\d+\.\d+/24$", prefix.recordData): print " Free IPv4 records in " + prefix.recordData ipPieces = prefix.recordData.split(".") foundCount = 0 # iterate through IP's for num in range(1,255): freeIP = "%s.%s.%s.%d" % (ipPieces[0], ipPieces[1], ipPieces[2], num) found = 0 # Iterate through records (should really just be a hash) for record in netRecords: if record.recordData == freeIP: found = 1 if not(found) and foundCount < 5: foundCount = foundCount + 1 print " " + freeIP validator = xmldns.validator.RecordValidator( state ) recordData = None userPrompt = option + " record data" if option == 'dnsrr': userPrompt = 'dnsrr hostname' while not( recordData ): recordData = getInput(userPrompt) recordData = validator.validRecord( option, recordData ) currentHost = state.getCurrentHost() # Add record appropriately if option in dnsRecords: state.appendLog("Added: %s [%s]: %s" % (option, networkName, recordData)) return currentHost.addDNSRecord( option, recordData, networkName ) if option == 'dnsrr': state.appendLog("Added: %s: %s" % (option, recordData)) return currentHost.addDNSRR( 'hostname', recordData ) state.appendLog("Added: %s: %s" % (option, recordData)) return currentHost.addRecord( option, recordData )