教程集 www.jiaochengji.com
教程集 >  脚本编程  >  shell  >  正文 经典FTP Shell脚本一例

经典FTP Shell脚本一例

发布时间:2014-11-17   编辑:jiaochengji.com
分享一个经典的ftp shell脚本,实现文件的ftp本地、远程服务器的互转,有需要的朋友参考下。

以下shell脚本,实现ftp文件操作:本地到远程服务器,以及远程到本地的文件传输。

代码:
 

复制代码 代码示例:

#!/usr/bin/ksh
# Program        : ftp_script.sh
#     # Functions      : get_job_num,            
#     check_ftp_log,                                                       
#                  send_notify,                                                         
#                  build_ftp_script,                                                    
#                  backup_data,                                                         
#                  run_ftp_script,                                                      
#                  log_err_msg   # Child Scripts  : None
# Change History :

get_job_num()
{
#   Build the job number to fetch details from TABLE
    NUM=$1
    LEN=${#NUM}
    case $LEN in
        1) JOB_NAME=xyz000${NUM};;
        2) JOB_NAME=xyz00${NUM};;
        3) JOB_NAME=xyz0${NUM};;
        4) JOB_NAME=xyz${NUM};;
    esac
    return
}

# -------------------------------------------------------------------------- #
# Function    : build_ftp_script                                             #
# Parameters  : Source File Name,                                            #
#               Destination File Name                                        #
# Called By   : Main Process                                                 #
# Description : Builds dynamic FTP script to FTP the file from UNIX to      #
#  mainframe.      
# ------------------------------------------------------------------------- #

build_ftp_script()
{
#   Building the FTP script
#   IP Address, FTP Parameters and Port Number

    if [[ $FIREWALL = "Y" ]]; then
        echo "ftp -$FTP_PARAM $FIRE_WALL << EOF >> $LOG_DIR/$FTP_LOG_FILE 2>&1" > $COMM_DIR/$FTP_FILE
    else
        if [[ $FTP_PARAM != "NA" ]]; then
            if [[ $PORT_NO != "NA" ]]; then
                   echo "ftp -$FTP_PARAM $IP_ADD $PORT_NO << EOF >> $LOG_DIR/$FTP_LOG_FILE 2>&1" > $COMM_DIR/$FTP_FILE
            else
                echo "ftp -$FTP_PARAM $IP_ADD << EOF >> $LOG_DIR/$FTP_LOG_FILE 2>&1" > $COMM_DIR/$FTP_FILE
            fi
        else
            if [[ $PORT_NO != "NA" ]]; then
                echo "ftp -v $IP_ADD $PORT_NO << EOF >> $LOG_DIR/$FTP_LOG_FILE 2>&1" > $COMM_DIR/$FTP_FILE
            else
                echo "ftp -v $IP_ADD << EOF >> $LOG_DIR/$FTP_LOG_FILE 2>&1" > $COMM_DIR/$FTP_FILE
            fi
        fi
    fi

#   Login and Passowrd information
    if [[ $LOGIN_ID != "NA" && $PASS != "NA" ]]; then
        if [[ $FIREWALL = "Y" ]]; then
            echo "user $LOGIN_ID@$IP_ADD $PASS" >> $COMM_DIR/$FTP_FILE
        else
            echo "user $LOGIN_ID $PASS" >> $COMM_DIR/$FTP_FILE
        fi
    fi

#   Mode of transfer
    if [[ $MODE != "NA" ]]; then
        echo "$MODE" >> $COMM_DIR/$FTP_FILE
    fi

#   Site parameters
    if [[ $SITE_PARAM != "NA" ]]; then
        echo "quote site $SITE_PARAM" >> $COMM_DIR/$FTP_FILE
    fi
#   Change the destination directory if needed

    if [[ $DEST_DIR != "NA" ]]; then
        echo "cd $DEST_DIR" >> $COMM_DIR/$FTP_FILE
    fi

#   Suppress Prompting
    echo "prompt off" >> $COMM_DIR/$FTP_FILE

#   Type of transfer
    if [[ $DEST_FILE != "NA" ]]; then
        if [[ $DEST_TS = "Y" ]]; then
            DEST_FILE=$DEST_FILE.`date +%Y%m%d%H%M%S`
            echo "$FTP_CMD $1 .$DEST_FILE" >> $COMM_DIR/$FTP_FILE
            echo "rename .$DEST_FILE $DEST_FILE" >> $COMM_DIR/$FTP_FILE
        else
            echo "$FTP_CMD $1 $DEST_FILE" >> $COMM_DIR/$FTP_FILE
        fi
    else
        echo "$FTP_CMD $1" >> $COMM_DIR/$FTP_FILE
    fi

    echo "bye" >> $COMM_DIR/$FTP_FILE
    echo "done" >> $COMM_DIR/$FTP_FILE
    return
}

