diff options
author | Christian Breunig <christian@breunig.cc> | 2023-07-15 20:12:56 +0200 |
---|---|---|
committer | Christian Breunig <christian@breunig.cc> | 2023-07-15 20:13:12 +0200 |
commit | 5f77ccf91eb402c548fc91b2e080a4b2b86f4181 (patch) | |
tree | 9b926dedc07ef547ad8bbe539f89990249552414 /python/vyos/utils/network.py | |
parent | 9285b9a571ee944daf6f17847a62f115146834a4 (diff) | |
download | vyos-1x-5f77ccf91eb402c548fc91b2e080a4b2b86f4181.tar.gz vyos-1x-5f77ccf91eb402c548fc91b2e080a4b2b86f4181.zip |
T5195: vyos.util -> vyos.utils package refactoring part #2
Diffstat (limited to 'python/vyos/utils/network.py')
-rw-r--r-- | python/vyos/utils/network.py | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/python/vyos/utils/network.py b/python/vyos/utils/network.py index 8db598f05..3786caf26 100644 --- a/python/vyos/utils/network.py +++ b/python/vyos/utils/network.py @@ -154,7 +154,6 @@ def mac2eui64(mac, prefix=None): except: # pylint: disable=bare-except return - def check_port_availability(ipaddress, port, protocol): """ Check if port is available and not used by any service @@ -189,3 +188,26 @@ def check_port_availability(ipaddress, port, protocol): return False return True + +def is_listen_port_bind_service(port: int, service: str) -> bool: + """Check if listen port bound to expected program name + :param port: Bind port + :param service: Program name + :return: bool + + Example: + % is_listen_port_bind_service(443, 'nginx') + True + % is_listen_port_bind_service(443, 'ocserv-main') + False + """ + from psutil import net_connections as connections + from psutil import Process as process + for connection in connections(): + addr = connection.laddr + pid = connection.pid + pid_name = process(pid).name() + pid_port = addr.port + if service == pid_name and port == pid_port: + return True + return False |