Skip to content

Latest commit

 

History

History
99 lines (86 loc) · 1.8 KB

20.md

File metadata and controls

99 lines (86 loc) · 1.8 KB

expect同步文件

#!/usr/bin/expect
set passwd "SGs2ox6uj"
set host "192.168.1.180"
spawn rsync -a  user01@$host:/tmp/test.txt /tmp/
expect {
    "yes/no" {send "yes\r"}
    "password:" {send "$passwd\r"}
}
expect eof

传递参数

#!/usr/bin/expect
set passwd "SGs2ox6uj"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -a --files-from=$file  / user01@$host:/
expect {
    "yes/no" {send "yes\r"}
    "password:" {send "$passwd\r"}
}
expect eof

本案例参考脚本

#/bin/bash
##代码上线
##作者:阿铭
##日期:2018-11-12

#提醒用户,是否更新了要上线的代码列表文件
read -p "你是否已经更新了文件列表./file.list?确认请输入y或者Y,否则按其他任意键退出脚本。" c

#如果直接按回车,也会退出脚本
if [ -z "$c" ]
then
    exit 1
fi

if [ $c == "y" -o $c == "Y" ]
then
    echo "脚本将在2秒后,继续执行。"
    #每秒输出一个.共输出两个.
    for i in 1 2
    do
        echo -n "."
        sleep 1
    done
    echo
else
    exit 1
fi

#判断有无./rsync.exp文件
[ -f ./rsync.exp ] && rm -f ./rsync.exp

#定义rsync.exp
cat >./rsync.exp <<EOF
#!/usr/bin/expect
set passwd "SGs2ox6uj"
set host [lindex \$argv 0]
set file [lindex \$argv 1]

spawn rsync -avR --files-from=\$file  /  user01@\$host:/
expect {
    "yes/no" {send "yes\r"}
    "password:" {send \$passwd\r}
}
expect eof
EOF

chmod a+x ./rsync.exp

#定义检测文件是否存在的函数
if_file_exist()
{
    if [ ! -f $1 ]
    then
        echo "文件$1不存在,请检查。"
        exit 1
}

#ip.list为所有WEB机器的ip列表
#file.list为要同步的文件列表

if_file_exist ./ip.list
if_file_exist ./file.list

for ip in `cat ./ip.list`
do
    ./rsync.exp $ip ./file.list
done

#善后处理
rm -f ./rsync.exp