使用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=$2containsElement () {set -A week mon tue wen wed thu fri sat sun ONLY bad#for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; donefor e in ${week[@]}do if [[ "$e" == "$1" ]]; thenecho "0"returnfidoneecho "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"thenreturn 0    # $substring is in $stringelsereturn 1    # $substring is not in $stringfi
}# 0-invalid; 1-valid
isvalid()
{string=$1if [ ${string%*.*} != $string ] || [ ${string%*_*} != $string ]; then        echo "0"returnfiend=${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`dofilename=`basename $path`r=`isvalid $filename`if [[ $r != 0 ]]; thenMarVer=${filename:0:3}MinVer=$filenameindex=`_index $MinVer "-"`if [[ $index -gt 0 ]]; thenindex=$index+1MinVer=${MinVer:$index}elif [[ ${#MinVer} = 6 ]]; thenMinVer=${MinVer:3}fiecho "MinVer"$MinVer" length:"${#MinVer}echo $MarVer $MinVer "-load st-lnx-$MarVer"startAutoview $MarVer $MinVerBLD_FOLDER=`insertdot $MarVer`VERSION_FOLDER=$filename"-Linux-objdump"echo "start to copy obj files to ftp server"cptoftpecho "delete obj files locally after uploaded"rm -rf $WORK_DIR$VERSION_FOLDERfidone
#Sdoneecho "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 解决办法


Configure an inbound firewall rule to allow inbound FTP connections to only the ports on which FTP is listening

  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.



本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部