- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to capture return codes from FTP
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 06:07 PM
тАО03-29-2004 06:07 PM
I have the following script to perform automated FTP ; I need to check the return code after a file is transmitted. If I do ...
ftp>!echo $? it always gives me "0". Which means this is not an error in my current shell but the ftp shell ! How do I capture the ftp error codes ?.
Please note that this node is out side my firewall and I only have ftp port open for this ! (i.e. I can not do an remsh ls -l to check in that case i would have gone for rcp ! )
*******************
#FTP Script
if [ $# -lt 4 ]
then
echo Usage :$0 host_name user_name passwd filename
fi
echo $* |read host_name user passwd file
#ping -c1 $host_name
#if [ $? - ne 0 ]
#then
# host is not reachable
# exit 0
#fi
if [ ! -f $file ]
then
echo Unable to access file $file
fi
ftp -n << END
open $host_name
user $user $passwd
prompt off
bin
put $file
XXXXXXXXX <<<=========== Here I need to capture whether a file is transmitted properly or no !
quit
END
**********************
Regds,
Kap
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 06:25 PM
тАО03-29-2004 06:25 PM
Re: How to capture return codes from FTP
Well, you can if you use something else. For example you could use "expect" to do your "ftp" for you and "screen scrape" the output to see if there is an error. This though, is horrible.
Alternatively, use "rcp" which does have an exit code which you can test.
If you have to use "ftp", I think you'll have to do something like check the size of the file on the server and then the size of the file you have recieved.
This kind of thing might help though
#!/usr/bin/perl
use Net::FTP;
$ftp=Net::FTP->new("ftp server");
$ftp->login("user","password");
$ftp->get("file") or die "Oh dear file didn't come over properly\n";
$ftp->quit;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 06:28 PM
тАО03-29-2004 06:28 PM
Re: How to capture return codes from FTP
What you can do is grep the return codes of the FTP commands.
Use the command ftp -nv << END ....
When grepping on the output you can conclud if the file went over without any errors.
Check also next thread:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=439471
HTH,
Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 06:41 PM
тАО03-29-2004 06:41 PM
Re: How to capture return codes from FTP
Also let me know If the following perl version is OK "5.005_03" !
Whats the syntax of Mr Clay's script ?. I would like to be a user than trying to act as a techy when I play with things on which i am not confident !
Mr A.Clay , Can u please let me know the syntax or ways to invoke this script I owuld like to keep the existing Syntax as my original shell script !
i.e. Command host_name user_name passwd file_name
Please help me out guys ..
Kaps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 07:15 PM
тАО03-29-2004 07:15 PM
Re: How to capture return codes from FTP
Using expect, I suppose you can deal with this return accordingly.
As for perl, look at cpam, maybe you'll find a module suiting your need.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 07:31 PM
тАО03-29-2004 07:31 PM
Re: How to capture return codes from FTP
I'll see if I have an example for you to use (code at home unfortunately, so it will be later). Its quite easy to use, and infinitely more stable than Expect (tried both methods - Expect always seemed to leave hanging processes everywhere). The Perl version you mentioned will work OK.
Col.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 08:42 PM
тАО03-29-2004 08:42 PM
Re: How to capture return codes from FTP
use Net::FTP;
$ftp=Net::FTP->new("$hostname");
$ftp->login("$user","$password");
$ftp->get("$file") or die "Oh dear file didn't come over properly\n";
$ftp->quit;
exit 0;
Save this as "myftp.pl" and then run as
myftp.pl hostname user password file
It will have an non 0 exit status on failure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 08:59 PM
тАО03-29-2004 08:59 PM
Re: How to capture return codes from FTP
Kaps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 10:13 PM
тАО03-29-2004 10:13 PM
Re: How to capture return codes from FTP
#!/usr/bin/perl
($hostname,$user,$password,$file)=@ARGV;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 10:43 PM
тАО03-29-2004 10:43 PM
Re: How to capture return codes from FTP
Thanks it is resolved at last leme ; This case is closed !
Kaps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2004 10:45 PM
тАО03-29-2004 10:45 PM
Re: How to capture return codes from FTP
Hey how can I ?
Kaps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2004 02:14 AM
тАО03-30-2004 02:14 AM
SolutionThe other thing to note that it is not necessary to supply the passwd because ythat can be read from the user's .netrc file. Man netrc for details.
I would use the Perl script that is attached. It's more advanced than most of the example scripts I have posted and is nearly the version I use in real life.
You don't need to ping beforehand as a non-zero result will be returned to the shell for whatever reason of failure.
I would use it something like this:
ftpput.pl -h remhost -l remuser -p topsecret -A -d /mydir/myfiles -t 3 myfile1 myfile2
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "All ok"
else
echo "Bad. Status = ${STAT}"
fi
This would connect to host remhost (-h remhost) as user remuser (-l remuser) with password topsecret (-p topsecret) in ASCII mode (-A) and cd to /mydir/myfiles (-d /mydir/myfiles). Each transmission would try up to 3 times (-t 3) before giving up. The last arguments are the files to send.
Again, there is no need to supply the passwd because that can be supplied from .netrc.
Invoke as ftpput.pl -u for full usage. Now all your shell scripting is trivially easy.
Here is ftpput.pl:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2004 02:15 AM
тАО03-30-2004 02:15 AM
Re: How to capture return codes from FTP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2004 07:07 AM
тАО03-30-2004 07:07 AM
Re: How to capture return codes from FTP
Secure copy and secure shell both return the exit code of whatever it is you executed on the remote host:
ssh $REMHOST "ls -l /etc/hots"
echo "$?"
1
ssh $REMOST "ls -l /etc/hosts"
echo "$?"
0
Further its a lot easier to script, and its very secure: scp $FILE $REMHOST:/$DIR/$FILE.
You can guarantee the intact delivery of your file with checksums:
SUM1=`sum -r $DIR/$FILE|awk '{ print $1 }'`
SUM2=`ssh $DESTHOST "sum -r $DIR/$FILE|awk '{ print $1}'"`
if test "$SUM1" -ne "$SUM2"
then
echo "There is a problem with $DIR/$FILE"
fi
Secure shell is not hard to install, and the configuration is easy. Try it, you'll like it.
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-31-2004 03:37 AM
тАО03-31-2004 03:37 AM
Re: How to capture return codes from FTP
You CAN use $? to get information about if FTP itself failed. For example, if your /var filesystem is full, ftp will fail (it needs /var/temp to store temporary files in order to work properly). If you don't check the $?, you may not detect this failure because FTP may not return an error to stdout.