Skip to content

Commit 0f73bc7

Browse files
committed
feat(cli): Added virtual hosts argument to search
1 parent 54791e7 commit 0f73bc7

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

censys/cli/commands/search.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ def cli_search(args: argparse.Namespace):
101101
if args.pages:
102102
search_args["pages"] = args.pages
103103

104+
if args.virtual_hosts:
105+
search_args["virtual_hosts"] = args.virtual_hosts
106+
104107
with err_console.status("Searching"):
105108
query = index.search(args.query, **search_args)
106109

@@ -183,6 +186,14 @@ def include(parent_parser: argparse._SubParsersAction, parents: dict):
183186
type=int,
184187
help="number of pages of results to return (when set to -1 returns all pages available)",
185188
)
189+
v2_group.add_argument(
190+
"--virtual-hosts",
191+
type=str,
192+
default="EXCLUDE",
193+
choices=["INCLUDE", "EXCLUDE", "ONLY"],
194+
metavar="INCLUDE|EXCLUDE|ONLY",
195+
help="whether to include virtual hosts in the results",
196+
)
186197

187198
v1_group = search_parser.add_argument_group(
188199
f"v1 specific arguments ({', '.join(V1_INDEXES)})"

tests/cli/test_search.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,42 @@ def test_max_records(self):
341341
def test_write_screen_v2(self):
342342
self.responses.add(
343343
responses.GET,
344-
V2_URL + "/hosts/search?q=service.service_name: HTTP&per_page=100",
344+
V2_URL
345+
+ "/hosts/search?q=service.service_name: HTTP&per_page=100&virtual_hosts=EXCLUDE",
346+
status=200,
347+
json=SEARCH_HOSTS_JSON,
348+
)
349+
350+
temp_stdout = StringIO()
351+
with contextlib.redirect_stdout(temp_stdout):
352+
cli_main()
353+
354+
json_response = json.loads(temp_stdout.getvalue().strip())
355+
356+
assert json_response == SEARCH_HOSTS_JSON["result"]["hits"]
357+
358+
@patch(
359+
"argparse._sys.argv",
360+
[
361+
"censys",
362+
"search",
363+
"service.service_name: HTTP",
364+
"--index-type",
365+
"hosts",
366+
"--format",
367+
"screen",
368+
"--pages",
369+
"1",
370+
"--virtual-hosts",
371+
"ONLY",
372+
]
373+
+ CensysTestCase.cli_args,
374+
)
375+
def test_search_virtual_hosts(self):
376+
self.responses.add(
377+
responses.GET,
378+
V2_URL
379+
+ "/hosts/search?q=service.service_name%3A+HTTP&per_page=100&virtual_hosts=ONLY",
345380
status=200,
346381
json=SEARCH_HOSTS_JSON,
347382
)

0 commit comments

Comments
 (0)