笔记内容:java
笔记日期:2017-11-29mysql
<br>web
所谓分发系统就是一个主要用来上线代码或同步文件的脚本,先来看一下需求背景:sql
因此分发系统就是用来完成以上这个需求的,分发系统须要完成的事情就是将须要上线的代码分发到这些线上服务器中。咱们如今要作的就是实现这个分发系统,实现这个系统须要用到两个主要的东西就是shell和expect,经过shell结合expect能够编写一个简单的分发系统。shell
<br>vim
文件分发系统的实现:
1.使用expect编写一个脚本文件rsync.expect,这个脚本是实现文件同步的脚本,内容以下:后端
[root@localhost ~/expectFiles]# 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 [root@localhost ~/expectFiles]# chmod a+x rsync.expect
2.而后再编辑一个文本文件,这个文件用来放须要同步的文件列表,例如我这里随便同步几个文件:bash
[root@localhost ~/expectFiles]# vim /tmp/list.txt /tmp/12.txt /root/test.sh /tmp/test.txt /root/expectFiles/hostFile.expect
提示:若是你的rsync命令没有加-R选项的话,就须要确保目标机器也有这个文件列表中所定义的目录路径,否则就会报错。服务器
3.还须要编辑一个ip.list,用于存放须要同步的目标机器的IP地址,例如我须要将文件都同步这几个IP的机器上:ssh
[root@localhost ~/expectFiles]# vim /tmp/ip.list 192.168.77.128 192.168.77.130
须要同步的目标机器的密码最好是一致的,由于只是作实验为了简单化就不使用密钥验证了。
4.再编写一个shell脚本rsync.sh,这个脚本比较简单,只是遍历出ip.list文件内容而后交给rsync.expect脚本去执行而已,示例:
[root@localhost ~/expectFiles]# vim rsync.sh #!/bin/bash for ip in `cat /tmp/ip.list` do echo $ip # 第二个参数就是须要同步的文件列表 ./rsync.expect $ip /tmp/list.txt done
最后咱们只须要执行rsync.sh脚本便可:
[root@localhost ~/expectFiles]# sh rsync.sh 192.168.77.128 spawn rsync -avR --files-from=/tmp/list.txt / root@192.168.77.128:/ root@192.168.77.128's password: building file list ... done root/ root/test.sh root/expectFiles/ root/expectFiles/hostFile.expect tmp/ tmp/12.txt tmp/test.txt sent 666 bytes received 97 bytes 1526.00 bytes/sec total size is 288 speedup is 0.38 192.168.77.130 spawn rsync -avR --files-from=/tmp/list.txt / root@192.168.77.130.83:/ root@192.168.77.130's password: building file list ... done root/ root/test.sh root/expectFiles/ root/expectFiles/hostFile.expect tmp/ tmp/12.txt tmp/test.txt sent 666 bytes received 97 bytes 1526.00 bytes/sec total size is 288 speedup is 0.38 [root@localhost ~/expectFiles]#
运行结果如上,没有报错,文件也正常同步了,这样咱们就实现了一个很简单的文件分发系统。
<br>
以上咱们已经实现了简单的文件分发系统,可是光只能同步文件还不够。由于假设这是网站的后端代码文件,同步完了以后须要重启web服务或者执行一些命令,因此还得再编写一个可以批量远程执行命令的脚本。
1.一样的先使用expect编写远程登陆的脚本文件exe.expect, 内容以下:
[root@localhost ~/expectFiles]# 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" [root@localhost ~/expectFiles]# chmod a+x exe.expect
2.除此以外还须要写一个shell脚本exe.sh,这个脚本和rsync.sh差很少都是遍历出ip.list文件里的ip,内容以下:
[root@localhost ~/expectFiles]# vim exe.sh #!/bin/bash for ip in `cat /tmp/ip.list` do echo $ip # 第二个参数就是须要执行的命令 ./exe.expect $ip "who;ls" done
执行exe.sh脚本:
[root@localhost ~/expectFiles]# sh exe.sh 192.168.77.128 spawn ssh root@192.168.77.128 root@192.168.77.128's password: Last login: Wed Nov 29 21:56:41 2017 from 192.168.77.130 [root@localhost ~]# who;ls root pts/0 2017-11-29 21:26 (192.168.77.1) root pts/1 2017-11-29 21:57 (192.168.77.130) \ mysql57-community-release-el7-8.noarch.rpm Test.java anaconda-ks.cfg stop.sh test.sh expectFiles Test.class zabbix-release-3.2-1.el7.noarch.rpm [root@localhost ~]# 192.168.77.130 spawn ssh root@192.168.77.130 root@192.168.77.130's password: Last login: Thu Nov 30 05:49:18 2017 from 192.168.77.130 [root@localhost ~]# who;ls root tty1 2017-11-30 05:23 root pts/0 2017-11-30 03:33 (192.168.77.1) root pts/2 2017-11-30 05:50 (192.168.77.130) accept_local~ expectFiles grep logs sed shellFile Test test.sh [root@localhost ~]# [root@localhost ~/expectFiles]#
运行结果如上,没毛病。
done:至此简易的分发系统和批量远程执行命令的功能就完成了。