Skip to content

Commit a02f772

Browse files
committed
[ ADD ] Added port range specification and single scan feature.
1 parent 8223278 commit a02f772

File tree

1 file changed

+76
-44
lines changed

1 file changed

+76
-44
lines changed

scan.py

Lines changed: 76 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,35 @@
1111
d.set_background_title('Port Scanner With pythondialog - Encoderpie')
1212
d.add_persistent_args(['--no-nl-expand'])
1313

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):
1827
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}'
2032
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...')
2840
openPorts = []
2941
startScanTime = timer()
30-
for port in range(startPort, endPort):
42+
for port in range(startPort, endPort + 1):
3143
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3244
socket.setdefaulttimeout(1)
3345
result = s.connect_ex((target,port))
@@ -40,12 +52,16 @@ def portScan():
4052
d.gauge_update(100)
4153
d.gauge_stop()
4254
openPortsText = ''
55+
if (len(openPorts) == 0):
56+
openPortsText = f'{openPortsText}\n The open port was not found.'
4357
for openport in openPorts:
4458
openPortsText = f'{openPortsText}\n {str(openport)} is open port'
45-
openPortsText = f'{openPortsText}\nPort 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}\nPort scan process took {str(startEndTimeDiffText)} seconds'
62+
msg = f'Port scanning completed!\nOpen port(s): {openPortsText}'
4763
except KeyboardInterrupt:
48-
msg = 'Exiting Program.'
64+
msg = 'Goodbye.'
4965
except socket.gaierror:
5066
msg = 'Hostname Could Not Be Resolved.'
5167
except socket.error:
@@ -55,40 +71,56 @@ def portScan():
5571
d.msgbox(f'{msg}\nExiting!', width=0, height=0, title='Result')
5672
sys.exit(0)
5773

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)}\nSelect 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}\nExiting!', width=0, height=0, title='Exiting')
100+
sys.exit(0)
61101

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}\nExiting!', width=0, height=0, title='Exiting')
114+
sys.exit(0)
66115

67116
# Welcome screen
68-
welcomeScreen = d.msgbox(''' \
69-
117+
welcomeScreen = d.msgbox('''
70118
Welcome to Port Scanner With pythondialog, \
71119
Press the 'OK' button at the bottom to start scanning the IP, \
72120
press the 'help' button at the bottom to get help.
73121
''', 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:
76125
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}\nExiting!', width=0, height=0, title='Exiting')
93-
94126
sys.exit(0)

0 commit comments

Comments
 (0)