Operating System - HP-UX
1822504 Members
2504 Online
109642 Solutions
New Discussion юеВ

Re: FTP scripts check remote servers filesize with ftped files

 
MANISH PATEL_2
Frequent Advisor

FTP scripts check remote servers filesize with ftped files

Hi guys,

I want to ftp the file in linux from windows server. After getting file in linux server i want to check the file size with windows(source) server. If the file size are same with linux then i want to erase the windows file remotely from linux server.

Can you please help me how can i write a
scripts to check file size with Win server and erase the same. I create a automated FTP scripts but i don't know how to incorporate the same in ftp scripts.

Thanks in advance.

Manish
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: FTP scripts check remote servers filesize with ftped files

Hi Manish:

Checking the size of text files FTP'd between a Unix platform and a Windows platform will always yield a difference!

Unix uses a single character (linefeed) for delimiting newlines. Windows uses a linefeed *and* a carriage-return to denote the same. An Ascii mode FTP transfer will add or delete the carriage-return as necessary.

You need to interrogate the status of the FTP transfer. If successful, then issue a 'delete' command in your FTP session as you wish.

To determine whether or not your FTP commands are successful, you will need to parse the text messages from the session or use Perl. Checking the exit (return) status of an FTP session is meaningless as it will virtually always appear to have been successful.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: FTP scripts check remote servers filesize with ftped files

Hi Manish:

Please see also:

http://forums1.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...
Peter Godron
Honored Contributor

Re: FTP scripts check remote servers filesize with ftped files

Manish,
you may be better off using NFS to mount the Windows directory onto the linux box.
You can then cp the file and check the cp return status.
if the return status is ok, remove the file from the nfs mount.
Bill Hassell
Honored Contributor

Re: FTP scripts check remote servers filesize with ftped files

Using filesize to verify accuracy is never accurate, especially with Unix systems. Linux, HP-UX, AIX, etc, all have the concept of a sparse file where there may be undefined records. These records are 'filled in' with zeros when they are read serially, even though there were no records and no space occupied. Both files are identical and produce identical results in all ways, but the simple filesize metric will not be the same. Always use a checksum to verify file content.


Bill Hassell, sysadmin
MANISH PATEL_2
Frequent Advisor

Re: FTP scripts check remote servers filesize with ftped files

Hi all

Thanks for your valuable info. Can you please somebody help me how to add check status and delete command in the following shell scripts.

following scripts ftp file from windows to linux and it residing in linux server.

#!/bin/bash
ftp -n -i << EOF
user
dir
mget *
bye
EOF
exit

Thankx
Manish
A. Clay Stephenson
Acclaimed Contributor

Re: FTP scripts check remote servers filesize with ftped files

Trying to do this within a shell script is very Mickey Mouse because it's difficult to parse ftp's stderr for error messages and you probably won't test everything you need anyway. Use Perl's Net::FTP and this is simple and you get error checking for free.

The attached Perl script returns 0 if ok and non-zero if bad and that's all you need to know to make this work. I will invoke the script one time to get the files in the remote directory and then invoke again for each to get and delete each file. It only deletes the remote file, if the get was ok.

#!/usr/bin/sh

typeset remhost="bugs"
typeset user="cstephen"
typeset passwd="secret"
typeset remdir="/tmp/acs" # remote directory

typeset FNAME=""
typeset -i STAT=0

ftpget.pl -h ${remhost} -l ${user} \
-p ${passwd} -d ${remdir} -L "" |\
while read FNAME
do
echo "Getting ${FNAME}"
ftpget.pl -h ${remhost} -l ${user} \
-p ${passwd} -d ${remdir} -B -r "${FNAME}"
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "Get of \"${FNAME}\" failed; status ${STAT}." >&2
break
fi
done
exit ${STAT}

The -B option asserts binary transfers; if you want ASCII use -A. It's important to quote the filename because you might have whitespace embedded in the pathnames --- especially on one of those evil Windows boxes. If you don't want to pass the password then put the username/password tuple in the .netrc file. Invoke as "ftpget.pl -u" for full usage.
If it ain't broke, I can fix that.
MANISH PATEL_2
Frequent Advisor

Re: FTP scripts check remote servers filesize with ftped files

Hi ACS

Thanks for providing me perl scripts. I tried this scripts in my Linux server but it says "ftpget.pl" command not found. I think i have to installed perl in linux server.

Do you have any idea which commands work (from linux to windows)remotely to get a filenames from windows directory.

Thankx
Manish
A. Clay Stephenson
Acclaimed Contributor

Re: FTP scripts check remote servers filesize with ftped files

The attachment IS ftpget.pl; install it in some directory such as /usr/local/bin and set the mode to 755. The script that I posted is a shell script which calls the attached ftpget.pl.
If it ain't broke, I can fix that.