# -------------------------------------------------------------------------------------- #
# Function    : run_ftp_script                                                           #
# Parameters  : Not Applicable                                                           #
# Called By   : Main Process                                                             #
# Description : Runs the dynamically built FTP script and log the details in a log file. #
# -------------------------------------------------------------------------------------- #

run_ftp_script()
{
#   Change permissions and execute ftp file
    chmod 755 $COMM_DIR/$FTP_FILE
    $COMM_DIR/$FTP_FILE
    cat $LOG_DIR/$FTP_LOG_FILE
    rm -f $COMM_DIR/$FTP_FILE
    log_err_msg "RUNNING " "Deleted temporary FTP script"
    return
}

# -------------------------------------------------------------------------------------- #
# Function    : check_ftp_log                                                            #
# Parameters  : Not Applicable                                                           #
# Called By   : Main Process                                                             #
# Description : Checks the log file generated by the FTP script for error messages and   #
#               creates an error message file that is to be mailed to support group.     #
# -------------------------------------------------------------------------------------- #

check_ftp_log()
{
#   Checking FTP log file
    /usr/xpg4/bin/grep -q "Login failed" $LOG_DIR/$FTP_LOG_FILE

    if [ $? -eq 0 ];then
        echo "Communication Error in: "$JOB_NAME > $MSGS_DIR/$MAIL_FILE
        echo "
$JOB_NAME FTP Error - Login Failed"
        log_err_msg "ERROR   " "FTP Error - Login failed"
        send_notify
        return 1
    fi

    /usr/xpg4/bin/grep -q "Not connected" $LOG_DIR/$FTP_LOG_FILE

    if [ $? -eq 0 ];then
        echo "Communication Error in: "$JOB_NAME > $MSGS_DIR/$MAIL_FILE
        echo "
$JOB_NAME FTP Error - Not Connected"
        log_err_msg "ERROR   " "FTP Error - Not connected"
        send_notify
        return 1
    fi

    /usr/xpg4/bin/grep -q "Service not available" $LOG_DIR/$FTP_LOG_FILE

    if [ $? -eq 0 ];then
        echo "Communication Error in: "$JOB_NAME > $MSGS_DIR/$MAIL_FILE
        echo "
$JOB_NAME FTP Error - Not Connected"
        log_err_msg "ERROR   " "FTP Error - Not connected"
        send_notify
        return 1
    fi

    /usr/xpg4/bin/grep -q "User not authorized." $LOG_DIR/$FTP_LOG_FILE

    if [ $? -eq 0 ];then
        echo "User Authorization Error in : "$JOB_NAME > $MSGS_DIR/$MAIL_FILE
        echo "
$JOB_NAME FTP Error - User Not Authorized"
        log_err_msg "ERROR   " "FTP Error - User not authorized"
        send_notify
        return 1
    fi

    return 0
}

# -------------------------------------------------------------------------------------- #
# Function    : backup_data                                                              #
# Parameters  : Not Applicable                                                           #
# Called By   : Main Process                                                             #
# Description : Archives the input file in archival directory and deletes the input file #
#               as well as the FTP script.                                               #
# -------------------------------------------------------------------------------------- #

backup_data()
{
#   Deleting the Temporary ftp-exec file and archiving the input data

    if [[ $2 != "" ]]; then
        mv $STG_DIR/$1 $ORG_DIR/$ARCH_DIR/$1.$2
    else
        mv $STG_DIR/$1 $ORG_DIR/$ARCH_DIR/$1
    fi 

    if [[ $CAT != "N" ]]; then
        log_err_msg "RUNNING " "Archived $SRC_FILE"
    else
        log_err_msg "RUNNING " "Archived the file(s) `cat $COMM_DIR/$NUM.dirlist`"   
    fi
 
#    rm -f $COMM_DIR/$FTP_FILE
#    log_err_msg "RUNNING " "Deleted temporary FTP script"
    return
}

