Check ftp return codes from Unix script

I am currently creating a nightly job that calls a Unix script, which in turn creates and transfers a file using ftp. I would like to check all possible return codes. The manual page for ftpdoes not contain return codes. Does anyone know where to find the list? Anyone with experience? We have other scripts that grep for specific return lines in the log, and they send an email in error. However, they often skip unexpected codes. Then I put the reason in the log and email.

+3
source share
9 answers

The command ftpreturns nothing but zero in most of the implementations I have implemented.

It is much better to handle three-digit codes in a log - and if you are sending a binary file, you can verify that the bytes sent were correct.

Three-digit codes are called β€œserial codes,” and the list can be found here.

+7
source

I wrote a script to transfer only one file at a time and in this script use grepto verify the message 226 Transfer complete. If he finds it, grepreturns 0.

ftp -niv < "$2"_ftp.tmp | grep "^226 "
+5
source

ncftp. ncftpget ncftpput / , . . "" .

+4

, ftp ftp, - .

, :

# ...
ftp -i -n $HOST 2>&1 1> $FTPLOG << EOF
quote USER $USER
quote PASS $PASSWD
cd $RFOLDER
binary
put $FOLDER/$FILE.sql.Z $FILE.sql.Z
bye
EOF

# Check the ftp util exit code (0 is ok, every else means an error occurred!)
EXITFTP=$?
if test $EXITFTP -ne 0; then echo "$D ERROR FTP" >> $LOG; exit 3; fi
if (grep "^Not connected." $FTPLOG); then echo "$D ERROR FTP CONNECT" >> $LOG; fi 
if (grep "No such file" $FTPLOG); then echo "$D ERROR FTP NO SUCH FILE" >> $LOG; fi 
if (grep "access denied" $FTPLOG ); then echo "$D ERROR FTP ACCESS DENIED" >> $LOG; fi
if (grep "^Please login" $FTPLOG ); then echo "$D ERROR FTP LOGIN" >> $LOG; fi

: , ftp. .

, Scriptlanguage, Perl, Python Ruby. FTP-, . . Perl:

#!/usr/bin/perl -w
use Net::FTP;
$ftp = Net::FTP->new("example.net") or die "Cannot connect to example.net: $@";
$ftp->login("username", "password") or die "Cannot login ", $ftp->message;
$ftp->cwd("/pub") or die "Cannot change working directory ", $ftp->message;
$ftp->binary;
$ftp->put("foo.bar") or die "Failed to upload ", $ftp->message;
$ftp->quit;
+2

lame answer , ftp

+1

Anurag, grep -v "bytes"

ie

grep "^ 530" ftp_out2.txt | grep -v "byte"

- 530 , Anurag.

+1

. . . , , , .

echo "open ftp_ip
pwd
binary    
lcd /out
cd /in
mput datafile.csv
quit"|ftp -iv > ftpreturn.log

ftpresult=$?

bytesindatafile=`wc -c datafile.csv | cut -d " " -f 1`    
bytestransferred=`grep -e '^[0-9]* bytes sent' ftpreturn.log | cut -d " " -f 1`    
ftptransfercomplete=`grep -e '226 ' ftpreturn.log | cut -d " " -f 1`

echo "-- FTP result code: $ftpresult" >> ftpreturn.log
echo "-- bytes in datafile: $bytesindatafile bytes" >> ftpreturn.log
echo "-- bytes transferred: $bytestransferred bytes sent" >> ftpreturn.log

if [ "$ftpresult" != "0" ] || [ "$bytestransferred" != "$bytesindatafile" ] || ["$ftptransfercomplete" != "226" ]    
then    
  echo "-- *abend* FTP Error occurred" >> ftpreturn.log    
  mailx -s 'FTP error' `cat email.lst` < ftpreturn.log
else    
  echo "-- file sent via ftp successfully" >> ftpreturn.log    
fi
0

, FTP , , FTP- BSD , . BSD FTP , , Unix, FTP, . FTP, .

FTPUSER - ftp-

FTPPASS - FTP-

FILE - , - (, file1.txt, /whatever/file 1.txt -/file1.txt

FTPHOST - , FTP

REMOTEDIR - , ,

:

curl --user $FTPUSER: $FTPPASS -T $FILE ftp://$FTPHOST/% 2f $REMOTEDIR

ftp-upload --host $FTPHOST --user $FTPUSER --password $FTPPASS --as $REMOTEDIR/$FILE $FILE

tnftp -u ftp://$FTPUSER: $FTPPASS @$FTPHOST/% 2f $REMOTEDIR/$ $

wput $ ftp://$FTPUSER: $FTPPASS @$FTPHOST/% 2f $REMOTEDIR/$FILE

, - , , , . , , , , , .. .

:

  • "% 2f" URL-, , . , FTP- , .

  • , URL- (ftp://etc) , , URL, .

  • , , - , . , .

  • , FTP-, , , script, , , , GET . FTP script , , . , , , , , , . BSD FTP .

0

Why not just save all the output from the command to the log file, and then check the return code from the command and, if it is not 0, send the log file in an email?

-1
source

Source: https://habr.com/ru/post/1697576/


All Articles