summaryrefslogtreecommitdiff
path: root/src/validators/port-multi
diff options
context:
space:
mode:
authorkumvijaya <kumvijaya@gmail.com>2024-05-21 16:41:14 +0530
committerkumvijaya <kumvijaya@gmail.com>2024-05-21 16:41:14 +0530
commitcc86483fdf7a6bd988f485c06402fd07368dd26e (patch)
tree9d892a9715106cc67bf1e57b15b999aa7e564057 /src/validators/port-multi
parent704ca2322d0bebcb923f5136f0f69fb23651a484 (diff)
downloadvyos-workflow-test-temp-cc86483fdf7a6bd988f485c06402fd07368dd26e.tar.gz
vyos-workflow-test-temp-cc86483fdf7a6bd988f485c06402fd07368dd26e.zip
T6357: create test repository to validate setup
Diffstat (limited to 'src/validators/port-multi')
-rw-r--r--src/validators/port-multi52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/validators/port-multi b/src/validators/port-multi
new file mode 100644
index 0000000..ed6ff68
--- /dev/null
+++ b/src/validators/port-multi
@@ -0,0 +1,52 @@
+#!/usr/bin/python3
+
+from sys import argv
+from sys import exit
+import re
+
+from vyos.utils.file import read_file
+
+services_file = '/etc/services'
+
+def get_services():
+ names = []
+ service_data = read_file(services_file, "")
+ for line in service_data.split("\n"):
+ if not line or line[0] == '#':
+ continue
+ tmp = line.split()
+ names.append(tmp[0])
+ if len(tmp) > 2:
+ # Add port aliases to service list, too
+ names.extend(tmp[2:])
+ # remove duplicate entries (e.g. echo) from list
+ names = list(dict.fromkeys(names))
+ return names
+
+if __name__ == '__main__':
+ if len(argv)>1:
+ ports = argv[1].split(",")
+ services = get_services()
+
+ for port in ports:
+ if port and port[0] == '!':
+ port = port[1:]
+ if re.match('^[0-9]{1,5}-[0-9]{1,5}$', port):
+ port_1, port_2 = port.split('-')
+ if int(port_1) not in range(1, 65536) or int(port_2) not in range(1, 65536):
+ print(f'Error: {port} is not a valid port range')
+ exit(1)
+ if int(port_1) > int(port_2):
+ print(f'Error: {port} is not a valid port range')
+ exit(1)
+ elif port.isnumeric():
+ if int(port) not in range(1, 65536):
+ print(f'Error: {port} is not a valid port')
+ exit(1)
+ elif port not in services:
+ print(f'Error: {port} is not a valid service name')
+ exit(1)
+ else:
+ exit(2)
+
+ exit(0)