写在前面
所谓分发系统,就是一个主要用来上线代码或同步文件的脚本。
对于大公司而言,使用机器少则几台,多则几十甚至上百台。如何将网站更新、扩展代码全部都发布到这些服务器上呢?这里就用到了分发系统。
expect的安装使用
准备工作:安装 expect
expect自动远程登录

1 2 3
| chmod 744 1.expect
./1.expect
|
expect脚本运程传递参数
编写脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| vi 1.expect
#!/usr/bin/expect set user "root" set passwd 123456" spawn ssh $user@172.16.111.110
expect { "yes/no" { send "yes\r"; exp_continue} "password:" { send "$passwd\r" } } //表示图中括号里的,表示当检测到这个符号时就执行我们要执行的命令 expect "]*" send "touch /tmp/12.txt\r" expect "]*" send "echo 1212 > /tmp/12.txt\r" expect "]*" send "exit\r"
|
1 2 3
| chmod 744 2.expect
./2.expect
|
expect脚本传递参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| vi 3.expect
增加如下脚本内容:
#!/usr/bin/expect
//把第一个参数的值赋给user set user [lindex $argv 0] set host [lindex $argv 1] set passwd "123456" set cm [lindex $argv 2] spawn ssh $user@$host
expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect "]*" send "$cm\r" //-1表示永远不超时,1表示1秒,2表示2秒,以此类推,表示执行命令几秒后停止 set timeout -1 expect "]*" send "exit\r"
|
1 2 3
| chmod 744 2.expect
./2.expect
|
实践项目:构建一个简易的分发系统?
需求描述:
假设有一个大公司,企业网站用PHP编写,在多台服务同时跑服务,如何将网站更新代码同步推送到各个服务器?
解决思路:
利用Shell和Expect,就能构建一个简单的分发系统,首先,准备好一台模板机器,将需要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
核心命令:
1
| rsync -av --files-from=list.txt / root@host:/
|
实现步骤:
step1:编写一个脚本文件rsync.expect
1 2 3 4 5 6 7 8 9 10 11 12 13
| vim rsync.expect #!/usr/bin/expect # 目标机器的登录密码 set passwd "123456" set host [lindex $argv 0] set file [lindex $argv 1] # 核心命令,同步多个文件 spawn rsync -avR --files-from=$file / root@$host:/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof
|
更改脚本权限
chmod 744 rsync.expect
step2:编辑一个文本文件,用来放需要同步的文件列表
1 2 3 4 5
| vim /tmp/list.txt /tmp/12.txt /root/test.sh /tmp/test.txt /root/expectFiles/hostFile.expect
|
step3: 编辑一个ip.list,用于存放需要同步的目标机器的IP地址
1 2 3
| vim /tmp/ip.list 192.16.155.128 192.16.155.132
|
step4:编写shell脚本rsync.sh,执行同步操作
1 2 3 4 5 6 7 8 9 10
| vim rsync.sh
#!/bin/bash for ip in `cat /tmp/ip.list` do echo $ip # 第二个参数就是需要同步的文件列表 ./rsync.expect $ip /tmp/list.txt done
|
本地执行同步:
远程执行同步
编写脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| vim exe.expect #!/usr/bin/expect set host [lindex $argv 0] set passwd "123456" set cm [lindex $argv 1] spawn ssh root@$host expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect "]*" send "$cm\r" expect "]*" send "exit\r"
|
更改权限
需要写shell脚本exe.sh,用于执行操作
1 2 3 4 5 6 7 8
| vim exe.sh #!/bin/bash for ip in `cat /tmp/ip.list` do echo $ip # 第二个参数就是需要执行的命令 ./exe.expect $ip "who;ls" done
|
执行脚本
expect脚本同步文件
1 2 3 4 5 6 7 8 9 10 11 12
| vi 4.expect
增加如下脚本内容:
#!/usr/bin/expect set passwd "123456" spawn rsync -av root@172.16.111.110:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof
|
1 2 3
| chmod a+x 4.expect
./4.expect
|
expect脚本指定host和要同步的文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| vi 5.expect
增加如下内容:
#!/usr/bin/expect set passwd "123456" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -av $file root@$host:$file expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof
[root@garytao-01 shell]# cat 5.expect #!/usr/bin/expect set passwd "123456" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -av $file root@$host:$file expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof
|
1 2 3
| chmod a+x 5.expect
./5.expect
|