2009-07-14 8 views
18

Ich habe ein UNIX-Shell-Skript, die FTP-Ports von mehreren Hosts in einer Datei getestet.Wie reduziere ich Timeout auf Unix Telnet auf Verbindung

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

Im Allgemeinen diese Skripte funktioniert, aber wenn ich einen Host auftreten, die keine Verbindung, das heißt Telnet ist „Der Versuch, ...“, wie kann ich diese Wartezeit zu reduzieren, so dass es das nächste Host testen kann?

Antwort

28

Haben Sie versucht, netcat (nc) anstelle von Telnet zu verwenden? Es hat mehr Flexibilität, einschließlich der Lage, den Timeout zu setzen:

echo "QUIT" | nc -w 5 host 21 

Die -w 5 Option, um die Verbindung nach 5 Sekunden Timeout.

+0

diese Idee funktioniert .. muss ich verwenden auf diese Weise "nc -z -w 3 ip port". Für Hosts, die keine Verbindung herstellen, wird jedoch kein Beendigungscode generiert. – chrisc

+0

seltsam. Wenn ich versuche "-w 1", dann sitzt es einfach länger als 1 Sekunde hängen ... – Michael

+0

@Michael Es gibt auch '-G' Option zur Angabe der TCP-Verbindung Timeout –

1

Verwenden Sie einen Prozess zum Schlafen und zum Beenden des Telnet-Prozesses. Ungefähr:

echo QUIT >quit.txt 
telnet $i 21 < quit.txt & 
sleep 10 && kill -9 %1 & 
ex=wait %1 
kill %2 
# Now check $ex for exit status of telnet. Note: 127 inidicates success as the 
# telnet process completed before we got to the wait. 

Ich vermied das Echo QUIT | Telnet-Pipeline, um keine Zweideutigkeiten zu hinterlassen, wenn es um den Exit-Code des ersten Jobs geht.

Dieser Code wurde nicht getestet.

+0

Können Sie den Code bitte erklären? – ADTC

+0

Telnet läuft im Hintergrund, ebenso wie eine Befehlssequenz, die es in 10 Sekunden beendet. Das 'ex = wait% 1' erhält den Exit-Code des Telnets. Wenn das Telnet vor dem Timeout beendet wird, wird es vom Kill% 2 gelöscht. Aber wenn die Zeitüberschreitung auftritt, tut der Kill% 2 nichts. –

1

wenn Sie nmap

nmap -iL hostfile -p21 | awk '/Interesting/{ip=$NF}/ftp/&&/open/{print "ftp port opened for: "ip}' 
+0

Meine Version davon: 'nmap -PS -p" $ port "--host-timeout 1501ms" $ host "2>/dev/null | grep '/ tcp * offen' '. '$?' ist 0 für Erfolg, 1 für geschlossen/Timeout. '1501ms' ist die minimale Zeitüberschreitung. – dwurf

3

Versuchen haben timeout3 Skript ist sehr robust und ich eine Menge ohne Probleme auf verschiedenen Situationen. Beispiel, um nur 3 Sekunden zu warten, um zu überprüfen, ob der ssh-Port offen ist.

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

Ausgänge: Sie können für "Connected" grep oder "Abgebrochen"

timeout3 Dateiinhalt:

#
#!/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 "[email protected]" 
#
Verwandte Themen