Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decrease TCP connect() system call timeout?

In command below I enable file /dev/tcp/10.10.10.1/80 both for reading and writing and associate it with file descriptor 3:

$ time exec 3<>/dev/tcp/10.10.10.1/80
bash: connect: Operation timed out
bash: /dev/tcp/10.10.10.1/80: Operation timed out

real    1m15.151s
user    0m0.000s
sys     0m0.000s

This automatically tries to perform TCP three-way handshake. If 10.10.10.1 is not reachable as in example above, then connect system call tries to connect for 75 seconds. Is this 75 second timeout determined by bash? Or is this system default? Last but not least, is there a way to decrease this timeout value?

like image 476
Martin Avatar asked Jun 19 '14 22:06

Martin


1 Answers

It's not possible in Bash without modifying the source as already mentioned, although here is the workaround by using timeout command, e.g.:

$ timeout 1 bash -c "</dev/tcp/stackoverflow.com/80" && echo Port open. || echo Port closed.
Port open.
$ timeout 1 bash -c "</dev/tcp/stackoverflow.com/81" && echo Port open. || echo Port closed.
Port closed.

Using this syntax, the timeout command will kill the process after the given time.

See: timeout --help for more options.

like image 172
kenorb Avatar answered Sep 19 '22 03:09

kenorb