From 1d13c5ea9e9c1f6fd8ef1d331ea2611b01a97bcc Mon Sep 17 00:00:00 2001 From: Tobias Waldekranz Date: Wed, 22 Jan 2025 20:16:47 +0100 Subject: [PATCH] test: infamy: Generalize netconf_syn() to test for any TCP service Being able to check for a TCP based service is generally useful, not only for NETCONF. Therefore, break it out to a separate function that allows any port to be queried. --- test/infamy/netconf.py | 10 +++------- test/infamy/netutil.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 test/infamy/netutil.py diff --git a/test/infamy/netconf.py b/test/infamy/netconf.py index bceba56c..1bb1f040 100644 --- a/test/infamy/netconf.py +++ b/test/infamy/netconf.py @@ -17,18 +17,14 @@ import netconf_client.connect import netconf_client.ncclient from infamy.transport import Transport,infer_put_dict from netconf_client.error import RpcError -from . import env +from . import env, netutil def netconf_syn(addr): - try: - ai = socket.getaddrinfo(addr, 830, 0, 0, socket.SOL_TCP) - sock = socket.socket(ai[0][0], ai[0][1], 0) - sock.connect(ai[0][4]) - sock.close() + if netutil.tcp_port_is_open(addr, 830): print(f"{addr} answers to TCP connections on port 830 (NETCONF)") return True - except Exception: + else: return False diff --git a/test/infamy/netutil.py b/test/infamy/netutil.py new file mode 100644 index 00000000..115d6e94 --- /dev/null +++ b/test/infamy/netutil.py @@ -0,0 +1,11 @@ +import socket + +def tcp_port_is_open(host, port): + try: + ai = socket.getaddrinfo(host, port, 0, 0, socket.SOL_TCP) + sock = socket.socket(ai[0][0], ai[0][1], 0) + sock.connect(ai[0][4]) + sock.close() + return True + except Exception: + return False