1826452 Members
3918 Online
109692 Solutions
New Discussion

FTP script

 
Pat Peter
Occasional Advisor

FTP script

Hi,

I need to write a FTP script to connect to various machines. The machine names should be obtained from a separate file. The file would look like

MachineIP AppVersion
......... .........


Now the ftp script should connect to all these machines and get file app_mmddyyyy_* in case the application is in 5.5 version. If the application is in 5.0 version the FTP script should obtain the file app_mmddyy_*.

Also, the FTP script should send error messages to a log file in case it is unable to connect to any of the machine or if the file which it is trying to obtain is not available.

Thanks,
Pat

7 REPLIES 7
Bill Hassell
Honored Contributor

Re: FTP script

This is fairly easy except for some missing information:

1. Where are these files (you must specify the full path to the files)

2. Where are these files going to be stored (as before, you need a full pathname for the destination)

3. How do you determine the version of the application?

Remember, FTP is not a shell and it does not login or have access to special commands. Run the ftp command by hand to see what steps you need (login, password, cd to remote directory, lcd to local directory, etc). To see what commands you have available in FTP, type the ? or help command.


Bill Hassell, sysadmin
Steven E. Protter
Exalted Contributor

Re: FTP script

Additional help.

How to actually automate:

USER=username
PASS=password
COLLECT=" /home/username/audit"
FILE1=" au_audit.op"

cd /home/ye103910/logftp01

ftp -nv 10.215.15.1 </tmp/get_logftp01.$$
user ${USER} ${PASS}
cd ${COLLECT}
get ${FILE1}
quit
FTPEOF

You can actually use this as a function and feed in the information from your data file.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
harry d brown jr
Honored Contributor

Re: FTP script


See Clay's answer (the attachment) for using perl to ftp:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=550649

live free or die
harry d brown jr
Live Free or Die
Pat Peter
Occasional Advisor

Re: FTP script

Hi,

Please find attached the input file to my FTP script. The FTP script should read the IP of the machines which it is suppose to connect from the input file. The input file will be located in a directory called /home/config/.

The FTP script should get the file named app_mmddyyyy_* in case the machine has a version 5.0 in the input file.

The FTP script should get the file named app_mmddyy_* in case the machine has a version 5.5 in the input file.

Also, the FTP script should send error messages to a log file in case it is unable to connect to any of the machines mentioned in the input file or if the file which it is trying to obtain is not available.

Hope it is little clear now.

Thanks,
Pat

A. Clay Stephenson
Acclaimed Contributor

Re: FTP script

First of all, ditch the header line and the blank lines from your input file (or at least adopt the universal convention of preceding them with '#' to signal a comment. You STILL haven't bothered to identify the directory in which these files are stored. I will be amazed if anyone bothers to write this for you when you haven't even made the slightest attempt at it.

Reading your input sans the header/blank line is easy:

INFILE=/xxx/myfile

cat ${INFILE} | while read IP VERS
do
echo "IP: ${IP} Version: ${VERS}"
done

Now I will give you the tricky piece, ftpget.pl:

ftpget.pl -h ${IP} -l cstephen -p "topsecret" -d ${REMOTE_DIR} -L 'app_mmddyy_*' | while read FNAME
do
ftpget.pl -h ${IP} -l cstephen -p "topsecret" ${FNAME}
STAT=${?}
done

These are the bare bones of the idea. I'll leave the rest as an exercise. I strongly suggest that you use .netrc files to store the passwd's so that no -p argument is necessary. It's also probably much smarter to use hostnames rather than IP addresses but ftpget.pl will handle either.

Invoke as ftpget.pl -u for full usage. You can also use the -z option to check for remote host connectivity.
If it ain't broke, I can fix that.
Pat Peter
Occasional Advisor

Re: FTP script

Clay - Thanks for your script. I am sorry that I forgot to mention the directory in which I need to store the files.

The files are actually to be stored in the directory /home/logs/.

Can you please suggest me If I could use Anonymous FTP instead of the normal FTP. Anonymous FTP is enabled in all the machines to which I am going to connect.

Also, the requirement is changed little in the sense that the input file will have only the machine names of the machine to which I need to connect. There will not be any version column.

I have to get the file app_mmddyyyy_* from /home/bin/logs directory of the remote machines to which I am connecting.

The script should also write errors to a file /home/config/errors in case if it is not able to connect to any of the machine listed in the input file or if the file is missing in the remote machine.

I am very new to FTP. Please help.

Thanks
Pat

A. Clay Stephenson
Acclaimed Contributor

Re: FTP script

The ftpget.pl script will work just fine with anonymous ftp; you simply simply -l anonymous as the user and you shouldn't need a -p passwd argument. You specify the directory with the -d arg. From this point on, you really don't need to know any Perl at all. It's all a matter of shell scripting that invokes ftpget.pl. I have no intention of teaching you shell scripting -- that should be part of your job. Invoke as "ftpget.pl -u" for a full usage display.

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