Parse tool output and make an empty parse loud

short · 50 min · Objective 2.4

Task

Write a small script that turns one tool's structured output into another's input, and make it fail loudly when it parses nothing — because a pipeline that yields nothing when the format changed looks identical to one that yields nothing because the target was clean.

Steps

  1. Take the structured (XML) scan output you saved in the Nmap lab, from a host on the lab network you own.
  2. Write /tmp/parse.sh (or a Python equivalent) that extracts open ports and services into a clean list another tool could consume.
  3. Make it fail loudly on an empty parse: if it reads a non-empty input file but extracts zero records, it must print a distinct error and exit non-zero, not print nothing.
  4. Test the loud-failure path: feed it a file in the wrong format and confirm it says so rather than returning a clean-looking empty result.
  5. Test the success path: feed it the real scan output and confirm it extracts the ports you know are open.

Verify

bash /tmp/parse.sh /tmp/scan.xml | wc -l
bash /tmp/parse.sh /dev/null; echo "empty-input exit: $?"
printf 'not xml at all\n' > /tmp/bad.txt; bash /tmp/parse.sh /tmp/bad.txt; echo "bad-input exit: $?"

The first count must be non-zero: the script extracted ports from real output. The second and third exit codes must be non-zero: an empty input and a malformed input both make the script fail loudly rather than return a clean-looking zero. If either exit is 0 with no output, the script cannot tell "nothing there" from "I could not read it", which is the exact failure this lab exists to prevent.

Notes

An empty parse that looks like a clean result is the "not measured is a third result" problem in miniature — the script must distinguish "I looked and found nothing" from "I did not look". Prefer structured output so this step is parsing rather than scraping; pin versions when you must scrape, because a scraper silently breaks when the format shifts. The input here came from a host you own.

This is an independent study companion for CompTIA PenTest+ PT0-003 and is not produced by or endorsed by CompTIA.