# -------------------------------------------------------------------------------------- #
# Function    : send_notify                                                              #
# Parameters  : Not Applicable                                                           #
# Called By   : check_ftp_log                                                            #
# Description : Mails the FTP error message file to the mail id list.   #
# -------------------------------------------------------------------------------------- #

send_notify()
{
#   Mail the error message file to support groups

    set -A NOTES_ID $MAIL_ID
    echo "Job ran at : " $ftpDate >> $MSGS_DIR/$MAIL_FILE
    echo "
" >> $MSGS_DIR/$MAIL_FILE
    cat $LOG_DIR/$FTP_LOG_FILE >> $MSGS_DIR/$MAIL_FILE

    for i in ${NOTES_ID[*]}
    do
        cat $MSGS_DIR/$MAIL_FILE | mailx -s "Intra-company Machine FTP Notification -  $JOB_NAME" $i
    done

    log_err_msg "RUNNING " "Mailed error details to support groups"
    return
}

# -------------------------------------------------------------------------------------- #
# Function    : log_err_msg                                                              #
# Parameters  : Error Message Code,                                                      #
#               Error Message                                                            #
# Called By   : Main Process,                                                            #
#               check_ftp_log,                                                           #
#               backup_data,                                                             #
#               send_notify                                                              #
# Description : Logs the program status in log file.                                     #
# -------------------------------------------------------------------------------------- #

log_err_msg()
{
#   Log the script execution details in log file

    if [ $1 = "STARTING" ]; then
        echo "
        ${JOB_NAME} LOG FILE
" > $LOG_DIR/$SCRIPT_LOG_FILE
        echo "JOB NAME DATE AND TIME  STATUS   MESSAGE" >> $LOG_DIR/$SCRIPT_LOG_FILE
        echo "--------------------------------------------------------"
             >> $LOG_DIR/$SCRIPT_LOG_FILE
    fi

    echo "${JOB_NAME} "`date +"%m-%d-%Y %H:%M"`" $1 $2" >> $LOG_DIR/$SCRIPT_LOG_FILE
    return
}

# -------------------------------------------------------------------------------------- #
# Function    : Main Process                                                             #
# Parameters  : Job Number                                                               #
# Called By   : Not Applicable                                                           #
# Description : 1. Sets up the needed environment variables                              #
#               2. Checks for the existence of files to be FTPed                         #
#               3. Builds the FTP script to FTP the files                                #
#               4. Executes the FTP script and check for any FTP error                   #
#               5. Mails the error message file to support groups if FTP error occurs    #
#               6. Archives input data in the archival directory and deletes input data  #
#                  as well as the FTP script                                             #
# -------------------------------------------------------------------------------------- #

SCRIPT_NAME=`echo ${0%.*}`
SCRIPT_NAME=`echo ${SCRIPT_NAME##*/}`

