Skip to content

Commit dbe98af

Browse files
authored
Update port_scanner.py
1 parent f9ad7d1 commit dbe98af

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

port_scanner.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
from rich.console import Console
44
from rich.panel import Panel
55
import subprocess
6+
import requests
67
import platform
78
import argparse
89
import shutil
910
import sys
11+
import os
1012
import re
1113

1214
PROGRAM = "port_scanner"
@@ -18,11 +20,21 @@
1820

1921

2022
def update():
21-
install_nmap(force=True)
2223
subprocess.run(
2324
["pipx", "install", "--force", "git+https://github.com/batubyte/port-scanner"],
2425
check=True,
2526
)
27+
install_nmap(force=True)
28+
29+
30+
def get_latest_nmap_url():
31+
url = "https://nmap.org/dist/"
32+
resp = requests.get(url)
33+
links = re.findall(r'href="(nmap-(\d+\.\d+)-setup\.exe)"', resp.text)
34+
if not links:
35+
return None
36+
latest = max(links, key=lambda x: tuple(map(int, x[1].split('.'))))
37+
return url + latest[0]
2638

2739

2840
def install_nmap(force=False):
@@ -49,14 +61,23 @@ def install_nmap(force=False):
4961
)
5062

5163
elif system == "Windows":
52-
if shutil.which("winget"):
53-
subprocess.run(
54-
["winget", "install", "--id=Insecure.Nmap", "-e"], check=True
55-
)
56-
else:
57-
error_console.print(
58-
"Winget not found. Install it from https://aka.ms/getwinget"
59-
)
64+
url = get_latest_nmap_url()
65+
if not url:
66+
error_console.log("Failed to find the latest Nmap installer URL.")
67+
sys.exit(1)
68+
69+
tmp_dir = os.environ.get("TEMP", "/tmp")
70+
installer_path = os.path.join(tmp_dir, "nmap-setup.exe")
71+
72+
console.print(f"Downloading {url}")
73+
with requests.get(url, stream=True) as r:
74+
r.raise_for_status()
75+
with open(installer_path, 'wb') as f:
76+
for chunk in r.iter_content(chunk_size=8192):
77+
f.write(chunk)
78+
79+
subprocess.Popen(["start", "", installer_path], shell=True)
80+
console.print("Please complete the Nmap installation manually.")
6081

6182

6283
def run_nmap(args):
@@ -86,7 +107,7 @@ def parse_args(parser):
86107
"-h", "--help", action="store_true", help="show this help message"
87108
)
88109
parser.add_argument(
89-
"-u", "--update", action="store_true", help="update nmap and this program"
110+
"-u", "--update", action="store_true", help="update port-scanner and nmap"
90111
)
91112
parser.add_argument(
92113
"-n", "--nmap", nargs=argparse.REMAINDER, help="run nmap with custom arguments"

0 commit comments

Comments
 (0)