def interactive_options(gmp, task, keywords): options_dict = {} options_dict['config'] = gmp.get_configs() options_dict['scanner'] = gmp.get_scanners() options_dict['target'] = gmp.get_targets() for option in options_dict: object_dict, object_list = {}, [] object_id = task.xpath('{}/@id'.format(option))[0] object_xml = options_dict[option] for i in object_xml.xpath('{}'.format(option)): object_dict[i.find('name').text] = i.xpath('@id')[0] object_list.append(i.find('name').text) if object_id in object_dict.values(): keywords['{}_id'.format(option)] = object_id elif object_id not in object_dict.values() and len(object_dict) != 0: response = yes_or_no( "\nRequired Field: failed to detect {}_id: {}... " "\nWould you like to select from available options, or exit " "the script?".format( option, task.xpath('{}/@id'.format(option))[0] ) ) if response is True: counter = 1 print("{} options:".format(option.capitalize())) for j in object_list: print(" {} - {}".format(counter, j)) counter += 1 answer = numerical_option( "\nPlease enter the number of your choice.", len(object_list), ) keywords['{}_id'.format(option)] = object_dict[ object_list[answer - 1] ] else: print("\nTerminating...") quit() else: error_and_exit( "Failed to detect {}_id" "\nThis field is required therefore the script is unable to " "continue.\n".format(option) )
def interactive_options(gmp, task, keywords): options_dict = {} options_dict['config'] = gmp.get_scan_configs() options_dict['scanner'] = gmp.get_scanners() options_dict['target'] = gmp.get_targets() for option_key, option_value in options_dict.items(): object_dict, object_list = {}, [] object_id = task.find(option_key).get('id') object_xml = option_value for i in object_xml.findall(option_key): object_dict[i.find('name').text] = i.xpath('@id')[0] object_list.append(i.find('name').text) if object_id in object_dict.values(): keywords[f'{option_key}_id'] = object_id elif object_id not in object_dict.values() and len(object_dict) != 0: response = yes_or_no( f"\nRequired Field: failed to detect {option_key}_id: " f"{task.xpath(f'{option_key}/@id')[0]}... " "\nWould you like to select from available options, or exit " "the script?") if response is True: counter = 1 print(f"{option_key.capitalize()} options:") for j in object_list: print(f" {counter} - {j}") counter += 1 answer = numerical_option( "\nPlease enter the number of your choice.", len(object_list), ) keywords[f'{option_key}_id'] = object_dict[object_list[answer - 1]] else: print("\nTerminating...") sys.exit() else: error_and_exit( f"Failed to detect {option_key}_id\nThis field is required " "therefore the script is unable to continue.\n")
def test_no(self): no = yes_or_no('bar?') self.assertFalse(no)
def test_yes(self): yes = yes_or_no('foo?') self.assertTrue(yes)
def parse_send_xml_tree(gmp, xml_tree): credential_options = [ 'ssh_credential', 'smb_credential', 'esxi_credential', 'snmp_credential', ] counter = 1 for target in xml_tree.xpath('target'): keywords = {} # {'make_unique': True} keywords['name'] = target.find('name').text keywords['hosts'] = target.find('hosts').text.split(',') exclude_hosts = target.find('exclude_hosts').text if exclude_hosts is not None: keywords['exclude_hosts'] = exclude_hosts.split(',') comment = target.find('comment').text if comment is not None: keywords['comment'] = comment credentials = gmp.get_credentials()[0].xpath("//credential/@id") for credential in credential_options: cred_id = target.find(credential).xpath('@id')[0] if cred_id == '': continue if cred_id not in credentials: response = yes_or_no( "\nThe credential '{}' for 'target {}' could not be " "located...\nWould you like to continue?".format( credential, counter)) if response is False: print("Terminating...\n") sys.exit() else: continue key = '{}_id'.format(credential) keywords[key] = cred_id elem_path = target.find(credential) port = elem_path.find('port') if port is not None and port.text is not None: port_key = '{}_port'.format(credential) keywords[port_key] = elem_path.find('port').text alive_test = get_alive_test_from_string( target.find('alive_tests').text) if alive_test is not None: keywords['alive_test'] = alive_test reverse_lookup_only = target.find('reverse_lookup_only').text if reverse_lookup_only == '1': keywords['reverse_lookup_only'] = 1 reverse_lookup_unify = target.find('reverse_lookup_unify').text if reverse_lookup_unify == '1': keywords['reverse_lookup_unify'] = 1 port_range = target.find('port_range') if port_range is not None: keywords['port_range'] = port_range.text if target.xpath('port_list/@id') is not None: port_list = {} port_list = target.xpath('port_list/@id')[0] keywords['port_list_id'] = port_list print(keywords) gmp.create_target(**keywords) counter += 1