if [[ $# < 1 ]]; then
    echo "Usage: ${SCRIPT_NAME}.sh <NUM>"
    exit 1
fi

NUM=$1
ftpDate=`date +%y%m%d%H%M`

FIRE_WALL="firewall.jiaochengji.com"
MSGS_DIR=$usr/msgs
SCRIPT_DIR=/usr/bin
SRC_TABLE=$SCRIPT_DIR/SRC_TABLE
FTP_TABLE=$SCRIPT_DIR/FTPPARM_TABLE
MAIL_TABLE=$SCRIPT_DIR/MAIL_TABLE

get_job_num $NUM

/usr/xpg4/bin/grep -q $JOB_NAME $SRC_TABLE
if [[ $? != 0 ]]; then
    echo "$JOB_NAME job details not found in $SRC_TABLE"
    echo | mailx -s "ERROR : $JOB_NAME job details not found in $SRC_TABLE" abc@jquerycn.cn
    exit 1
fi

/usr/xpg4/bin/grep -q $JOB_NAME $FTP_TABLE
if [[ $? != 0 ]]; then
    echo "$JOB_NAME job details not found in $FTP_TABLE"
    echo | mailx -s "ERROR : $JOB_NAME job details not found in $FTP_TABLE" abc@jquerycn.cn
    exit 1
fi

/usr/xpg4/bin/grep -q $JOB_NAME $MAIL_TABLE
if [[ $? != 0 ]]; then
    echo "$JOB_NAME job details not found in $MAIL_TABLE"
    echo | mailx -s "ERROR : $JOB_NAME job details not found in $MAIL_TABLE" abc@jquerycn.cn
    exit 1
fi

grep $JOB_NAME $SRC_TABLE  | read -r JOB_NAME ORG_ID CAT DEST_TS ARCH_DIR DEST_FILE SRC_DIR DEST_DIR
grep $JOB_NAME $FTP_TABLE  | read -r JOB_NAME IP_ADD PORT_NO LOGIN_ID PASS FTP_PARAM MODE FTP_CMD FIREWALL SITE_PARAM
grep $JOB_NAME $MAIL_TABLE | read -r JOB_NAME MAIL_ORG_ID MAIL_LOG_FILE MAIL_ID

ORG_DIR=/usr/data/$ORG_ID
COMM_DIR=$ORG_DIR/COMM
LOG_DIR=$COMM_DIR/log

FTP_FILE=${SCRIPT_NAME}.$NUM.ftp.$ftpDate
MAIL_FILE=$JOB_NAME.msg.$ftpDate
SCRIPT_LOG_FILE=${SCRIPT_NAME}.$NUM.log.$ftpDate
FTP_LOG_FILE=$NUM.ftp.log.$ftpDate

if [[ $FTP_CMD != "get" && $FTP_CMD != "mget" ]]; then
    SRC_FILE=$NUM.send.file
    STG_DIR=$COMM_DIR/$NUM.last.sent
else
    SRC_FILE=$NUM.rec.file
    STG_DIR=$COMM_DIR/$NUM.last.rec
fi

if [[ ! -d $STG_DIR ]];then
    echo "Directory $STG_DIR dosen't exist"
    echo | mailx -s "ERROR : Directory $STG_DIR dosen't exist" abc@jquerycn.cn
    exit 1
fi

if [[ ! -d $LOG_DIR ]];then
    echo "Directory $LOG_DIR dosen't exist"
    echo | mailx -s "ERROR : Directory $LOG_DIR dosen't exist" abc@jquerycn.cn
    exit 1
fi

#if [[ $ARCH_DIR != "NA" && ! -d $COMM_DIR/$ARCH_DIR ]];then
#    echo "Directory $COMM_DIR/$ARCH_DIR dosen't exist"
#    echo | mailx -s "ERROR : Directory $COMM_DIR/$ARCH_DIR dosen't exist" abc@jquerycn.cn
if [[ $ARCH_DIR != "NA" && ! -d $ORG_DIR/$ARCH_DIR ]];then
    echo "Directory $ORG_DIR/$ARCH_DIR dosen't exist"
    echo | mailx -s "ERROR : Directory $ORG_DIR/$ARCH_DIR dosen't exist" abc@jquerycn.cn
    exit 1
fi

log_err_msg "STARTING" "Starting FTP process"

cd $COMM_DIR
################### Remove Old Trash and Check for Existance of Files ####################
rm -f $STG_DIR/*
rm -f $NUM.dirlist

if [[ $FTP_CMD != "get" && $FTP_CMD != "mget" ]]; then
    ls $ORG_DIR/$SRC_DIR > $NUM.dirlist
    if [ -s $NUM.dirlist ]; then
        FILE_NUM=`wc -l $NUM.dirlist | awk '{print $1}'`
        log_err_msg "RUNNING " "$FILE_NUM file(s) found"
    else
        log_err_msg "RUNNING " "No files to send"
        log_err_msg "COMPLETE" "Program execution successful"
        exit 0
    fi

############################## Create Archive File Listing ###############################
    mv $ORG_DIR/$SRC_DIR/* $STG_DIR/ 2>/dev/null
    cd $STG_DIR
    ls > .$NUM.list 2>/dev/null

    if [[ $CAT != "N" ]]; then
        cat `cat .$NUM.list` > $SRC_FILE
        build_ftp_script $SRC_FILE $DEST_FILE
        log_err_msg "RUNNING " "FTPing $SRC_FILE"
    else
        for i in `cat .$NUM.list`
        do
            mv $STG_DIR/$i $STG_DIR/$i.$ftpDate
        done
        build_ftp_script "*" $DEST_FILE
        log_err_msg "RUNNING " "FTPing `cat .$NUM.list`"
    fi

    log_err_msg "RUNNING " "FTP file list archival successful"
else
    cd $STG_DIR
    build_ftp_script "*"
fi
   
run_ftp_script
check_ftp_log
RC=$?

if [[ $FTP_CMD != "get" && $FTP_CMD != "mget" ]]; then
    if [[ $RC = 1 ]]; then
        log_err_msg "RUNNING " "Check $FTP_LOG_FILE for details"
        log_err_msg "FAILED  " "Program execution failed"
        rm -f $STG_DIR/$SRC_FILE
        if [[ $CAT != "N" ]]; then
             mv $STG_DIR/* $ORG_DIR/$SRC_DIR/
        else
             cd $STG_DIR/   
             for k in `ls -1`
             do
                 mv $k $ORG_DIR/$SRC_DIR/${k%.*}
             done
        fi   
        log_err_msg "FAILED  " "Moved files to source directory"
        exit 1
    else
        if [[ $CAT != "N" ]]; then
            log_err_msg "RUNNING " "FTP of $SRC_FILE successful"
            if [[ $ARCH_DIR != "NA" ]]; then
#                mv .$NUM.list $COMM_DIR/$ARCH_DIR/$NUM.list.$ftpDate
                mv .$NUM.list $LOG_DIR/$NUM.list.$ftpDate
                backup_data $SRC_FILE $ftpDate
            fi
        else
            log_err_msg "RUNNING " " FTP of file(s) `cat $COMM_DIR/$NUM.dirlist` successful"
            if [[ $ARCH_DIR != "NA" ]]; then            
#                mv .$NUM.list $COMM_DIR/$ARCH_DIR/$NUM.list.$ftpDate
                mv .$NUM.list $LOG_DIR/$NUM.list.$ftpDate
                backup_data "*"
            fi
        fi
        log_err_msg "RUNNING " "$FILE_NUM file(s) FTPed successfully"
    fi
else
    if [[ $RC = 1 ]]; then
        log_err_msg "RUNNING " "Check $FTP_LOG_FILE for details"
        log_err_msg "FAILED  " "Program execution failed"
        exit 1
    else
        cd $COMM_DIR
        ls $STG_DIR/* > $NUM.dirlist
        if [ -s $NUM.dirlist ];then
            FILE_NUM=`wc -l $NUM.dirlist | awk '{print $1}'`
            log_err_msg "RUNNING " "$FILE_NUM file(s) received"
            FILE_NAME=`echo ${SRC_DIR##*/}`
            cat `cat $NUM.dirlist` > $STG_DIR/$FILE_NAME
            /usr/bin/rm -rf `cat $NUM.dirlist`
            cp $STG_DIR/$FILE_NAME $ORG_DIR/$SRC_DIR
            ls $STG_DIR/* > $NUM.dirlist
            if [[ $ARCH_DIR != "NA" ]]; then            
#                cp $NUM.dirlist $COMM_DIR/$ARCH_DIR/$NUM.list.$ftpDate
                backup_data $FILE_NAME $ftpDate
            fi
        else
            log_err_msg "RUNNING " "No file(s) received"
        fi
    fi
fi

log_err_msg "RUNNING " "Check $FTP_LOG_FILE for details"
log_err_msg "COMPLETE" "Program execution successful"
exit 0

您可能感兴趣的文章:
在shell脚本中使用ftp的方法分享
ftp自动下载文件脚本 ftp命令下载文件
使用ftp命令传输文件的shell脚本
Shell实现的 FTP 上传文件的脚本
ftp自动传输的shell脚本
一例mysql数据库备份的shell脚本
linux下用脚本实现自动ftp的方法
linux下mysql自动备份与ftp上传的shell脚本
Solaris shell下操作数据库的方法
自动登陆FTP服务器的小shell

[关闭]
~ ~