首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

用expect文件同步脚本实现升级自动化

首先安装tcl,下载地址如下

https://jaist.dl.sourceforge.net/project/tcl/Tcl/8.4.11/tcl8.4.11-src.tar.gz

expect的下载地址:

https://jaist.dl.sourceforge.net/project/expect/Expect/5.45/expect5.45.tar.gz

安装tcl:

cd ./tcl8.4.11/unix/

./configure

make && make install

安装expect:

tar -zxvf expect5.45.tar.gz

cd expect5.45

./configure --prefix=/usr/local/expect5.45/ --with-tcl=//usr/local/tcl8.4.11/unix/ --with-tclinclude=/usr/local/tcl8.4.11/generic/ --enable-shared

[appuser@AY1310231051052001fdZ ~]$whereis tcl

tcl: /usr/lib/tcl8.4 /usr/local/lib/tcl8.4 /usr/share/tcl8.4

[appuser@AY1310231051052001fdZ ~]$whereis expect

expect: /usr/local/bin/expect /usr/local/lib/expect5.45 /usr/local/expect5.45

[appuser@AY1310231051052001fdZ ~]$expect

expect1.1>

环境准备完毕!

脚本1:expect_scp

#!/usr/local/bin/expect

set timeout 600

set host [lindex $argv 0]

set port [lindex $argv 1]

set username [lindex $argv 2]

set password [lindex $argv 3]

set source_path [lindex $argv 4]

set target_path [lindex $argv 5]

spawn scp -P$port -r $source_path $username@$host:$target_path

expect {

"(y/n)?" {

send "yes\n"

expect "*assword:" { send "$password\n"}

}

"*assword:" {

send "$password\n"

}

}

#expect "100%"

#expect eof

脚本2:deploy.sh

#!/bin/bash

source_dir="/alidata/myupdate/"$1 #源文件存放路径

target_dir=$2 #同步文件目标路径

host=$3 #目标服务器ip

port=$4 #目标服务器断开

username=testuser #登录目标服务器用户名

#password=$5 #登录密码

logfile=/alidata/log/updatelog/deploy.`date -d "-0 day" +%F`.log #同步日志文件

#echo $source_dir $target_dir $host $port $username $password

#服务器1

if [ $host == 'IP1' ]; then

password='passwd1'

#服务器2

elif [ $host == 'IP2' ]; then

password='passwd2'

#服务器3

elif [ $host == 'IP3' ]; then

password='passwd3'

fi

# 目录不存在,则创建,如果存在先删除再创建

echo -e '+++++++'`date "+%Y-%m-%d %H:%M:%S"` start $source_dir '++++' >> $logfile

echo -e '+++++++'`date "+%Y-%m-%d %H:%M:%S"` start $source_dir '++++++'

if [ ! -d $source_dir ]; then

echo $source_dir "is not exist"

else

# 将源路径文件夹拷贝目标服务器

/home/appuser/script/expect_scp $host $port $username $password $source_dir $target_dir

fi

echo -e '===='`date "+%Y-%m-%d %H:%M:%S"` end $source_dir ===' >> $logfile

echo -e '====='`date "+%Y-%m-%d %H:%M:%S"` end $source_dir '==='

将两个脚本放在同一路径下!

[appuser@AY1310231051052001fdZ ~]$ deploy.sh "group.war " "/alidata/jboss/jboss4.3/jboss/server/group/deploy" "10.161.168.178" "50001"

+++++++++++++++++++++++++++++2018-03-08 16:59:54 start /alidata/myupdate/group.war +++++++++++++++++++++++++++++

spawn scp -P50001 -r /alidata/myupdate/group.war appuser@10.161.168.178:/alidata/jboss/jboss4.3/jboss/server/group/deploy

appuser@10.161.168.178's password: =============================2018-03-08 16:59:54 end /alidata/myupdate/group.war ==============================

[appuser@AY1310231051052001fdZ ~]$ tail -5 /alidata/log/updatelog/deploy.2018-03-08.log

==============================2018-03-08 16:49:46 end /alidata/myupdate/lis ==============================

+++++++++++++++++++++++++++++2018-03-08 16:50:00 start /alidata/myupdate/lis +++++++++++++++++++++++++++++

=============================2018-03-08 16:50:00 end /alidata/myupdate/lis ==============================

+++++++++++++++++++++++++++++2018-03-08 16:59:54 start /alidata/myupdate/group.war +++++++++++++++++++++++++++++

=============================2018-03-08 16:59:54 end /alidata/myupdate/group.war ==============================

[appuser@AY1310231051052001fdZ ~]$

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180308G1GVQ600?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券