What is the best way to measure latency via a "telnet to port" test, preferably with built-in Linux tools?

I'm writing a Graphite/Diamond collector measuring network latency. Specifically it should measure the time it takes to open up a connection to a port on a remote server. And it needs to work via script, i.e. no humans on the keyboard running this interactively.

To give an example with a fictional parameter to telnet which would do exactly what I need.

time telnet --close-connection-immediately-after-established somehost.somedomain.com 1234

Trying somehost.somedomain.com...
Connected to somehost.somedomain.com.
Escape character is '^]'.
Connection closed automatically after established.

real    0m3.978s
user    0m0.001s
sys 0m0.003s

Basically, what's a command-line way to output 3.978 given the example above using only builtin tools?

You could wrap the telnet in an expect script I suppose and have it issue:

^]
close

... but that seems rather ugly. Any ideas?


What about :

time nc -zw30 <host> <port>