From 34cc7d1d2656f78243ecb7091f870d29748723e5 Mon Sep 17 00:00:00 2001 From: Victoria Shepard <5807469+demonolock@users.noreply.github.com> Date: Sat, 16 Aug 2025 10:44:03 +0200 Subject: [PATCH] Remove usage of not standard nc in remote_ops.py is_port_free (#284) * Remove usage of not standard nc in remote_ops.py is_port_free We will check the content of /proc/net/tcp instead usage of 'nc' utility. Co-authored-by: vshepard Co-authored-by: d.kovalenko --- testgres/operations/local_ops.py | 2 ++ testgres/operations/remote_ops.py | 46 +++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/testgres/operations/local_ops.py b/testgres/operations/local_ops.py index 99d8e32..c2bed24 100644 --- a/testgres/operations/local_ops.py +++ b/testgres/operations/local_ops.py @@ -583,6 +583,8 @@ def get_process_children(self, pid): def is_port_free(self, number: int) -> bool: assert type(number) == int # noqa: E721 + assert number >= 0 + assert number <= 65535 # OK? with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: try: diff --git a/testgres/operations/remote_ops.py b/testgres/operations/remote_ops.py index 15d78b1..4cdb158 100644 --- a/testgres/operations/remote_ops.py +++ b/testgres/operations/remote_ops.py @@ -10,6 +10,7 @@ import logging import typing import copy +import re from ..exceptions import ExecUtilException from ..exceptions import InvalidOperationException @@ -680,23 +681,45 @@ def get_process_children(self, pid): def is_port_free(self, number: int) -> bool: assert type(number) == int # noqa: E721 + assert number >= 0 + assert number <= 65535 # OK? - cmd = ["nc", "-w", "5", "-z", "-v", "localhost", str(number)] + # grep -q returns 0 if a listening socket on that port is found + port_hex = format(number, '04X') - exit_status, output, error = self.exec_command(cmd=cmd, encoding=get_default_encoding(), ignore_errors=True, verbose=True) + # sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt ... + # 137: 0A01A8C0:EC08 1DA2A959:01BB 01 00000000:00000000 02:00000000 00000000 ... + C_REGEXP = r"^\s*[0-9]+:\s*[0-9a-fA-F]{8}:" + re.escape(port_hex) + r"\s+[0-9a-fA-F]{8}:[0-9a-fA-F]{4}\s+" - assert type(output) == str # noqa: E721 - assert type(error) == str # noqa: E721 + # Search /proc/net/tcp for any entry with this port + # NOTE: grep requires quote string with regular expression + # TODO: added a support for tcp/ip v6 + grep_cmd_s = "grep -q -E \"" + C_REGEXP + "\" /proc/net/tcp" + + cmd = [ + "/bin/bash", + "-c", + grep_cmd_s, + ] + + exit_status, output, error = self.exec_command( + cmd=cmd, + encoding=get_default_encoding(), + ignore_errors=True, + verbose=True + ) + # grep exit 0 -> port is busy if exit_status == 0: - return __class__._is_port_free__process_0(error) + return False + # grep exit 1 -> port is free if exit_status == 1: - return __class__._is_port_free__process_1(error) - - errMsg = "nc returns an unknown result code: {0}".format(exit_status) + return True - RaiseError.CommandExecutionError( + # any other code is an unexpected error + errMsg = f"grep returned unexpected exit code: {exit_status}" + raise RaiseError.CommandExecutionError( cmd=cmd, exit_code=exit_status, message=errMsg, @@ -746,12 +769,7 @@ def _is_port_free__process_0(error: str) -> bool: @staticmethod def _is_port_free__process_1(error: str) -> bool: assert type(error) == str # noqa: E721 - # - # Example of error text: - # "nc: connect to localhost (127.0.0.1) port 1024 (tcp) failed: Connection refused\n" - # # May be here is needed to check error message? - # return True @staticmethod pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy