Skip to content

Commit db92297

Browse files
code reformat & version bump
1 parent 560a860 commit db92297

3 files changed

Lines changed: 16 additions & 11 deletions

File tree

src/nmapp/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.0"
1+
__version__ = "1.1"

src/nmapp/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
from nmapp import util
33

4+
45
def main():
56
try:
67
nmap_original_output = sys.stdin.read()
@@ -13,5 +14,6 @@ def main():
1314
print(f"Error: {e}", file=sys.stderr)
1415
sys.exit(1)
1516

17+
1618
if __name__ == "__main__":
1719
main()

src/nmapp/util.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
import xml.etree.ElementTree as ET
33
from typing import Dict, List
44

5+
56
class NmapOriginalFormat(Enum):
67
NORMAL = 1
78
XML = 2
89
GREPABLE = 3
910
LOOKS_INVALID = 10
1011

12+
1113
def detect_format(nmap_original_output: str) -> NmapOriginalFormat:
1214
if nmap_original_output.find("Starting Nmap ") >= 0:
1315
return NmapOriginalFormat.NORMAL
@@ -17,6 +19,7 @@ def detect_format(nmap_original_output: str) -> NmapOriginalFormat:
1719
return NmapOriginalFormat.GREPABLE
1820
return NmapOriginalFormat.LOOKS_INVALID
1921

22+
2023
def reformat_to_markdown(nmap_original_output: str) -> str:
2124
if detect_format(nmap_original_output) == NmapOriginalFormat.LOOKS_INVALID:
2225
raise ValueError("The provided input looks invalid")
@@ -29,39 +32,39 @@ def reformat_to_markdown(nmap_original_output: str) -> str:
2932
raise NotImplementedError("Only XML input is supported for now")
3033
return reformat_dict_to_markdown(nmap_open_ports)
3134

35+
3236
def reformat_xml_to_dict(nmap_original_output: str) -> dict:
33-
"""Parse Nmap XML output and return a dict of address -> sorted list of open ports (ints).
34-
"""
37+
"""Parse Nmap XML output and return a dict of address -> sorted list of open ports (ints)."""
3538
try:
3639
root = ET.fromstring(nmap_original_output)
3740
except ET.ParseError as exc:
3841
raise ValueError(f"Failed to parse XML input: {exc}")
3942

4043
hosts_ports: Dict[str, set] = {}
4144

42-
for host in root.findall('host'):
43-
addrs = [a.get('addr') for a in host.findall('address') if a.get('addr')]
45+
for host in root.findall("host"):
46+
addrs = [a.get("addr") for a in host.findall("address") if a.get("addr")]
4447
if not addrs:
4548
continue
4649

4750
# Sort addresses
4851
addrs.sort()
4952

5053
# Join into a comma-separated string
51-
addr = ','.join(addrs)
54+
addr = ",".join(addrs)
5255

5356
# find ports
54-
ports_elem = host.find('ports')
57+
ports_elem = host.find("ports")
5558
if ports_elem is None:
5659
continue
5760

58-
for port in ports_elem.findall('port'):
59-
state = port.find('state')
61+
for port in ports_elem.findall("port"):
62+
state = port.find("state")
6063
if state is None:
6164
continue
62-
if state.get('state') != 'open':
65+
if state.get("state") != "open":
6366
continue
64-
portid = port.get('portid')
67+
portid = port.get("portid")
6568
if not portid:
6669
continue
6770
try:

0 commit comments

Comments
 (0)