Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i reduce timeout on unix telnet on connection

Tags:

I have a unix shell script which test ftp ports of multiple hosts listed in a file.

for i in `cat ftp-hosts.txt`         do         echo "QUIT" | telnet $i 21 done 

In general this scripts works, however if i encounter a host which does not connect, i.e telnet is "Trying...", how can I reduce this wait time so it can test the next host ?

like image 566
chrisc Avatar asked Jul 14 '09 20:07

chrisc


People also ask

How do I fix telnet connection timeout?

Alternatively, if telnet sits there for a few seconds without any output, it is usually safe to assume that the connection will time out, so you can stop the connection attempt by doing a Ctrl+C. When you see errors like this, it means that any of the following things are wrong: The server daemon isn't running.

How do I keep telnet session alive?

Wrap the telnet within an expect script that detects lack of input. It sends the escape sequence control-] to the telnet client to get the command prompt telnet> , and issues the command send nop (no-operation). I assume this is enough to keep the connection alive.

What is the timeout for telnet?

By default, Telnet and SSH-interactive sessions have a timeout period of 60 minutes.


2 Answers

Have you tried using netcat (nc) instead of telnet? It has more flexibility, including being able to set the timeout:

echo 'QUIT' | nc -w SECONDS YOUR_HOST PORT # e.g. echo "QUIT" | nc -w 5       localhost 21 

The -w 5 option will timeout the connection after 5 seconds.

like image 131
DaveR Avatar answered Nov 01 '22 13:11

DaveR


Try using timeout3 script is very robust and I used a lot without problems on different situations. Example to wait just 3 seconds trying to check if ssh port is open.

> echo QUIT > quit.txt > ./timeout3 -t 3 telnet HOST 22 < quit.txt  

outputs: you can grep for "Connected" or "Terminated"

timeout3 file contents:

#
#!/bin/bash # # The Bash shell script executes a command with a time-out. # Upon time-out expiration SIGTERM (15) is sent to the process. If the signal # is blocked, then the subsequent SIGKILL (9) terminates it. # # Based on the Bash documentation example. # If you find it suitable, feel free to include # anywhere: the very same logic as in the original examples/scripts, a # little more transparent implementation to my taste. # # Dmitry V Golovashkin <[email protected]>  scriptName="${0##*/}" declare -i DEFAULT_TIMEOUT=9 declare -i DEFAULT_INTERVAL=1 declare -i DEFAULT_DELAY=1 # Timeout. declare -i timeout=DEFAULT_TIMEOUT  # Interval between checks if the process is still alive. declare -i interval=DEFAULT_INTERVAL  # Delay between posting the SIGTERM signal and destroying the process by SIGKILL. declare -i delay=DEFAULT_DELAY  function printUsage() {     cat <<EOF  Synopsis     $scriptName [-t timeout] [-i interval] [-d delay] command     Execute a command with a time-out.     Upon time-out expiration SIGTERM (15) is sent to the process. If SIGTERM     signal is blocked, then the subsequent SIGKILL (9) terminates it.      -t timeout         Number of seconds to wait for command completion.         Default value: $DEFAULT_TIMEOUT seconds.      -i interval         Interval between checks if the process is still alive.         Positive integer, default value: $DEFAULT_INTERVAL seconds.      -d delay         Delay between posting the SIGTERM signal and destroying the         process by SIGKILL. Default value: $DEFAULT_DELAY seconds.  As of today, Bash does not support floating point arithmetic (sleep does), therefore all delay/time values must be integers. EOF }  # Options. while getopts ":t:i:d:" option; do       case "$option" in         t) timeout=$OPTARG ;;         i) interval=$OPTARG ;;         d) delay=$OPTARG ;;         *) printUsage; exit 1 ;;     esac done shift $((OPTIND - 1))  # $# should be at least 1 (the command to execute), however it may be strictly # greater than 1 if the command itself has options.  if (($# == 0 || interval <= 0)); then      printUsage     exit 1 fi  # kill -0 pid   Exit code indicates if a signal may be sent to $pid process. (     ((t = timeout))      while ((t > 0)); do         sleep $interval         kill -0 $$ || exit 0         ((t -= interval))     done     # Be nice, post SIGTERM first.     # The 'exit 0' below will be executed if any preceeding command fails.     kill -s SIGTERM $$ && kill -0 $$ || exit 0     sleep $delay     kill -s SIGKILL $$ ) 2> /dev/null &  exec "$@" 
#
like image 38
rbologna Avatar answered Nov 01 '22 11:11

rbologna