33from rich .console import Console
44from rich .panel import Panel
55import subprocess
6+ import requests
67import platform
78import argparse
89import shutil
910import sys
11+ import os
1012import re
1113
1214PROGRAM = "port_scanner"
1820
1921
2022def 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
2840def 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
6283def 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