文件同步采用expect,需提前准备相关环环境,见http://www.imitker.com/index.php/post/404.html
两个脚本放到同一路径下

内容分别如下:
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
"
expect "*assword:" { send "$password
"}
}
"*assword:" {
send "$password
"
}
}
#expect "100%"
#expect eofdeploy.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 '========================================'
脚本执行demo:
配置环境变量,则脚本文件可处处可执行。



©️公众号:思考者文刀
- 上一篇: 文件同步工具expect
- 下一篇: 拜占庭将军问题和区块链技术
















评论