Hello,

There is a list of servers that need to call nc (ping is not available), how to set the -w parameter to a millisecond value?

-w timeout If a connection and stdin are idle for more than timeout seconds, then the connection is silently closed. 

This is part of the “fact” for Ansilbe with up to 10 servers that need to be checked from each managed node (up to 3000), if you wait for a second each, you get 10 x 3000 seconds of downtime.

  • I was once interested in the same question, but it didn’t come further than “it is necessary to patch”. - 0andriy
  • if you want to access 10 servers from each of the 3000 nodes (tcp connect () or something else?), then why is “10 x 3000 seconds idle”? What prevents to run simultaneously on different nodes of the command? What prevents on one node more than one connection to open at the same time? - jfs
  • @jfs wrote a question at night :-), processing goes in parallel on 100. - Hellseher
  • nmap here taxis. there is also a parallelization and a simple check tcp-syn - eri
  • @eri. Looked better, you need to limit the waiting time if the server is not accessible to 0.1s - Hellseher

2 answers 2

nc -w does not support milliseconds. Looking at the netcat-openbsd implementation :

  case 'w': timeout = strtonum(optarg, 0, INT_MAX / 1000, &errstr); if (errstr) errx(1, "timeout %s: %s", errstr, optarg); timeout *= 1000; break; 

that is, -w takes an integer number of seconds and turns them into milliseconds (but other parts of the code can also only wait whole seconds).

Depending on what you want to do, you can implement it using other utilities such as socat, nmap.

  • Thanks for socat, read doc. It is necessary to collect the metrics that all 10 servers are available for a node on port 3346, thereby checking the activity of the servers themselves and the VPN tunnels on the firewall through which they are accessed. - Hellseher
  • Wrapped through timeout 0.1 nc -vz server.com 443 - Hellseher

Found a timeout wrapper (GNU coreutils)

Some tests:

 ~$ time nc -vzw1 8.8.8.8 53 || echo "down or notactive" Connection to 8.8.8.8 53 port [tcp/domain] succeeded! real 0m0.015s user 0m0.000s sys 0m0.000s ~$ time nc -vzw1 8.8.8.3 53 || echo "down or not active" nc: connect to 8.8.8.3 port 53 (tcp) timed out: Operation now in progress real 0m1.003s user 0m0.000s sys 0m0.000s down or not active ~$ time timeout 0.1 nc -vzw1 8.8.8.8 53 || echo "down or not active" Connection to 8.8.8.8 53 port [tcp/domain] succeeded! real 0m0.018s user 0m0.000s sys 0m0.000s ~$ time timeout 0.1 nc -vzw1 8.8.8.3 53 || echo "down or not active" real 0m0.101s user 0m0.000s sys 0m0.000s down or not active 

And a working bash script:

 #!/usr/bin/env bash SERVER_LIST_PATH="/home/login_user/server_list" server_active() { [ -e /tmp/srv_active ] && rm /tmp/srv_active while read -r srv; do timeout 0.1 nc -vz "$(echo "$srv" | tr ':' ' ')" &>/dev/null if [[ "$?" = 0 ]]; then echo "$srv" > /tmp/srv_active fi done < "$SERVER_LIST_PATH" if [[ -e /tmp/srv_active ]]; then printf '%d' "$(wc -l /tmp/srv_active)" else printf '0' fi [ -e /tmp/srv_active ] && rm /tmp/srv_active } main() { server_active } main "$@"