Linux Network Port Watching

This issue is from a machine without net-tools.x86_64 : Basic networking tools pre-installed, so netstat command does not exist. When watching docker registry pod setup, ansible runs netstat -tunlp | grep 5000 to check 5000 port, failed.

Is there other way around to check if the 5000 port is up or not? Yes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- name: Wait for docker registry to come up
any_errors_fatal: true
shell: |
declare -a array=($(cat /proc/net/tcp6 | cut -d":" -f"3"|cut -d" " -f"1"))
for port in ${array[@]};
do
# $((0x$port)) is 16 based, will output 10 based
val=$(echo $((0x$port)) | grep 5000)
if ! [[ "X${val}" == "X" ]]; then
break
fi
done
echo ${val} | grep 5000
register: docker_registry
until: docker_registry.rc == 0
retries: "{{ 10 }}"

Here I watch /proc/net/tcp6 kernel file to see what ipv6 port is running (docker registry port is in ipv6 scope here). for ipv4, use /proc/net/tcp.

This file is not plain text, after use cut to extra the port field, then convert the number to decimal. see https://www.kernel.org/doc/Documentation/networking/proc_net_tcp.txt

0%