Unix Technical Forum

Script that checks for existence of file on remote server.

This is a discussion on Script that checks for existence of file on remote server. within the AIX Operating System forums, part of the Unix Operating Systems category; --> Hi - I would like to know if there is a way in shell scripting to check the existence ...


Go Back   Unix Technical Forum > Unix Operating Systems > AIX Operating System

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-05-2008, 05:29 AM
vegas_girlie
 
Posts: n/a
Default Script that checks for existence of file on remote server.

Hi -
I would like to know if there is a way in shell scripting to check the
existence of a file after FTP? Basically if the file does not exist in
a certain directory, the shell script should fail.

thanks,
val

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-05-2008, 05:29 AM
Laurenz Albe
 
Posts: n/a
Default Re: Script that checks for existence of file on remote server.

vegas_girlie <minnie_valerie@hotmail.com> wrote:
> I would like to know if there is a way in shell scripting to check the
> existence of a file after FTP? Basically if the file does not exist in
> a certain directory, the shell script should fail.


if [ ! -f somefile ]; then
exit 1
fi

to check for the existence of a certain file.

Yours,
Laurenz Albe
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-05-2008, 05:30 AM
steven_nospam at Yahoo! Canada
 
Posts: n/a
Default Re: Script that checks for existence of file on remote server.

If I read the message correctly, I suspect Minnie wanted a way to check
that the file was FTP'd over onto the REMOTE system, not to check
locally...I may be wrong.

If you are administrator of both systems and can do remote shell
commands, you can simply set up so that you can issue "rsh" commands.
Check into the rsh command and .rhosts or hosts.equiv files.

If you must use FTP to do this, you can always capture your FTP
commands and output to a file, then scan that output file for the
expected returned FTP messages. For example, you may see something like
the following:
-----------------------------------------------------------------------------------
ftp> put sample.dat
200 PORT command successful.
150 Opening data connection for sample.dat.
226 Transfer complete.
5098 bytes sent in 0.01821 seconds (273.4 Kbytes/s)
local: sample.dat remote: sample.dat
ftp> quit
221 Goodbye.
-------------------------------------------------------------------------------------
You could use "grep" to scan for "^226 Transfer complete." or look for
the "bytes sent" line and grab the number of bytes, then compare it to
the one you have locally. I believe they are usually the same size...
Just some ideas for you to think about ;-)

Steve

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-05-2008, 05:30 AM
vegas_girlie
 
Posts: n/a
Default Re: Script that checks for existence of file on remote server.

Hi Steven -
You are correct. I need to check the file that was FTP'd over to make
sure they landed ok.

I was able to create an output file that captured the file name that
was FTP'd. However, my other problem is how to write error codes if
the file name does not exist in the output file. Will TEST command
work? Doesnt Test just check for existence of files or directories and
not words?

Thanks.

steven_nospam at Yahoo! Canada wrote:
> If I read the message correctly, I suspect Minnie wanted a way to

check
> that the file was FTP'd over onto the REMOTE system, not to check
> locally...I may be wrong.
>
> If you are administrator of both systems and can do remote shell
> commands, you can simply set up so that you can issue "rsh" commands.
> Check into the rsh command and .rhosts or hosts.equiv files.
>
> If you must use FTP to do this, you can always capture your FTP
> commands and output to a file, then scan that output file for the
> expected returned FTP messages. For example, you may see something

like
> the following:
>

-----------------------------------------------------------------------------------
> ftp> put sample.dat
> 200 PORT command successful.
> 150 Opening data connection for sample.dat.
> 226 Transfer complete.
> 5098 bytes sent in 0.01821 seconds (273.4 Kbytes/s)
> local: sample.dat remote: sample.dat
> ftp> quit
> 221 Goodbye.
>

-------------------------------------------------------------------------------------
> You could use "grep" to scan for "^226 Transfer complete." or look

for
> the "bytes sent" line and grab the number of bytes, then compare it

to
> the one you have locally. I believe they are usually the same size...
> Just some ideas for you to think about ;-)
>
> Steve


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-05-2008, 05:30 AM
Douglas R. Probst
 
Posts: n/a
Default Re: Script that checks for existence of file on remote server.


"vegas_girlie" <minnie_valerie@hotmail.com> wrote in message
news:1104947445.075923.122430@c13g2000cwb.googlegr oups.com...
> Hi Steven -
> You are correct. I need to check the file that was FTP'd over to make
> sure they landed ok.
>
> I was able to create an output file that captured the file name that
> was FTP'd. However, my other problem is how to write error codes if
> the file name does not exist in the output file. Will TEST command
> work? Doesnt Test just check for existence of files or directories and
> not words?
>
> Thanks.
>
> steven_nospam at Yahoo! Canada wrote:
> > If I read the message correctly, I suspect Minnie wanted a way to

> check
> > that the file was FTP'd over onto the REMOTE system, not to check
> > locally...I may be wrong.
> >
> > If you are administrator of both systems and can do remote shell
> > commands, you can simply set up so that you can issue "rsh" commands.
> > Check into the rsh command and .rhosts or hosts.equiv files.
> >
> > If you must use FTP to do this, you can always capture your FTP
> > commands and output to a file, then scan that output file for the
> > expected returned FTP messages. For example, you may see something

