55import subprocess
66import platform
77import argparse
8- import requests
98import shutil
109import sys
11- import os
1210import re
1311
1412PROGRAM = "port_scanner"
2119
2220def update ():
2321 install_nmap (force = True )
24- subprocess .run (["pipx" , "install" , "--force" , "git+https://github.com/batubyte/port-scanner" ], check = True )
25-
26-
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 ]
22+ subprocess .run (
23+ ["pipx" , "install" , "--force" , "git+https://github.com/batubyte/port-scanner" ],
24+ check = True ,
25+ )
3526
3627
3728def install_nmap (force = False ):
3829 if not force and shutil .which ("nmap" ):
3930 return
40-
31+
4132 if not force :
4233 answer = input ("Nmap not found. Install? [Y/n]: " ).strip ().lower ()
4334 if answer not in ("" , "y" , "yes" ):
44- sys . exit ( 1 )
35+ return
4536
4637 system = platform .system ()
4738 if system == "Linux" :
@@ -53,27 +44,19 @@ def install_nmap(force=False):
5344 elif shutil .which ("yum" ):
5445 subprocess .run (["sudo" , "yum" , "install" , "-y" , "nmap" ], check = True )
5546 else :
56- error_console .print ("No supported package manager found. Please install nmap manually." )
57- sys .exit (1 )
47+ error_console .print (
48+ "No supported package manager found. Please install nmap manually."
49+ )
5850
5951 elif system == "Windows" :
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." )
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+ )
7760
7861
7962def run_nmap (args ):
@@ -91,38 +74,42 @@ def run_nmap(args):
9174 styled_output = "\n " .join (colored_output )
9275
9376 console .print (
94- Panel (
95- styled_output ,
96- title = "nmap " + " " .join (args ),
97- border_style = "cyan"
98- )
77+ Panel (styled_output , title = "nmap " + " " .join (args ), border_style = "cyan" )
9978 )
10079
10180
10281def parse_args (parser ):
10382 parser .add_argument (
10483 "-v" , "--version" , action = "version" , version = f"%(prog)s version { VERSION } "
10584 )
106- parser .add_argument ("-h" , "--help" , action = "store_true" , help = "show this help message" )
107- parser .add_argument ("-u" , "--update" , action = "store_true" , help = "update nmap and this program" )
108- parser .add_argument ("-n" , "--nmap" , nargs = argparse .REMAINDER , help = "run nmap with custom arguments" )
85+ parser .add_argument (
86+ "-h" , "--help" , action = "store_true" , help = "show this help message"
87+ )
88+ parser .add_argument (
89+ "-u" , "--update" , action = "store_true" , help = "update nmap and this program"
90+ )
91+ parser .add_argument (
92+ "-n" , "--nmap" , nargs = argparse .REMAINDER , help = "run nmap with custom arguments"
93+ )
10994 return parser .parse_args ()
11095
11196
11297def main ():
113- parser = argparse .ArgumentParser (prog = PROGRAM , description = DESCRIPTION , add_help = False )
98+ parser = argparse .ArgumentParser (
99+ prog = PROGRAM , description = DESCRIPTION , add_help = False
100+ )
114101 args = parse_args (parser )
115-
102+
116103 if len (sys .argv ) == 1 or args .help :
117104 console .print (
118105 Panel (
119106 parser .format_help (),
120- title = ' ' .join (sys .argv ),
107+ title = " " .join (sys .argv ),
121108 border_style = "cyan" ,
122109 )
123110 )
124111 return
125-
112+
126113 if args .update :
127114 update ()
128115
0 commit comments