tags:

views:

964

answers:

3

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 ?

+4  A: 

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

echo "QUIT" | nc -w 5 host 21

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

Dave Rigby
this idea works.. i will need to use it in this way "nc -z -w 3 ip port". However for hosts which do not connect it does not generate an exit code.
chrisc
+1  A: 

Use start a process to sleep and kill the telnet process. Roughly:

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.

I avoided the echo QUIT | telnet pipeline to leave no ambiguity when it comes to the exit code of the first job.

This code has not been tested.

George Phillips
A: 

if you have nmap

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