分发系统介绍
- 分发系统-expect讲解(也就是一个分发的脚本)
场景:
- 业务越来越大,网站app,后端,编程语言是php,所以就需要配置lamp或者lnmp,最好还需要吧代码上传到服务器上;但是因为业务增加,代码增加,多台机器,就会非常麻烦;这是只需要一个分发系统,就可以把每次更新的代码发布到需要更新的服务器上
- expect,是一种脚本语言;通过他可以实现传输,输入命令(上线代码)
- 首先要准备一台模板机器,机器的IP,对应用户的密码,通过rsync同步代码,还可以通过expect去执行某些命令
expect脚本远程登录
- yum install -y expect
- 自动远程登录 vim 1.expect
[root@aminglinux sbin]# vim 1.expect #!/usr/bin/expect //使用expect工具执行脚本 set host "192.168.222.111" //设置变量 ip set passwd "123456" //设置密码 spawn ssh@$host //远程登录 expect{ //与系统交互 "yes/no" {send "yes\r"; exp_continue } //检测到关键字是,发送指令并回车 "password: " {send "$passwd\r"} //发送密码回车 } interact //结束脚本,保持登录远程状态
-
在expect 定义变量,用set
-
这个文件是就保证登录信息的,清空的话,重新远程登录ssh 会有提示 /root/.ssh/known_hosts
-
exp_continue 表示继续 \r 表示换行 interact 继续停留在这台机器,不退出
-
加入执行权限
[root@aminglinux sbin]# chmod a+x 1.expect [root@aminglinux sbin]# ll 1.expect -rwxr-xr-x 1 root root 203 7月 31 22:20 1.expect
- 登录
[root@aminglinux sbin]# ./1.expect spawn ssh root@192.168.222.111 root@192.168.222.111's password: Last login: Tue Jul 31 22:12:22 2018 from 192.168.222.1
expect脚本远程执行命令
- 退出远程登录后,执行命令并退出 vim 2.expect
[root@aminglinux sbin]# vim 2.expect #!/usr/bin/expect set user "root" set passwd "123456" spawn ssh $user@192.168.222.111 expect { "yes/no" { send "yes\r"; exp_continue} "password:" { send "$passwd\r" } } expect "]*" //遇到shell命令输入提示符,则发送指令,*号是通配,因为root用户是#,普通用户是$ send "touch /tmp/12.txt\r" //发送命令并回车执行 expect "]*" send "echo 1212 > /tmp/12.txt\r" expect "]*" send "exit\r"
-
expect "]" *“” 通配]右边所有字符;send 执行命令
-
加入执行权限,执行
[root@aminglinux sbin]# chmod a+x 2.expect [root@aminglinux sbin]# ./2.expect spawn ssh root@192.168.222.111 root@192.168.222.111's password: Last login: Tue Jul 31 22:25:47 2018 from 192.168.222.110 [root@localhost ~]# touch /tmp/12.txt [root@localhost ~]# echo 1212 > /tmp/12.txt
- B机器查看:
[root@aminglinux sbin]# ./1.expect spawn ssh root@192.168.222.111 root@192.168.222.111's password: Last login: Tue Jul 31 22:36:17 2018 from 192.168.222.110 [root@localhost ~]# ll /tmp/12.txt -rw-r--r--. 1 root root 5 7月 31 22:32 /tmp/12.txt [root@localhost ~]# cat /tmp/12.txt 1212
expect脚本传递参数
- 传递参数 vim 3.expect
[root@aminglinux sbin]# vim 3.expect #!/usr/bin/expect 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" expect "]*" send "exit\r"
- 加入执行权限
[root@aminglinux sbin]# chmod a+x 3.expect
- ./3.expect [第一个参数] [第二个参数] [第三个参数]
[root@aminglinux sbin]#[root@aminglinux sbin]# ./3.expect root 192.168.222.111 ls
支持多条参数
[root@aminglinux sbin]#[root@aminglinux sbin]# ./3.expect root 192.168.222.111 "ls;w"
expect脚本同步文件
- 在一台机器上把文件同步到多台机器上
- 自动同步文件 vim 4.expect
[root@aminglinux sbin]# vim 4.expect #!/usr/bin/expect set passwd "123456" spawn rsync -av root@192.168.222.111:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send $passwd\r} } expect eof
- 加入执行权限,执行
[root@aminglinux sbin]# chmod a+x 4.expect [root@aminglinux sbin]# ./4.expect spawn rsync -av root@192.168.222.111:/tmp/12.txt /tmp/ root@192.168.222.111's password: receiving incremental file list 12.txt sent 43 bytes received 96 bytes 92.67 bytes/sec total size is 5 speedup is 0.04
- expect eof :只有spawn执行的命令结果才会被expect捕捉到,因为spawn会启动一个进程,只有这个进程的相关信息才会被捕捉到,主要包括:标准输入的提示信息,eof和timeout。
expect脚本指定host和要同步的文件
- set timeout 定义超时时间(单位为 秒) -1 为永远不超时
- 指定host和要同步的文件 vim 5.expect
[root@aminglinux sbin]# vim 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
-
变量定义的文件地址,使用时,必须写绝对路径
-
加入执行权限,执行本机文件同步到远程机器上去: ./5.expect 192.168.222.111 "/tmp/12.txt"
[root@aminglinux sbin]# chmod a+x 5.expect [root@aminglinux sbin]# ./5.expect 192.168.222.111 "/tmp/12.txt"
构建文件分发系统
需求背景:
- 对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
实现思路
- 首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
核心命令
-
rsync -av --files-from=list.txt / root@host:/
-
使用rsync 的 --files参数,可以实现调用文件里面的列表,进行多个文件远程传输,进而实现文件分发
-
文件分发系统的实现
-
rsync.expect 内容
[root@aminglinux sbin]# 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
-
同步的路径,需要保证对方机器也有这个相同的路径,如果没有路径,需要使用 -R 创建路径
-
因为实现分发系统,肯定是因为需要分发的机器数量过大,所以,定义好了 文件 的 list 列表文件以后, 还需要配置 ip 的列表文件
-
创建需要同步文件的列表文件
-
创建需要同步IP地址的列表文件
-
rsync.sh 内容
[root@aminglinux sbin]# vim rsync.sh #!/bin/bash for ip in `cat /tmp/ip.list` do echo $ip ./rsync.expect $ip /tmp/file.list done
- 这个sh 的目的,就是遍历一下 ip列表文件中的 ip地址
- expect脚本 必须加入执行权限
- 分发系统还有一个重要的关键是,确保同步的机器的密码一致,否则将不能实现同步;所以这就存在一个弊端,一旦脚本暴露,将会让别人知道如何登陆你机器;当然也有对应的解决办法,那就是使用密钥认证,这样的话,自然在命令行业省去“输入密码< password:" { send "$passwd\r" } >''”和“定义密码< set passwd "123123a" >”的命令了
批量远程执行命令
- exe.expect 内容
[root@aminglinux sbin]# 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"
- exe.sh 内容
[root@aminglinux sbin]# vim exe.sh #!/bin/bash for ip in `cat ip.list` do echo $ip ./exe.expect $ip "w;free -m;ls /tmp" done
-
也可以执行多个命令:
./exe.expect $ip "w;free -m;ls /tmp/" //命令之间使用分号;间隔