首先安装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 ~]$
领取专属 10元无门槛券
私享最新 技术干货