It's easy enough to do with the -z
and -w TIMEOUT
options to nc
, but not all systems have nc
installed. If you have a recent enough version of bash, this will work:
# Connection successful:
$ timeout 1 bash -c 'cat < /dev/null > /dev/tcp/google.com/80'
$ echo $?
0
# Connection failure prior to the timeout
$ timeout 1 bash -c 'cat < /dev/null > /dev/tcp/sfsfdfdff.com/80'
bash: sfsfdfdff.com: Name or service not known
bash: /dev/tcp/sfsfdfdff.com/80: Invalid argument
$ echo $?
1
# Connection not established by the timeout
$ timeout 1 bash -c 'cat < /dev/null > /dev/tcp/google.com/81'
$ echo $?
124
What's happening here is that timeout
will run the subcommand and kill it if it doesn't exit within the specified timeout (1 second in the above example). In this case bash
is the subcommand and uses its special /dev/tcp handling to try and open a connection to the server and port specified. If bash
can open the connection within the timeout, cat
will just close it immediately (since it's reading from /dev/null
) and exit with a status code of 0
which will propagate through bash
and then timeout
. If bash
gets a connection failure prior to the specified timeout, then bash
will exit with an exit code of 1 which timeout
will also return. And if bash isn't able to establish a connection and the specified timeout expires, then timeout
will kill bash
and exit with a status of 124.