自动FTP脚本

自动FTP脚本 收藏
现网要升级到B470版本,而现网有30台机器需要安装DSS和SERSRV,如果手动安装的话很费力气,所有想利用脚本来完成自动上传自动安装的功能。基本思想是采用csh编写ftp命令,和telnet命令去远程控制30台的机器。

 

#!/bin/sh

 

#配置参数

CONFIG_FILE='FTP.conf'

DSS_HOST_IP_FILE='IP-DSS.conf'

SERSRV_HOST_IP_FILE='IP-Server.conf'

SERSRV_HOST_FLOAT_IP_FILE='IP-Server-Float.conf'

DSS_NAME='DSS'

SERSRV_NAME='SERSRV'

 

#加载upload.conf配置参数

DSS_GZ_NAME=`grep 'DSS_GZ_NAME' $CONFIG_FILE | awk '{print $2}'`

DSS_CSHRC=`grep 'DSS_CSHRC' $CONFIG_FILE | awk '{print $2}'`

DSS_USER=`grep 'DSS_USER' $CONFIG_FILE | awk '{print $2}'`

DSS_PASSWORD=`grep 'DSS_PASSWORD' $CONFIG_FILE | awk '{print $2}'`

 

SERSRV_GZ_NAME=`grep 'SERSRV_GZ_NAME' $CONFIG_FILE| awk '{print $2}'`

SERSRV_CSHRC=`grep 'SERSRV_CSHRC' $CONFIG_FILE | awk '{print $2}'`

SERSRV_USER=`grep 'SERSRV_USER' $CONFIG_FILE | awk '{print $2}'`

 

SERSRV_PASSWORD=`grep 'SERSRV_PASSWORD' $CONFIG_FILE | awk '{print $2}'`

SERSRV_WAP_CSHRC=`grep 'SERSRV_WAP_CSHRC' $CONFIG_FILE | awk '{print $2}'`

 

#准备DSS自动上传脚本参数

PrepareDSS()

{

#将加载后的参数打印出来,供执行者确认

echo $DSS_NAME'       gz :['$DSS_GZ_NAME']'

echo $DSS_NAME'    cshrc :['$DSS_CSHRC']'

echo $DSS_NAME' username :['$DSS_USER']'

echo $DSS_NAME' password :['$DSS_PASSWORD']'

 

echo 'Please confirm the config, press y if you want to upload dss ?[y or n]'

#确认参数配置正确与否,正确输入y,程序继续执行,不正确输入n,程序退出

while read flag

do

  case $flag in

  y|Y|yes|Yes|YES) echo 'yes is selected, start upload dss!'

  break

  ;;

  n|N|no|NO) echo 'no is selected, shell will exit!'

  exit 0

  ;;

  *) echo 'Unknown command, please try again!'

  continue

  ;;

  esac

  done

 

}

 

#准备SERSRV自动上传脚本参数

PrepareSERSRV()

{

#将加载后的参数打印出来,供执行者确认

echo $SERSRV_NAME'       gz :['$SERSRV_GZ_NAME']'

  echo $SERSRV_NAME'    cshrc :['$SERSRV_CSHRC']'

  echo $SERSRV_NAME' username :['$SERSRV_USER']'

  echo $SERSRV_NAME' password :['$SERSRV_PASSWORD']'

 

echo 'Please confirm the config, press y if you want to upload sersrv ?[y or n]'

#确认参数配置正确与否,正确输入y,程序继续执行,不正确输入n,程序退出

while read flag

do

  case $flag in

  y|Y|yes|Yes|YES) echo 'yes is selected, start upload sersrv!'

  break

  ;;

  n|N|no|NO) echo 'no is selected, shell will exit!'

  exit 0

  ;;

  *) echo 'Unknown command, please try again!'

  continue

  ;;

  esac

  done

 

}

 

#准备SERSRV自动上传脚本参数

PrepareAll()

