So i Administer multiple ftp servers that run on dynamic IP's as well as user and password settings are changed by other people constantly. What i need to do is ensure that an FTP is server is up on the IP i check. As well as the login credentials work.
Here is a simple script i wrote. However it freezes depending on whether the ftp server is up. or if the credentials work or not. what i need is some sort of timeout on each connection attempt:
heres what i got so far
Code:
#!/bin/sh
for ip in $(cat /root/Desktop/ftp.txt)
do
HOST=$ip
USER='bob'
PASS='bob'
ftp -n $HOST <<EOF
user bob bob
EOF
echo "$ip"
done
suggestions please.
1) That is a useless use of cat
2) That is a useless use of backticks as $() are modern equivalents of the backtick. If your input file ever becomes too large for a shell variable to handle, the end of it could be silently ignored.
3) If you're running this code as root, that's dangerous and unnecessary.
This is safer and more efficient:
while read LINE
do
stuff
done < filename
As for the timeout, that's simple enough:
while read ip
do
HOST=$ip
USER='bob'
PASS='bob'
timeout 300 ftp -n $HOST <<EOF && echo "$ip"
user bob bob
EOF
echo "$ip"
done < /root/Desktop/ftp.txt
The 300 is in seconds. If ftp takes longer than that, it will be killed and 'echo "$ip"' will be skipped. If FTP connects but fails to login, it will return nonzero and echo "$ip" will still be skipped.