Close enough:
timeout -5s "ssh $line true"
That is:
Use correct quotes.
timeout `ssh …`
cannot possibly work here, because it means "run 'ssh', capture its output, then give the output to 'timeout'."Run ssh in "batch" mode, that is, give it a command to run. Without it, ssh will run in interactive mode, and will hang forever even on successful connections.
You could also try:
ssh -o ConnectTimeout=5s $line true