{

#将加载后的参数打印出来,供执行者确认

echo $DSS_NAME'       gz :['$DSS_GZ_NAME']'

echo $DSS_NAME'    cshrc :['$DSS_CSHRC']'

echo $DSS_NAME' username :['$DSS_USER']'

echo $DSS_NAME' password :['$DSS_PASSWORD']'

 

#将加载后的参数打印出来,供执行者确认

echo $SERSRV_NAME'       gz :['$SERSRV_GZ_NAME']'

  echo $SERSRV_NAME'    cshrc :['$SERSRV_CSHRC']'

  echo $SERSRV_NAME' username :['$SERSRV_USER']'

  echo $SERSRV_NAME' password :['$SERSRV_PASSWORD']'

 

echo 'Please confirm the config, press y if you want to upload sersrv ?[y or n]'

#确认参数配置正确与否,正确输入y,程序继续执行,不正确输入n,程序退出

while read flag

do

  case $flag in

  y|Y|yes|Yes|YES) echo 'yes is selected, start upload sersrv!'

  break

  ;;

  n|N|no|NO) echo 'no is selected, shell will exit!'

  exit 0

  ;;

  *) echo 'Unknown command, please try again!'

  continue

  ;;

  esac

  done

 

}

 

#调用FTP脚本上传DSS

FtpDSS()

{

#开始循环上传DSS文件

for loop in `cat $DSS_HOST_IP_FILE | awk '{print $1}'`

do

  echo ==========================FTP DSS to the host[$loop]==========================

  perl -p -i -e 's/127.0.0.1/'$loop'/' $DSS_CSHRC

  ./FTP-UPLOAD.sh $loop $DSS_USER $DSS_PASSWORD $DSS_GZ_NAME $DSS_CSHRC

  perl -p -i -e 's/'$loop'/127.0.0.1/' $DSS_CSHRC

done

}

 

 

#调用FTP脚本上传SERSRV

FtpSERSRV()

{

#开始循环上传SERSRV文件

for loop in `cat $SERSRV_HOST_IP_FILE | awk '{print $1}'`

do

  echo ==========================FTP SERSRV to host[$loop]==========================

  float_ip=`grep "$loop" $SERSRV_HOST_FLOAT_IP_FILE | awk '{print $2}'`  

  perl -p -i -e 's/127\.0\.0\.1/'$float_ip'/' $SERSRV_CSHRC 

  ./FTP-UPLOAD.sh $loop $SERSRV_USER $SERSRV_PASSWORD $SERSRV_GZ_NAME $SERSRV_CSHRC

  perl -p -i -e 's/'$float_ip'/127\.0\.0\.1/' $SERSRV_CSHRC

done

}

 

# 显示本程序用法

ShowUsage()

{

  if [ "x`echo $LANG | grep zh`" != "x" ]; then

    echo "用法:"

    echo "  `basename $0` dss                     启动DSS自动上传脚本."

    echo "  `basename $0` sersrv                  启动SERSRV自动上传脚本."

    echo "  `basename $0` all                     启动DSS和SERSRV自动上传脚本."

    echo "  `basename $0` --help                  显示脚本用法."

    exit 1

  else

    echo "Usage:"

    echo "  `basename $0` dss                     Start DSS auto upload shell."

    echo "  `basename $0` sersrv                  Start SERSRV auto upload shell."

    echo "  `basename $0` all                     Start DSS and SERSRV auto upload shell."

    echo "  `basename $0` --help                  Show Help."

    exit 1

  fi

}

 

if [ $#  -eq  0 ];then

# CheckShell

  ShowUsage

  exit 0

fi

case $1 in

dss|DSS)

  PrepareDSS

  FtpDSS

  exit 0

  ;;

sersrv|SERSRV)

  PrepareSERSRV

  FtpSERSRV

  exit 0

  ;;

all|ALL)

  PrepareAll

FtpDSS

  FtpSERSRV

  exit 0

  ;;

--help)

  ShowUsage

  exit 0

  ;;

esac

ShowUsage

  exit 0

 

主要设计到的知识点有,read等待用户输入,并更具输入做出不同的响应,以及从配置文件加载参数grep 【关键字】 | awk ‘{print $2}’,将参数以打屏方式打echo出来,使用for循环对ip地址进行循环for loop in cat [文本] | awk ‘{print $1}’,使用perl命令替换文本中的关键字perl –p –i –e ‘s/[关键字]/[替换后关键字]/’ [文本]。SHELL参数的制作,使用read读取用户输入,判断相应的输入调用相应的函数,做好把每个功能点都封装成一个函数以供其他地方重复调用。使用*号来匹配其他输入。

还用到主要的流程控制语句,if else fi while do done for loop in 【】 do done

 

使用ftp命令:

#!/bin/sh

 

ftp -i -n $1 <<!

user $2 $3

put $4

put $5

quit

!

登录FTP后就进入ftp交互界面,脚本需要特殊处理以防执行中断。

处理方式如下:

ftp -inv<<!

//your cmd

……

你可能感兴趣的:(脚本,perl,WAP)