> like
> > the following:
> >

> --------------------------------------------------------------------------

---------
> > ftp> put sample.dat
> > 200 PORT command successful.
> > 150 Opening data connection for sample.dat.
> > 226 Transfer complete.
> > 5098 bytes sent in 0.01821 seconds (273.4 Kbytes/s)
> > local: sample.dat remote: sample.dat
> > ftp> quit
> > 221 Goodbye.
> >

> --------------------------------------------------------------------------

-----------
> > You could use "grep" to scan for "^226 Transfer complete." or look

> for
> > the "bytes sent" line and grab the number of bytes, then compare it

> to
> > the one you have locally. I believe they are usually the same size...
> > Just some ideas for you to think about ;-)
> >
> > Steve

>



Might be way off base here, but ssh is secure, free and much easier to
script and check results.
In today's world, ftp, telnet and rsh should be banned and removed from all
servers to help protect our personal security.
Then again, just my 2 cents worth.
Have a good day
Doug


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-05-2008, 05:30 AM
steven_nospam at Yahoo! Canada
 
Posts: n/a
Default Re: Script that checks for existence of file on remote server.

If you are going to use FTP, here's a method that may work:

ftp -i ${DESTHOST} << END >> ${TMPOUT}
verbose
prompt
lcd ${SRCDIR}
cd ${DESTDIR}
pwd
put ${SRCFILE} ${DESTFILE}
quit
END

BYTES_SENT=$(cat ${TMPOUT} | grep "bytes sent" | cut -f1 -d ' ')
BYTES_ORIG=$(ls -l $(SRCFILE} | awk '{ print $5 }')

if test "${BYTES_SENT}" = "${BYTES_ORIG}"
then
echo "File is same size on remote host - successful transfer"
else
echo "File sizes differ:"
echo "Original:\t${BYTES_ORIG}"
echo "Remote:\t${BYTES_SENT}"
fi

Of course, I'm not sure if the size on the remote is ALWAYS going to be
the same as on the local system. That is something you'd have to
evaluate.

Also , you may want to consider secure shell (ssh) as many people are
disabling the FTP and other services to prevent unauthorized logins. If
these two systems are internal, then it is not as critical but may
still be prone to hacking....

Hope this helps...

Steve

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-05-2008, 05:31 AM
Laurenz Albe
 
Posts: n/a
Default Re: Script that checks for existence of file on remote server.

steven_nospam at Yahoo! Canada <steven_nospam@yahoo.ca> wrote:
> If you are going to use FTP, here's a method that may work:
>
> ftp -i ${DESTHOST} << END >> ${TMPOUT}
>
> BYTES_SENT=$(cat ${TMPOUT} | grep "bytes sent" | cut -f1 -d ' ')
> BYTES_ORIG=$(ls -l $(SRCFILE} | awk '{ print $5 }')
>
> if test "${BYTES_SENT}" = "${BYTES_ORIG}"
> then
> echo "File is same size on remote host - successful transfer"
> else
> echo "File sizes differ:"
> fi
>
> Of course, I'm not sure if the size on the remote is ALWAYS going to be
> the same as on the local system. That is something you'd have to
> evaluate.


One way to make sure that the size on the destination system will be the
same is to use binary mode file transfer.

If you have to transfer ASCII files, and the destination system does not
run UNIX, you will need ascii mode file transfer.

You can force binary mode by issuing 'binary' before the 'put' command
and ascii mode by issuing 'ascii'.

If you use ascii file transfer, the size of the file on the receiving end
could be different; e.g. if you transfer from UNIX to Windows, an
ASCII 0x0d character (carriage return) will be inserted before every
line feed (ASCII 0x0a), thereby enlarging the destination file.

In that case I see two possibilities:
a) forget about the file size and check the output file for the message
that indicates successful transfer.
b) convert the file to the correct ascii format on the destination
platform first and then transfer it in binary mode. For UNIX to Windows
transformation, you could use the 'doswrite' command that is shipped
with AIX.

Yours,
Laurenz Albe
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-05-2008, 05:32 AM
vegas_girlie
 
Posts: n/a
Default Re: Script that checks for existence of file on remote server.

The verbose command worked. Thanks! however, i would now like to
search for the number of occurrences of '226 Transfer complete' and if
it is less than 46 occurrences fail the jobs. I have code that is
something like this but is not working. Any advice would be greatly
appreciated.

Count=$(grep '226 Transfer complete.' file.txt)
if (( $Count < 46))
then
echo 'missing ftp files'
exit 1
fi

can

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-05-2008, 05:32 AM
Laurenz Albe
 
Posts: n/a
Default Re: Script that checks for existence of file on remote server.

vegas_girlie <minnie_valerie@hotmail.com> wrote:
> however, i would now like to
> search for the number of occurrences of '226 Transfer complete' and if
> it is less than 46 occurrences fail the jobs.
>
> Count=$(grep '226 Transfer complete.' file.txt)


Try something like

Count=$(grep '226 Transfer complete.' file.txt|wc -l)

Yours,
Laurenz Albe
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:55 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com