20.27 分发系统介绍
分发系统使用场景,把每天更新的代码分发到系统上去。
上线:把开发的代码发布到线上去。
分发系统:
1、准备一台模板机器,上面有要发布的代码。
2、线上服务器IP、用户名密码,使用expect和rsync把代码推送到服务器上去。
20.27 分发系统介绍
安装expect工具:
yum install -y expect
1、自动远程登录,并执行命令。
#!/usr/bin/expect #定义变量:host、passwd set host "192.168.37.200" set passwd "123456a" #ssh登录服务器 spawn ssh root@$host #expect语句,遇到提示时之后的做法,\r回车符,exp_continue继续匹配。 expect { "yes/no" {send "yes\r";exp_continue} "assword:" {send "$passwd\r"} } #结束执行脚本,保留在登录的机器 interact
expect和{}之间要有空格。
一台机器在首次登录另一台服务器时,会有yes/no的提示,来提示操作者,此设备在本机上没有登录记录,是否要继续登录。
如果登录过了,会在/root/.ssh/known_hosts有相关的记录。
[root@liang-00 sbin]# cat /root/.ssh/known_hosts 192.168.37.203 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBD4ibyI009HUAAVJrqpcbr3obtgnD3CHhjqx5cvnTH2AnVejuhgp8/aflDO3xvywbpSSqD2cu9rePlcwfsu4wyI= [root@liang-00 sbin]#
执行expect脚本:
[root@liang-00 sbin]# > /root/.ssh/known_hosts [root@liang-00 sbin]# 1.expect spawn ssh root@192.168.37.203 The authenticity of host '192.168.37.203 (192.168.37.203)' can't be established. ECDSA key fingerprint is SHA256:8inulIfa68P+cyhMNY+ezu+ViedRNFf+JJQpWxwN2yE. ECDSA key fingerprint is MD5:80:11:c0:45:72:bb:74:7c:ac:ea:49:0c:1b:e7:6c:dd. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.37.203' (ECDSA) to the list of known hosts. Last login: Sat Jan 12 10:15:46 2019 from 192.168.37.200 [root@liang-03 ~]#
20.29 expect脚本远程执行命令
1、自动远程登录后,执行命令并退出。
#!/usr/bin/expect set user "root" set passwd "123456a" spawn ssh $user@192.168.37.203 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 "]8" #退出系统 send "exit\r"
20.30 expect脚本传递参数
1、expect eof 语句在登录机器上等待程序执完命令再退出。
2、传递参数
#!/usr/bin/expect set user [lindex $argv 0] set host [lindex $argv 1] set passwd "123456a" set cm [lindex $argv 2] spawn ssh $user@$host expect { "yes/no" {send "yes\r"; exp_continue "password:" {send "$passwd\r"} } expect "]*" send "$cm\r" expect "]*" send "exit\r"
set user [lindex $argv 0] 传递给user第一个参数;
set host [lindex $argv 1] 传递给host第二个参数;
set cm [lindex $argv 2] 传递给cm第三个参数。
执行3.expect脚本:
[root@liang-00 sbin]#chmod a+x 3.expect [root@liang-00 sbin]# 3.expect root 192.168.37.203 ls spawn ssh root@192.168.37.203 Last login: Sat Jan 12 10:39:49 2019 from 192.168.37.200 [root@liang-03 ~]# ls anaconda-ks.cfg zabbix-release-3.2-1.el7.noarch.rpm [root@liang-03 ~]# [root@liang-00 sbin]#
在第三个参数中传递多个命令:
[root@liang-00 sbin]# 3.expect root 192.168.37.203 "ls;w"
spawn ssh root@192.168.37.203
Last login: Sat Jan 12 10:52:05 2019 from 192.168.37.200
[root@liang-03 ~]# ls;w
anaconda-ks.cfg zabbix-release-3.2-1.el7.noarch.rpm
10:55:00 up 56 min, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.37.1 09:59 18:52 0.15s 0.15s -bash
root pts/1 192.168.37.200 10:54 4.00s 0.04s 0.00s w
[root@liang-03 ~]# [root@liang-00 sbin]#
20.31 expect脚本同步文件
1、自动同步文件。
#!/usr/bin/expect set passwd "123456a" spawn rsync -av root@192.168.37.203:/tmp/12.txt /tmp/ expect { "yes/no" {send "yes\r" } "password:" {send "$passwd\r"} } #set timeout 10 expect eof #interact
执行脚本:
[root@liang-00 expect_dir]# ./4.expect spawn rsync -av root@192.168.37.203:/tmp/12.txt /tmp/ receiving incremental file list 12.txt sent 43 bytes received 97 bytes 280.00 bytes/sec total size is 5 speedup is 0.04 expect: spawn id exp6 not open while executing "expect eof " (file "./4.expect" line 10)
expect eof 等待命令执行完成在退出,如果没有此命令的话,登录后会直接退出,无法完成要执行的命令。
20.32 expect脚本指定host和要同步的文件
1、在expect中可以设置set timeout 数字,来指定超时时间,永远不超时用“-1”。
2、指定host和同步的文件。
#!/usr/bin/expect set passwd "123456a" 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@liang-00 expect_dir]# ./5.expect 192.168.37.203 /tmp/12.txt spawn rsync -av /tmp/12.txt root@192.168.37.203:/tmp/12.txt sending incremental file list sent 45 bytes received 12 bytes 38.00 bytes/sec total size is 5 speedup is 0.09 expect: spawn id exp6 not open while executing "expect eof" (file "./5.expect" line 12) [root@liang-00 expect_dir]#
20.33 构建文件分发系统
需求背景:
对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
实现思路:
首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
核心命令:rsync -av --files-from=list.txt / root@host:/
文件分发系统的实现。
1、rysnc.expect文件,实现文件的分发。
#!/usr/bin/expect set passwd "123456a" 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
2、文件列表文件。
[root@liang-00 expect_dir]# cat /tmp/list.txt /tmp/12.txt /root/grep/2.txt [root@liang-00 expect_dir]#
3、根据循环IP分发服务器的IP列表文件。
[root@liang-00 expect_dir]# vi /tmp/ip.txt [root@liang-00 expect_dir]# cat !$ cat /tmp/ip.txt 192.168.37.203 127.0.0.1 [root@liang-00 expect_dir]#
4、rsync.sh 实现分发的shell脚本。
[root@liang-00 expect_dir]# sh rsync.sh 192.168.37.203 spawn rsync -avR --files-from=/tmp/list.txt / root@192.168.37.203:/ building file list ... done root/ root/grep/ root/grep/2.txt tmp/ tmp/12.txt sent 252 bytes received 63 bytes 630.00 bytes/sec total size is 14 speedup is 0.04 expect: spawn id exp6 not open while executing "expect eof" (file "./rsync.expect" line 12) 127.0.0.1 spawn rsync -avR --files-from=/tmp/list.txt / root@127.0.0.1:/ root@127.0.0.1's password: Permission denied, please try again. root@127.0.0.1's password: [root@liang-00 expect_dir]# [root@liang-00 expect_dir]# [root@liang-00 expect_dir]# ll /root/grep/2.txt -rw-r--r-- 1 root root 9 Jan 12 12:14 /root/grep/2.txt [root@liang-00 expect_dir]#
同步的服务器上文件:
[root@liang-03 ~]# ll /root/grep/2.txt -rw-r--r-- 1 root root 9 Jan 12 12:14 /root/grep/2.txt [root@liang-03 ~]#
20.34 批量远程执行命令
在多台服务器中批量执行命令。
1、exe.expect文件,实现远程执行命令
#!/usr/bin/expect set passwd "123456a" set host [lindex $argv 0] 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"
2、循环IP实现的shell脚本
[root@liang-00 expect_dir]# cat exe.sh #!/bin/bash for ip in `cat /tmp/ip.txt` do echo $ip ./exe.expect $ip hostname done [root@liang-00 expect_dir]#
3、执行脚本:
[root@liang-00 expect_dir]# chmod a+x exe.expect [root@liang-00 expect_dir]# sh exe.sh 192.168.37.203 spawn ssh root@192.168.37.203 Last login: Sat Jan 12 10:54:50 2019 from 192.168.37.200 [root@liang-03 ~]# hostname liang-03 [root@liang-03 ~]# 127.0.0.1 spawn ssh root@127.0.0.1 root@127.0.0.1's password: Last failed login: Sat Jan 12 12:28:58 CST 2019 from localhost on ssh:notty There were 8 failed login attempts since the last successful login. Last login: Sat Jan 12 10:32:42 2019 from 192.168.37.1 抵住诱惑,坚持不懈! 破釜沉舟,志在必得! [root@liang-00 ~]# hostname liang-00 [root@liang-00 ~]# [root@liang-00 expect_dir]#
扩展:
shell多线程:http://blog.lishiming.net/?p=448
shell习题:http://www.apelearn.com/study_v2/chapter15.html#shll