26 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03#4# Helper functions5 6wait_local_port_listen()7{8 local listener_ns="${1}"9 local port="${2}"10 local protocol="${3}"11 local pattern12 local i13 14 pattern=":$(printf "%04X" "${port}") "15 16 # for tcp protocol additionally check the socket state17 [ ${protocol} = "tcp" ] && pattern="${pattern}0A"18 for i in $(seq 10); do19 if ip netns exec "${listener_ns}" awk '{print $2" "$4}' \20 /proc/net/"${protocol}"* | grep -q "${pattern}"; then21 break22 fi23 sleep 0.124 done25}26