2009-07-14 34 views
18

Tôi có tập lệnh shell unix kiểm tra các cổng ftp của nhiều máy chủ được liệt kê trong một tệp.làm cách nào để giảm thời gian chờ trên telnet unix trên kết nối

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

Nói chung, tập lệnh này hoạt động, tuy nhiên nếu tôi gặp máy chủ không kết nối, telnet là "Đang thử ...", làm cách nào để có thể giảm thời gian chờ này để kiểm tra máy chủ tiếp theo?

Trả lời

28

Bạn đã thử sử dụng netcat (nc) thay vì telnet? Nó có tính linh hoạt hơn, bao gồm cả việc có thể để thiết lập thời gian chờ:

echo "QUIT" | nc -w 5 host 21 

Tùy chọn -w 5 sẽ timeout kết nối sau 5 giây.

+0

ý tưởng này làm việc .. tôi sẽ cần phải sử dụng theo cách này "nc-z -w 3 ip port". Tuy nhiên đối với các máy chủ không kết nối nó không tạo ra một mã thoát. – chrisc

+0

lạ. khi tôi cố gắng '-w 1' nó chỉ ngồi ở đó treo nhiều hơn 1 giây ... – Michael

+0

@Michael Cũng có tùy chọn' -G' để chỉ định thời gian chờ kết nối TCP –

1

Sử dụng bắt đầu quy trình để ngủ và giết quá trình telnet. Khoảng:

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. 

Tôi tránh tiếng vang QUIT | telnet đường ống để lại không mơ hồ khi nói đến mã thoát của công việc đầu tiên.

Mã này chưa được kiểm tra.

+0

Bạn có thể giải thích mã không? – ADTC

+0

telnet chạy ẩn cũng như một chuỗi lệnh sẽ giết nó trong 10 giây. 'Ex = wait% 1' lấy mã thoát của telnet. Nếu telnet kết thúc trước thời gian chờ, kill% 2 sẽ loại bỏ nó. Nhưng nếu thời gian chờ xảy ra, việc giết% 2 không làm gì cả. –

1

nếu bạn có nmap

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

Phiên bản của tôi về điều này: 'nmap -PS -p" $ port "--host-timeout 1501ms" $ host "2>/dev/null | grep '/ tcp * mở' '. '$?' sẽ là 0 cho thành công, 1 cho đóng/timeout. '1501ms' là thời gian chờ tối thiểu. – dwurf

3

Hãy thử sử dụng kịch bản timeout3 là rất mạnh mẽ và tôi đã sử dụng rất nhiều không có vấn đề về tình huống khác nhau. Ví dụ để chờ đợi chỉ 3 giây cố gắng kiểm tra xem cổng ssh có đang mở hay không.

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

kết quả đầu ra: bạn có thể grep cho "Connected" hoặc "Kết thúc"

tập tin timeout3 nội dung:

#
#!/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]" 
#
Các vấn đề liên quan