def get_host_ids(hosts): # Initialize timer for status messages last_status_time = time.time() ids = [] password = decrypt_password() client = initialize_api(password) asset_api = rapid7vmconsole.AssetApi(client) counter = 0 for host in hosts: # Print status if more than 5 seconds have elapsed since last status counter += 1 if (time.time() - last_status_time > 5): print('Processed ' + str(counter) + ' of ' + str(len(hosts))) last_status_time = time.time() criteria = SwaggerSearchCriteriaFilter(field='host-name', operator="starts-with", value=host) search_criteria = SearchCriteria(filters=(criteria, ), match='all') asset_call = asset_api.find_assets(param1=search_criteria) if (len(asset_call.resources) == 1): ids.append(asset_call.resources[0].id) return ids
def search_assets(field='host-name',operator='is',lower=None,upper=None,value='testhost',values=None): # create an instance of the API class api_instance = rapid7vmconsole.AssetApi(client) #param1 = rapid7vmconsole.SearchCriteria() # SearchCriteria | param1 # this method works to use the library-provided class when defining filters # there may not be a reason to do this, but the example is included here because # using intermediate object and its included to_dict() method # (in rapid7vmconsole/models/swagger_search_criteria_filter.py) is how I derived proper # dict syntax # from rapid7vmconsole.models.swagger_search_criteria_filter import SwaggerSearchCriteriaFilter # filters = [SwaggerSearchCriteriaFilter(field="host-name",operator="is",value="testhost"),] # filters can also be defined by using a native dict: # note this dict must be embedded in an array, as the API expects an array filters = [{'field': field, 'lower': lower, 'operator': operator, 'upper': upper, 'value': value, 'values': values},] param1 = rapid7vmconsole.SearchCriteria(filters=filters,match='any') page = 0 # int | The index of the page (zero-based) to retrieve. (optional) (default to 0) size = 10 # int | The number of records per page to retrieve. (optional) (default to 10) #sort = ['sort_example'] # list[str] | The criteria to sort the records by, in the format: `property[,ASC|DESC]`. The default sort order is ascending. Multiple sort criteria can be specified using multiple sort query parameters. (optional) try: # Asset Search api_response = api_instance.find_assets(param1, page=page, size=size) # pprint(api_response) return api_response except ApiException as e: # print("Exception when calling AssetApi->find_assets: %s\n" % e) return None
def __init__(self): self.nexpose_config = rapid7vmconsole.Configuration(name='Scanner') self.nexpose_config.username = config['USERNAME'] self.nexpose_config.password = config['PASSWORD'] self.nexpose_config.host = config['HOST'] self.nexpose_config.assert_hostname = False self.nexpose_config.verify_ssl = False self.nexpose_config.ssl_ca_cert = None self.nexpose_config.connection_pool_maxsize = None self.nexpose_config.proxy = None self.nexpose_config.cert_file = None self.nexpose_config.key_file = None self.nexpose_config.safe_chars_for_path_param = '' auth_token = f'{config["USERNAME"]}:{config["PASSWORD"]}' auth_token = base64.b64encode(auth_token.encode('ascii')).decode() api_client = rapid7vmconsole.ApiClient( configuration=self.nexpose_config) api_client.default_headers['Authorization'] = f'Basic {auth_token}' self.nexpose_admin = rapid7vmconsole.AdministrationApi(api_client) self.nexpose = rapid7vmconsole.ScanApi(api_client) self.nexpose_site = rapid7vmconsole.SiteApi(api_client) self.nexpose_assets = rapid7vmconsole.AssetApi(api_client) self.nexpose_report = rapid7vmconsole.ReportApi(api_client) self.storage_service = StorageService()
config = rapid7vmconsole.Configuration(name='Rapid7') config.username = '******' config.password = '******' config.host = 'https://localhost:3780' config.verify_ssl = False config.assert_hostname = False config.proxy = None config.ssl_ca_cert = None config.connection_pool_maxsize = None config.cert_file = None config.key_file = None # Logging logger = logging.getLogger() logger.setLevel(logging.DEBUG) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.INFO) logger.addHandler(ch) config.debug = False auth = "%s:%s" % (config.username, config.password) auth = base64.b64encode(auth.encode('ascii')).decode() client = rapid7vmconsole.ApiClient(configuration=config) client.default_headers['Authorization'] = "Basic %s" % auth asset_api = rapid7vmconsole.AssetApi(client) assets = asset_api.get_assets() for a in assets.resources: print("Asset ID: %s; Hostname: %s; IP Address: %s" % (a.id, a.host_name, a.ip))