| @@ 73-151 (lines=79) @@ | ||
| 70 | return xml_tree |
|
| 71 | ||
| 72 | ||
| 73 | def parse_send_xml_tree(gmp, xml_tree): |
|
| 74 | credential_options = [ |
|
| 75 | 'ssh_credential', |
|
| 76 | 'smb_credential', |
|
| 77 | 'esxi_credential', |
|
| 78 | 'snmp_credential', |
|
| 79 | ] |
|
| 80 | counter = 1 |
|
| 81 | ||
| 82 | for target in xml_tree.xpath('target'): |
|
| 83 | keywords = {} # {'make_unique': True} |
|
| 84 | ||
| 85 | keywords['name'] = target.find('name').text |
|
| 86 | ||
| 87 | keywords['hosts'] = target.find('hosts').text.split(',') |
|
| 88 | ||
| 89 | exclude_hosts = target.find('exclude_hosts').text |
|
| 90 | if exclude_hosts is not None: |
|
| 91 | keywords['exclude_hosts'] = exclude_hosts.split(',') |
|
| 92 | ||
| 93 | comment = target.find('comment').text |
|
| 94 | if comment is not None: |
|
| 95 | keywords['comment'] = comment |
|
| 96 | ||
| 97 | credentials = gmp.get_credentials()[0].xpath("//credential/@id") |
|
| 98 | ||
| 99 | for credential in credential_options: |
|
| 100 | cred_id = target.find(credential).xpath('@id')[0] |
|
| 101 | if cred_id == '': |
|
| 102 | continue |
|
| 103 | if cred_id not in credentials: |
|
| 104 | response = yes_or_no( |
|
| 105 | "\nThe credential '{}' for 'target {}' could not be " |
|
| 106 | "located...\nWould you like to continue?".format( |
|
| 107 | credential, counter |
|
| 108 | ) |
|
| 109 | ) |
|
| 110 | ||
| 111 | if response is False: |
|
| 112 | print("Terminating...\n") |
|
| 113 | quit() |
|
| 114 | else: |
|
| 115 | continue |
|
| 116 | ||
| 117 | key = '{}_id'.format(credential) |
|
| 118 | keywords[key] = cred_id |
|
| 119 | elem_path = target.find(credential) |
|
| 120 | port = elem_path.find('port') |
|
| 121 | if port is not None and port.text is not None: |
|
| 122 | port_key = '{}_port'.format(credential) |
|
| 123 | keywords[port_key] = elem_path.find('port').text |
|
| 124 | ||
| 125 | alive_test = get_alive_test_from_string(target.find('alive_tests').text) |
|
| 126 | ||
| 127 | if alive_test is not None: |
|
| 128 | keywords['alive_test'] = alive_test |
|
| 129 | ||
| 130 | reverse_lookup_only = target.find('reverse_lookup_only').text |
|
| 131 | if reverse_lookup_only == '1': |
|
| 132 | keywords['reverse_lookup_only'] = 1 |
|
| 133 | ||
| 134 | reverse_lookup_unify = target.find('reverse_lookup_unify').text |
|
| 135 | if reverse_lookup_unify == '1': |
|
| 136 | keywords['reverse_lookup_unify'] = 1 |
|
| 137 | ||
| 138 | port_range = target.find('port_range') |
|
| 139 | if port_range is not None: |
|
| 140 | keywords['port_range'] = port_range.text |
|
| 141 | ||
| 142 | if target.xpath('port_list/@id') is not None: |
|
| 143 | port_list = {} |
|
| 144 | port_list = target.xpath('port_list/@id')[0] |
|
| 145 | keywords['port_list_id'] = port_list |
|
| 146 | ||
| 147 | print(keywords) |
|
| 148 | ||
| 149 | gmp.create_target(**keywords) |
|
| 150 | ||
| 151 | counter += 1 |
|
| 152 | ||
| 153 | ||
| 154 | def main(gmp, args): |
|
| @@ 72-150 (lines=79) @@ | ||
| 69 | return xml_tree |
|
| 70 | ||
| 71 | ||
| 72 | def parse_send_xml_tree(gmp, xml_tree): |
|
| 73 | credential_options = [ |
|
| 74 | 'ssh_credential', |
|
| 75 | 'smb_credential', |
|
| 76 | 'esxi_credential', |
|
| 77 | 'snmp_credential', |
|
| 78 | ] |
|
| 79 | counter = 1 |
|
| 80 | ||
| 81 | for target in xml_tree.xpath('target'): |
|
| 82 | keywords = {} # {'make_unique': True} |
|
| 83 | ||
| 84 | keywords['name'] = target.find('name').text |
|
| 85 | ||
| 86 | keywords['hosts'] = target.find('hosts').text.split(',') |
|
| 87 | ||
| 88 | exclude_hosts = target.find('exclude_hosts').text |
|
| 89 | if exclude_hosts is not None: |
|
| 90 | keywords['exclude_hosts'] = exclude_hosts.split(',') |
|
| 91 | ||
| 92 | comment = target.find('comment').text |
|
| 93 | if comment is not None: |
|
| 94 | keywords['comment'] = comment |
|
| 95 | ||
| 96 | credentials = gmp.get_credentials()[0].xpath("//credential/@id") |
|
| 97 | ||
| 98 | for credential in credential_options: |
|
| 99 | cred_id = target.find(credential).xpath('@id')[0] |
|
| 100 | if cred_id == '': |
|
| 101 | continue |
|
| 102 | if cred_id not in credentials: |
|
| 103 | response = yes_or_no( |
|
| 104 | "\nThe credential '{}' for 'target {}' could not be " |
|
| 105 | "located...\nWould you like to continue?".format( |
|
| 106 | credential, counter |
|
| 107 | ) |
|
| 108 | ) |
|
| 109 | ||
| 110 | if response is False: |
|
| 111 | print("Terminating...\n") |
|
| 112 | quit() |
|
| 113 | else: |
|
| 114 | continue |
|
| 115 | ||
| 116 | key = '{}_id'.format(credential) |
|
| 117 | keywords[key] = cred_id |
|
| 118 | elem_path = target.find(credential) |
|
| 119 | port = elem_path.find('port') |
|
| 120 | if port and port.text is not None: |
|
| 121 | port_key = '{}_port'.format(credential) |
|
| 122 | keywords[port_key] = elem_path.find('port').text |
|
| 123 | ||
| 124 | alive_test = get_alive_test_from_string(target.find('alive_tests').text) |
|
| 125 | ||
| 126 | if alive_test is not None: |
|
| 127 | keywords['alive_test'] = alive_test |
|
| 128 | ||
| 129 | reverse_lookup_only = target.find('reverse_lookup_only').text |
|
| 130 | if reverse_lookup_only == '1': |
|
| 131 | keywords['reverse_lookup_only'] = 1 |
|
| 132 | ||
| 133 | reverse_lookup_unify = target.find('reverse_lookup_unify').text |
|
| 134 | if reverse_lookup_unify == '1': |
|
| 135 | keywords['reverse_lookup_unify'] = 1 |
|
| 136 | ||
| 137 | port_range = target.find('port_range') |
|
| 138 | if port_range is not None: |
|
| 139 | keywords['port_range'] = port_range.text |
|
| 140 | ||
| 141 | if target.xpath('port_list/@id') is not None: |
|
| 142 | port_list = {} |
|
| 143 | port_list = target.xpath('port_list/@id')[0] |
|
| 144 | keywords['port_list_id'] = port_list |
|
| 145 | ||
| 146 | print(keywords) |
|
| 147 | ||
| 148 | gmp.create_target(**keywords) |
|
| 149 | ||
| 150 | counter += 1 |
|
| 151 | ||
| 152 | ||
| 153 | def main(gmp, args): |
|