2009-07-14 13 views
18

पर यूनिक्स टेलनेट पर टाइमआउट को कैसे कम करूं, मेरे पास एक यूनिक्स शैल स्क्रिप्ट है जो फ़ाइल में सूचीबद्ध कई होस्ट्स के ftp पोर्ट्स का परीक्षण करती है।मैं

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

सामान्य तौर पर इस स्क्रिप्ट काम करता है, लेकिन अगर मैं एक मेजबान जो कनेक्ट नहीं करता मुठभेड़, यानी टेलनेट है "करने का प्रयास ...", जिससे वह अगले मेजबान परीक्षण कर सकते हैं कि कैसे मैं इस प्रतीक्षा समय को कम कर सकते हैं?

उत्तर

28

क्या आपने टेलनेट के बजाय नेटकैट (nc) का उपयोग करने का प्रयास किया है?

echo "QUIT" | nc -w 5 host 21 

-w 5 विकल्प 5 सेकंड के बाद कनेक्शन समयबाह्य होगा: यह समाप्ति सेट करने में सक्षम होने के साथ ही अधिक लचीलापन, है।

+0

इस विचार से काम करता है .. मैं का उपयोग करने की आवश्यकता होगी इस तरह से "एनसी-जेडब्ल्यू 3 आईपी पोर्ट"। हालांकि मेजबानों के लिए जो इसे कनेक्ट नहीं करते हैं, वे बाहर निकलने वाले कोड उत्पन्न नहीं करते हैं। – chrisc

+0

अजीब। जब मैं '-w 1' कोशिश करता हूं तो यह सिर्फ 1 सेकंड से अधिक समय तक लटकता है ... – Michael

+0

@ माइकल टीसीपी कनेक्शन टाइमआउट –

1

टेलनेट प्रक्रिया को सोने और मारने की प्रक्रिया शुरू करें। लगभग:

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. 

मैंने इको QUIT से परहेज किया। पहली नौकरी के बाहर निकलने के कोड की बात आती है जब टेलनेट पाइपलाइन कोई अस्पष्टता छोड़ने के लिए।

इस कोड का परीक्षण नहीं किया गया है।

+0

निर्दिष्ट करने के लिए '-G' विकल्प भी है? क्या आप कोड को समझा सकते हैं? – ADTC

+0

टेलनेट पृष्ठभूमि में चलता है जैसे कमांड अनुक्रम जो इसे 10 सेकंड में मार देगा। 'Ex = प्रतीक्षा% 1' को टेलनेट का निकास कोड मिलता है। यदि टेलनेट टाइमआउट से पहले समाप्त होता है, तो मार% 2 से छुटकारा पाता है। लेकिन अगर टाइमआउट होता है तो मार% 2 कुछ भी नहीं करता है। –

1

अगर आपके पास nmap

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

इसका मेरा संस्करण: 'nmap -PS -p" $ पोर्ट "--होस्ट-टाइमआउट 1501ms" $ होस्ट "2>/dev/null | grep '/ tcp * open' '। '$?' सफलता के लिए 0 होगा, बंद/टाइमआउट के लिए 1। '1501ms' न्यूनतम टाइमआउट है। – dwurf

3

timeout3 स्क्रिप्ट का उपयोग कर प्रयास करें बहुत मजबूत है और मैं विभिन्न स्थितियों पर समस्याओं के बिना एक बहुत इस्तेमाल किया। उदाहरण के लिए केवल 3 सेकंड प्रतीक्षा करें कि एसएसएच पोर्ट खुला है या नहीं।

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

आउटपुट: यदि आप "कनेक्ट किया गया" या "समाप्त"

timeout3 फ़ाइल की सामग्री के लिए grep कर सकते हैं:

#
#!/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]" 
#