Operating System - Linux
1825987 Members
3175 Online
109690 Solutions
New Discussion

Re: Script to test FTP functionality

 
SOLVED
Go to solution
Coolmar
Esteemed Contributor

Script to test FTP functionality

Hi,

We have around 400 servers here and I want to write a script to see where FTP is available and where it is not, on all servers. It is open on all in the inetd, but we are not sure about the firewall rules...and not having anything to do with the firewall, I need to find another course of action. So is there a way to test ftp functionality on all servers? Like with SSH, I just ssh to each server and run hostname. With ftp, I would have to telnet to port 21, but that is interactive and so is ftp'ing to a server. There is also where you ftp to a server and it doesn't open a connection but just hangs there until a CTRL-C is done...so I would need a timeout.

Thanks,
16 REPLIES 16
spex
Honored Contributor

Re: Script to test FTP functionality

Sally,

Install nmap and scan port 21.

PCS
Coolmar
Esteemed Contributor

Re: Script to test FTP functionality

Can nmap be run from one server to scan 400 other servers? I don't necessarily want to test if the port is "open", but if it is "accessible" by the other servers at the firewall.
Thanks,
spex
Honored Contributor

Re: Script to test FTP functionality

Sally,

I'm not sure that there's an easy way to accomplish this. What follows is one possible solution, but it requires remsh permission on each server (set in /etc/hosts.equiv).

Say s_list.text contains a list of the 400 server hostnames, one per line. If user@host1 can is authorized to remsh on each of these servers, then a script like the following might work:

#!/usr/bin/sh
for i in $(cat s_list.text)
do
echo "Result for host $i:" >> results.text
remsh $i ftp \& >> results.text 2>&1
done
exit 0

After the script is done, review results.text. Check out every line following "Result for host". If you see something like:

ftp: : Unknown host

Then was unreachable from that host. However, if you see something like:

Name (:): Login incorrect.
Login failed.

You know that was reachable from that host. You could also write a little script to parse results.text. Just remember that there may be more than one line of output separating lines containing "Result for host".

Good luck!

PCS



Michael Steele_2
Honored Contributor

Re: Script to test FTP functionality

Here a basic example:

ftp -in server << EOF
user username password
get /something
put /something_else
EOF

With EOF you don't worry about a timeout or control c.

Support Fatherhood - Stop Family Law
James R. Ferguson
Acclaimed Contributor
Solution

Re: Script to test FTP functionality

Hi Sally:

Here's a potentially workable approach. The script needs embellishment. You will need to add a driver loop to query your hosts and to collect the results. A simple file of hostnames (or IP addresses) will do as input. A simple log of the output will suffice.

# cat .query21
#!/usr/bin/sh
typeset HOST=${1}
telnet ${HOST} 21 |&
sleep 3
read -p REPLY
read -p REPLY
print -p quit
ACTION=`echo ${REPLY}|cut -d" " -f1`
if [ "${ACTION}" != "Connected" ]; then
echo "${HOST} FAILED to connect"
else
echo "${HOST} reponded_ok!"
fi
exit 0

...Run as:

# ./query21 somehost

...The exact message ("Connected ...") returned, may vary and that may need some tweaking too.

You will see output like:

telnet: connect: A remote host refused an attempted connect operation.
/tmp/telnet[7]: print: 0403-039 No query process for pipe.
xxxxxxxx to connect

...for a failure; and:

xxxxxxxx reponded_ok!

...for success.

This offers you a direction, at least.

Regards!

...JRF...
Ninad_1
Honored Contributor

Re: Script to test FTP functionality

Here you go - tailor made for you.
Assuming you have the list of hostnames in a file hosts.dat

The script is attached

Regards,
Ninad
Ninad_1
Honored Contributor

Re: Script to test FTP functionality

Sorry forgot to explain how it works.

The trick here is to give junk username and password so that ftp fails [ for server where it is able to connect thru ftp ]
If you cannot connect to a server it is also displayed.

Regards,
Ninad
A. Clay Stephenson
Acclaimed Contributor

Re: Script to test FTP functionality

Okay, here's a real solution:

#!/usr/bin/sh

typeset HOST=remhost
typeset USER=mickey
typeset PASSWD=topsecret
typeset -i TIMEOUT=20
typeset -i STAT=0

ftpget.pl -h ${HOST} -l ${USER} -p ${PASSWD} -s ${TIMEOUT} -z
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "${HOST} Okay"
else
echo "FTP to ${HOST} failed; status ${STAT}" >&2
fi
exit ${STAT}

This will attempt to establish an ftp connect to the remote. It is not necessary to supply a password on the command line as that can be read from an optional .netrc file.

Invoke as ftpget.pl -u for full usage; this will also transfer your files with full error checking and optional repeat requests if a transfer fails.

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Script to test FTP functionality

In case, you ultimately want to do puts to the remote host, here's ftpput.pl. The commands are the same; invoke as ftpput.pl -u for usage.

If it ain't broke, I can fix that.
Coolmar
Esteemed Contributor

Re: Script to test FTP functionality

Thanks Clay...the problem is that I don't want to login, I just want to connect. If there is a connection, then that's all I need.
spex
Honored Contributor

Re: Script to test FTP functionality

Sally,

nmap was made for this sort of thing.

Get the depot here:

http://hpux.connect.org.uk/hppd/hpux/Networking/Admin/nmap-3.93/

Then:

nmap -p 21

PCS
James R. Ferguson
Acclaimed Contributor

Re: Script to test FTP functionality

Hi (again) Sally:

...and the script I suggested does *not* attempt to login to a server. It merely queries the connection result.

I'm sure that with 400 servers the accounts and passwords are not all the same :-))

Regards!

...JRF...
Coolmar
Esteemed Contributor

Re: Script to test FTP functionality

Thanks James...I will give yours a shot.
S
Ninad_1
Honored Contributor

Re: Script to test FTP functionality

Sally,

Did you try the script I have posted. It uses junk user and password in the script so you need not mention and reveal actual passwords also script automatically checks conenctions for each host and you do not need to enter anything.

Regards,
Ninad
A. Clay Stephenson
Acclaimed Contributor

Re: Script to test FTP functionality

Oh, in that case, ftpget.pl (or ftpput.pl) still works because the error returned is different.

ftpput.pl -h goodhost -s 15 -z 2>/dev/null
STAT=${?}
echo "${STAT}" # this will be 5; meaning I could connect but the login failed


ftpput.pl -h badhost -s 15 -z 2>/dev/null
STAT=${?}
echo "${STAT}" # this will not be 5



If it ain't broke, I can fix that.
Coolmar
Esteemed Contributor

Re: Script to test FTP functionality

Thanks everyone!