11
11
d .set_background_title ('Port Scanner With pythondialog - Encoderpie' )
12
12
d .add_persistent_args (['--no-nl-expand' ])
13
13
14
- # Port scan def
15
- def portScan ():
16
- startPort = 1
17
- endPort = 65535
14
+ # Help menu def
15
+ def helpMenu ():
16
+ d .msgbox ('''
17
+ The scanning process starts when you enter the \
18
+ IP address or hostname and press 'OK'.
19
+ ''' , width = 0 , height = 0 , title = 'Help' )
20
+
21
+ # Default ports
22
+ defaultStartPort = 1
23
+ defaultEndPort = 65534
24
+
25
+ # Port scan
26
+ def portScan (targetInput , startPort , endPort ):
18
27
try :
19
- target = socket .gethostbyname (user_input )
28
+ target = socket .gethostbyname (targetInput )
29
+ scanningPortsText = f'Scanning port(s): { startPort } - { endPort } '
30
+ if (startPort == endPort ):
31
+ scanningPortsText = f'Scanning port: { startPort } '
20
32
try :
21
- gauge_text = '''
22
- Scanning Target : {} \
23
-
24
- Scanning started at: { }
25
- Open ports :
26
- ''' . format ( target , str ( datetime . now ()))
27
- d .gauge_start (text = gauge_text , height = 0 , width = 0 , percent = 0 , title = 'Ports Scanning...' )
33
+ gauge_text = f '''
34
+ Scanning target : { target }
35
+ Scanning started at: { str ( datetime . now ()) }
36
+ { scanningPortsText }
37
+ Open port(s) :
38
+ '''
39
+ d .gauge_start (text = gauge_text , height = 0 , width = 0 , percent = 0 , title = 'Port(s) Scanning...' )
28
40
openPorts = []
29
41
startScanTime = timer ()
30
- for port in range (startPort , endPort ):
42
+ for port in range (startPort , endPort + 1 ):
31
43
s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
32
44
socket .setdefaulttimeout (1 )
33
45
result = s .connect_ex ((target ,port ))
@@ -40,12 +52,16 @@ def portScan():
40
52
d .gauge_update (100 )
41
53
d .gauge_stop ()
42
54
openPortsText = ''
55
+ if (len (openPorts ) == 0 ):
56
+ openPortsText = f'{ openPortsText } \n The open port was not found.'
43
57
for openport in openPorts :
44
58
openPortsText = f'{ openPortsText } \n { str (openport )} is open port'
45
- openPortsText = f'{ openPortsText } \n Port scan process took { str (int (startfinishDiffTime ))} seconds'
46
- msg = f'Open ports: { openPortsText } '
59
+ startEndTimeDiffText = int (startfinishDiffTime )
60
+ if (startEndTimeDiffText > 0 ):
61
+ openPortsText = f'{ openPortsText } \n Port scan process took { str (startEndTimeDiffText )} seconds'
62
+ msg = f'Port scanning completed!\n Open port(s): { openPortsText } '
47
63
except KeyboardInterrupt :
48
- msg = 'Exiting Program .'
64
+ msg = 'Goodbye .'
49
65
except socket .gaierror :
50
66
msg = 'Hostname Could Not Be Resolved.'
51
67
except socket .error :
@@ -55,40 +71,56 @@ def portScan():
55
71
d .msgbox (f'{ msg } \n Exiting!' , width = 0 , height = 0 , title = 'Result' )
56
72
sys .exit (0 )
57
73
58
- # Help menu def
59
- def helpMenu ():
60
- d .msgbox (''' \
74
+ # PORT input menu
75
+ def portInputScreen (ip ):
76
+ userPortFormat = d .menu (f'Target IP/Hostname: { str (ip )} \n Select the port scanning process.' ,
77
+ choices = [
78
+ ('1.' , f'Scan all ports between { defaultStartPort } and { defaultEndPort } ' ),
79
+ ('2.' , 'Scan a single port' ),
80
+ ('3.' , 'Scan all ports in the specified range' )],
81
+ height = None , width = None , menu_height = None , title = 'Port scanning process' )[1 ]
82
+ if (userPortFormat == '1.' ):
83
+ portScan (ip , defaultStartPort , defaultEndPort )
84
+ elif (userPortFormat == '2.' ):
85
+ code , userPortSingle = d .inputbox ('Input port' , init = '80' , width = 0 , height = 0 , title = 'Port' , help_button = True )
86
+ if code == d .OK :
87
+ portScan (ip , int (userPortSingle ), int (userPortSingle ))
88
+ elif (userPortFormat == '3.' ):
89
+ code , userPortRange = d .inputbox ('Input port range' , init = '80 443' , width = 0 , height = 0 , title = 'Port range' , help_button = True )
90
+ if code == d .OK :
91
+ portRangeParsed = userPortRange .split (' ' )
92
+ portScan (ip , int (portRangeParsed [0 ]), int (portRangeParsed [1 ]) - 1 )
93
+ if code == d .CANCEL :
94
+ msg = 'You pressed the Cancel button in the previous menu.'
95
+ elif code == d .ESC :
96
+ msg = 'You pressed the Escape key in the previous menu.'
97
+ elif code == d .HELP :
98
+ helpMenu ()
99
+ d .msgbox (f'{ msg } \n Exiting!' , width = 0 , height = 0 , title = 'Exiting' )
100
+ sys .exit (0 )
61
101
62
- Welcome to Port Scanner With pythondialog, \
63
- Press the 'OK' button at the bottom to start scanning the IP, \
64
- press the 'help' button at the bottom to get help.
65
- ''' , width = 0 , height = 0 , title = 'Help' )
102
+ # IP input menu
103
+ def ipInputScreen ():
104
+ code , target = d .inputbox ('Input target IP or Host address' , init = '192.168.' , width = 0 , height = 0 , title = 'IP or Host name' , help_button = True )
105
+ if code == d .OK :
106
+ portInputScreen (target )
107
+ elif code == d .CANCEL :
108
+ msg = 'You pressed the Cancel button in the previous menu.'
109
+ elif code == d .ESC :
110
+ msg = 'You pressed the Escape key in the previous menu.'
111
+ elif code == d .HELP :
112
+ helpMenu ()
113
+ d .msgbox (f'{ msg } \n Exiting!' , width = 0 , height = 0 , title = 'Exiting' )
114
+ sys .exit (0 )
66
115
67
116
# Welcome screen
68
- welcomeScreen = d .msgbox (''' \
69
-
117
+ welcomeScreen = d .msgbox ('''
70
118
Welcome to Port Scanner With pythondialog, \
71
119
Press the 'OK' button at the bottom to start scanning the IP, \
72
120
press the 'help' button at the bottom to get help.
73
121
''' , width = 0 , height = 0 , title = 'Port Scanner With pythondialog' , help_button = True )
74
-
75
- if welcomeScreen == d .HELP :
122
+ if welcomeScreen == d .OK :
123
+ ipInputScreen ()
124
+ elif welcomeScreen == d .HELP :
76
125
helpMenu ()
77
-
78
- # IP, PORT input
79
- code , user_input = d .inputbox ('Input target IP or Host address' , init = '192.168.' , width = 0 , height = 0 , title = 'IP or Host name' , help_button = True )
80
-
81
- if code == d .OK :
82
- portScan ()
83
- elif code == d .CANCEL :
84
- msg = 'You chose the Cancel button in the previous dialog.'
85
- elif code == d .ESC :
86
- msg = 'You pressed the Escape key in the previous dialog.'
87
- elif code == d .HELP :
88
- helpMenu ()
89
- else :
90
- msg = 'Unexpected exit code from d.inputbox(). Please report a bug.'
91
-
92
- d .msgbox (f'{ msg } \n Exiting!' , width = 0 , height = 0 , title = 'Exiting' )
93
-
94
126
sys .exit (0 )
0 commit comments