55import subprocess
66import platform
77import argparse
8+ import requests
89import shutil
910import sys
11+ import os
1012import re
1113
1214PROGRAM = "port_scanner"
@@ -22,13 +24,24 @@ def update():
2224 subprocess .run (["pipx" , "install" , "--force" , "git+https://github.com/batubyte/port-scanner" ], check = True )
2325
2426
27+ def get_latest_nmap_url ():
28+ url = "https://nmap.org/dist/"
29+ resp = requests .get (url )
30+ links = re .findall (r'href="(nmap-(\d+\.\d+)-setup\.exe)"' , resp .text )
31+ if not links :
32+ return None
33+ latest = max (links , key = lambda x : tuple (map (int , x [1 ].split ('.' ))))
34+ return url + latest [0 ]
35+
36+
2537def install_nmap (force = False ):
2638 if not force and shutil .which ("nmap" ):
2739 return
28-
29- answer = input ("Nmap not found. Install? [Y/n]: " ).strip ().lower ()
30- if answer not in ("" , "y" , "yes" ):
31- sys .exit (1 )
40+
41+ if not force :
42+ answer = input ("Nmap not found. Install? [Y/n]: " ).strip ().lower ()
43+ if answer not in ("" , "y" , "yes" ):
44+ sys .exit (1 )
3245
3346 system = platform .system ()
3447 if system == "Linux" :
@@ -40,12 +53,27 @@ def install_nmap(force=False):
4053 elif shutil .which ("yum" ):
4154 subprocess .run (["sudo" , "yum" , "install" , "-y" , "nmap" ], check = True )
4255 else :
43- console .print ("No supported package manager found. Install nmap manually." , style = "bold red " )
56+ error_console .print ("No supported package manager found. Please install nmap manually." )
4457 sys .exit (1 )
4558
4659 elif system == "Windows" :
47- # install nmap sliently
48- sys .exit (1 )
60+ url = get_latest_nmap_url ()
61+ if not url :
62+ error_console .print ("Failed to find the latest Nmap installer URL." )
63+ sys .exit (1 )
64+
65+ tmp_dir = os .environ .get ("TEMP" , "/tmp" )
66+ installer_path = os .path .join (tmp_dir , "nmap-setup.exe" )
67+
68+ console .print (f"Downloading Nmap from { url } " )
69+ with requests .get (url , stream = True ) as r :
70+ r .raise_for_status ()
71+ with open (installer_path , 'wb' ) as f :
72+ for chunk in r .iter_content (chunk_size = 8192 ):
73+ f .write (chunk )
74+
75+ subprocess .Popen (["start" , "" , installer_path ], shell = True )
76+ console .print ("Please complete the Nmap installation manually." )
4977
5078
5179def run_nmap (args ):
@@ -112,5 +140,5 @@ def main():
112140 except KeyboardInterrupt :
113141 sys .exit (130 )
114142 except Exception as e :
115- error_console .print (f"Error: { e } " )
143+ error_console .log (f"Error: { e } " )
116144 sys .exit (1 )
0 commit comments