使用KSH编写的一个自动化工具

这是一个边学边做的KSH脚本实例

任务描述:

  1. 检查目标文件夹是否有最近新生成的版本目录
  2. 检验版本目录是否为正式版
  3. 调用其他.sh文件对有效的版本进行二进制编译
  4. 将新生成的二进制文件上传至ftp server上对应版本的文件夹中
  5. 定期自动执行该脚本

用到的KSH操作:

  1. 全局变量定义、引用,数组、for循环、if条件判断、函数的声明及调用,返回值
  2. 文件操作,遍历、文件夹创建时间、文件夹名字获取、文件夹层级限制
  3. 字符串操作,截取、长度、子字符串
  4. ftp操作,登录、上传、创建服务器端文件夹


模块列表

/mount/proj-psft/jiuhzhan/autowrkspace/modlist.lst


脚本全文

#!/bin/ksh

# ftp account info
THIS_USER=`whoami`
FTP_SITE=slc03hni
USERNAM=autodump
PASSWORD=Auto,dump
WORK_DIR="/mount/proj-psft/"$THIS_USER"/autowrkspace/"
PATH=/jre/prod/1.8.0/bin:/bin:/sbin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/etc:/usr/local/etc:/usr/bin/X11:.:/dssapps/psbin:/tuxedo/prod/12.1.3.0-j12-64bit/bin:/cobol/prod/svrexp-5.1_wp11-64bit/bin:/clrcase/prod/current/bin:/products/oracle/11.2.0.4.0-64bit/bin:/pt/bin:/pt/products/oel-5-x86_64/bin

#build folder and version folder, say 855 and 855-801-I1-Linux-objdump
BLD_FOLDER=$1
VERSION_FOLDER=$2

containsElement () {
  set -A week mon tue wen wed thu fri sat sun ONLY bad
  #for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  for e in ${week[@]}
  do 
    if [[ "$e" == "$1" ]]; then
        echo "0"
        return
    fi
  done
  echo "1"
}

_rindex() 
{ 
    case $1 in 
        *$2*)
            idx=${1%$2*};
            echo $(( ${#idx} ))
        ;;
        *)
            echo "-1";
            return
        ;;
    esac
}

_index() 
{ 
    case $1 in 
        *$2*)
            idx=${1%%$2*};
            echo $(( ${#idx} ))
        ;;
        *)
            echo "-1";
            return
        ;;
    esac
}

# contains(string, substring)
#
# Returns 0 if the specified string contains the specified substring,
# otherwise returns 1.
contains() {
    string="$1"
    substring="$2"
    if test "${string#*$substring}" != "$string"
    then
        return 0    # $substring is in $string
    else
        return 1    # $substring is not in $string
    fi
}

# 0-invalid; 1-valid
isvalid()
{
    string=$1
    if [ ${string%*.*} != $string ] || [ ${string%*_*} != $string ]; then        
        echo "0"
        return
    fi
    end=${string##*-}    
    containsElement $end
}

#855 => 8.55
insertdot()
{
echo $1|sed 's/^\(.\{1\}\)/\1./'
}

#start autoview.sh compile
# ./autoview.sh -M 855 -m 801-I1 -v 855-801-I1_Linux_autowizd -I Y -r retail -D ORA -u 0 -copf -op
startAutoview()
{
./autoview.sh -M $1 -m $2 -v $2_Linux_autowizd -I Y -r retail -D ORA -u 0 -copf -op
}

cptoftp()
{
echo "***********************************************************************************"
echo "*                           - Autodump create objdump folder  -                   *"
echo "***********************************************************************************"

ftp -in <<-EOF
open $FTP_SITE
user $USERNAM $PASSWORD
cd $BLD_FOLDER
mkdir $VERSION_FOLDER
cd $VERSION_FOLDER
lcd $WORK_DIR$VERSION_FOLDER
mput ./*
close
bye
EOF
}

#build version info
#set -A bildfolerArray 852 853 854 855 856
BUILD_FOLDER_ROOT=/dfs/build/pt/ptdist/pt


#find all valid directories created within 24hrs
#MarVer:"855", MinVer:"801-I1"
#for i in ${bildfolerArray[@]}
#do
    #echo $BUILD_FOLDER_ROOT$i
    #for path in  `find $BUILD_FOLDER_ROOT$i/* -maxdepth 0 -type d -ctime -7` 
    for path in  `find $BUILD_FOLDER_ROOT$BLD_FOLDER/* -maxdepth 0 -type d -ctime -7`
    do
        filename=`basename $path`
        r=`isvalid $filename`
        
        if [[ $r != 0 ]]; then
            MarVer=${filename:0:3}
            MinVer=$filename
            index=`_index $MinVer "-"`
            if [[ $index -gt 0 ]]; then
                index=$index+1
                MinVer=${MinVer:$index}
            elif [[ ${#MinVer} = 6 ]]; then
                MinVer=${MinVer:3}
            fi
            echo "MinVer"$MinVer" length:"${#MinVer}
            echo $MarVer $MinVer "-load st-lnx-$MarVer"
            startAutoview $MarVer $MinVer

            BLD_FOLDER=`insertdot $MarVer`
            VERSION_FOLDER=$filename"-Linux-objdump"
            echo "start to copy obj files to ftp server"
            cptoftp
            echo "delete obj files locally after uploaded"
            rm -rf $WORK_DIR$VERSION_FOLDER
        fi
    done
#Sdone

echo "Done!"
exit 0

crontab的使用

要求每周四晚上23点自动执行脚本

执行进入编辑状态

crontab -e

00 23 * * 4 /home/jiuhzhan/peopleauto/autoftp.sh 855

列举当前自动执行的命令行

crontab -l

crontabl脚本运行中的问题

批量上传文件到Windows FTP Server

尽量使用绝对路径,比如

cptoftp()
{
echo "***********************************************************************************"
echo "*                           - Autodump create objdump folder  -                   *"
echo "***********************************************************************************"

ftp -in <<-EOF
passive
open $FTP_SITE
user $USERNAM $PASSWORD
cd $BLD_FOLDER
mkdir $VERSION_FOLDER
cd $VERSION_FOLDER
lcd $WORK_DIR$VERSION_FOLDER
mput $WORK_DIR$VERSION_FOLDER/*
close
bye
EOF
}


其中的mput处,不要使用如下方式

lcd $WORK_DIR$VERSION_FOLDER
mput ./*

否则在crontab执行的时候, mput ./*执行的可能是 /home/username下的所有文件

Passive mode refused 解决办法


  1. Open an Administrator command-prompt. Click Start, click All Programs, click Accessories, right-click Command Prompt, and then clickRun as Administrator.

  2. Run the following command:

    netsh advfirewall firewall add rule name=”FTP Service” action=allow service=ftpsvc protocol=TCP dir=in
    
  3. Finally, disable stateful FTP filtering so that the firewall does not block any FTP traffic.

    netsh advfirewall set global StatefulFTP disable
    

这样如果还是弹出如下错误

Passive mode refused.
No control connection for command: Illegal seek


在open之前加上一行,passive

ftp -in <<-EOF
passive
open $FTP_SITE
user $USERNAM $PASSWORD
cd $BLD_FOLDER
mkdir $VERSION_FOLDER
cd $VERSION_FOLDER
lcd $WORK_DIR$VERSION_FOLDER
mput ./*

运行该脚本会打印

Passive mode off.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值