-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_input.py
More file actions
59 lines (42 loc) · 1.9 KB
/
clean_input.py
File metadata and controls
59 lines (42 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import socket
import ipaddress
def verify_input(scanner):
listed = scanner.instructions.split()
if len(listed) != 4 or listed[0] != 'scan' or listed[2] != '-p': # verifys whether input matches format
return 'Instruction unrecognised enter in format: scan 192.168.4.27/domain name -p 34-65635'
address = listed[1] # singles out the IP address
try:
ipaddress.ip_address(address) # verifys ip address is a real address
scanner.computers.append(address)
except ValueError:
try:
new_ip = socket.gethostbyname(address)
print(f'\nIP address {address} resolved to {new_ip}')
print('')
scanner.computers.append(new_ip)
except socket.gaierror:
return f'Invalid IP address {address}'
try:
ports = listed[3].split('-')
if len(ports) != 2:
return 'Invalid port range format. Use: START-END'
scanner.start_port = int(ports[0])
scanner.end_port = int(ports[1])
if scanner.start_port < 1 or scanner.end_port > 65_535:
return 'Ports must be in range 1 ---> 65,535'
if scanner.start_port > scanner.end_port:
return 'Start port must be less than or equal to the end port'
except (ValueError, IndexError):
return 'Invalid port range format. Use: START-END'
if scanner.end_port - scanner.start_port + 1 > 500:
accepted = False
while not accepted:
print('Scanning anything over 500+ ports will take some time')
user_input = input('Do you understand? [Y/n] ').lower()
if user_input == 'y':
accepted = True
elif user_input == 'n':
return 'cancel scan'
else:
return 'Input not understood, please try again'
return True