summaryrefslogtreecommitdiff
path: root/src/validators/port-range
blob: 6c01048f0f6330e43f39ff400b591358726772dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/python3

import sys
import re

def error(port_range):
    print(f'Error: {port_range} is not a valid port or port range')
    sys.exit(1)

if __name__ == '__main__':
    if len(sys.argv)>1:
        port_range = sys.argv[1]
        if re.match('^[0-9]{1,5}-[0-9]{1,5}$', port_range):
            port_1, port_2 = port_range.split('-')
            if int(port_1) not in range(1, 65536) or int(port_2) not in range(1, 65536):
                error(port_range)
            if int(port_1) > int(port_2):
                error(port_range)
        elif not port_range.isnumeric() or int(port_range) not in range(1, 65536):
                error(port_range)
    else:
        sys.exit(2)

    sys.exit(0)