20.27 分发系统介绍
需求:
服务端使用的编程语言为php,想运行php的代码就必须配置LAMP或LNMP环境;
需要将代码上传到服务器上;
平时工作中代码在不断的迭代,可能有新的功能出现,需要修改代码;
提供服务的机器数量庞大,逐一更新服务器上的代码较困难;
部分开源软件可以帮助用户分发代码到服务器
Shell编程实现分发:
expect是一种类似shell的脚本语言,可以实现传输文件和远程执行命令等;
不需要输入密码;
上线:将开发人员开发的代码发布到线上环境上;
上线前准备:
准备一台模板机器,该机器上的代码是最新修改,准备用于上线的代码;
知道所有需要上线机器的ip地址和对应用户的账号、密码;
使用expect脚本,借助rsync将代码推送到需要上线的机器上;
假如需要执行一些命令,可以借助expect登录机器执行命令;
20.28 expect脚本远程登录
安装expect:
[root@hyc-01-01 ~]# yum install -y expect
使用expect编写脚本,实现自动远程登录并执行命令:
编辑expect脚本:
[root@hyc-01-01 ~]# cd /usr/local/sbin
[root@hyc-01-01 sbin]# vim 1.expect
#! /usr/bin/expect
set host "192.168.31.128"
set passwd "hyc940421"
expect中定义变量方法为set 变量名
spawn ssh root@$host
登录机器的语句
expect {
"yes/no" { send "yes\r"; exp_continue}
当输出的语句含有yes/no时,发送yes,\r表示回车,exp_continue表示继续
"assword:" { send "$passwd\r" }
}
Interact
Interact表示需要停留在远程机器上而不需要退出;
如果脚本中interact的一行不写任何内容则脚本执行结束会马上退出;
如果脚本中interact的一行内容为expect eof则会在登录机器上停顿2秒后再退出;
若脚本末尾出现这样的代码则会马上退出:
expect "]*"
send "exit\r"
执行expect脚本:
[root@hyc-01-01 sbin]# ./1.expect
spawn ssh root@192.168.31.128
The authenticity of host '192.168.31.128 (192.168.31.128)' can't be established.
ECDSA key fingerprint is SHA256:0SErfGbbc3AfFcxC92Tav9X/T/bOn8wfnvum/wnw5Xs.
ECDSA key fingerprint is MD5:b7:d4:e4:4a:4a:33:29:99:1a:2e:45:94:d9:40:17:fb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.31.128' (ECDSA) to the list of known hosts.
root@192.168.31.128's password:
Last login: Wed Oct 3 11:05:39 2018 from 192.168.31.1
[root@hyc-01 ~]#
登录机器成功
20.29 expect脚本远程执行命令
远程登录,执行命令并退出:
[root@hyc-01-01 sbin]# vim 1.expect
#!/usr/bin/expect
set host "192.168.31.128"
set passwd "hyc940421"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
expect "]*"
当匹配到对应字符时执行以下命令
当遇到”]”后跟任意字符时执行下面的命令,这里*表示通配
这里若登录的是root则*位置为#,若为普通用户则为$
send "touch /tmp/12.txt\r"
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"
hyc-01-01上:
[root@hyc-01-01 sbin]# ./1.expect
spawn ssh root@192.168.31.128
root@192.168.31.128's password:
Last login: Wed Oct 3 11:22:16 2018 from 192.168.31.129
[root@hyc-01 ~]# touch /tmp/12.txt
[root@hyc-01 ~]# echo 1212 > /tmp/12.txt
[root@hyc-01 ~]#
[root@hyc-01-01 sbin]#
hyc-01上:
[root@hyc-01 tmp]# ls 12.txt
12.txt
[root@hyc-01 tmp]# cat !$
cat 12.txt
1212
20.30 expect脚本传递参数
[root@hyc-01-01 sbin]# vim 111.expect
#!/usr/bin/expect
set user [lindex $argv 0]
表示将第一个参数传递给变量user
set host [lindex $argv 1]
第二个变量传递给变量host
set passwd "hyc940421"
set cm [lindex $argv 2]
第三个变量传递给cm
spawn ssh $user@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
[root@hyc-01-01 sbin]# ./111.expect root 192.168.31.128 ls
spawn ssh root@192.168.31.128
root@192.168.31.128's password:
Last login: Wed Oct 3 22:41:05 2018 from 192.168.31.129
[root@hyc-01 ~]# ls
anaconda-ks.cfg hyc.txt root.txt WebIOL.7z zabbix-release-3.2-1.el7.noarch.rpm
[root@hyc-01 ~]# [root@hyc-01-01 sbin]#
[root@hyc-01-01 sbin]# ./111.expect root 192.168.31.128 "ls;cd /tmp;vmstat 1 20"
spawn ssh root@192.168.31.128
root@192.168.31.128's password:
Last login: Wed Oct 3 22:59:27 2018 from 192.168.31.129
[root@hyc-01 ~]# ls;cd /tmp;vmstat 1 20
anaconda-ks.cfg hyc.txt root.txt WebIOL.7z zabbix-release-3.2-1.el7.noarch.rpm
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
3 0 4788 77448 0 349556 0 0 3 98 110 131 0 0 99 0 0
0 0 4788 77468 0 349572 0 0 0 8 127 153 0 1 99 0 0
0 0 4788 77500 0 349572 0 0 0 6 110 130 0 0 100 0 0
0 0 4788 77500 0 349572 0 0 0 932 145 152 0 1 99 0 0
0 0 4788 77500 0 349572 0 0 0 5 114 131 0 0 99 1 0
0 0 4788 77500 0 349572 0 0 0 5 125 143 0 0 100 0 0
0 0 4788 77500 0 349572 0 0 0 6 115 134 0 1 99 0 0
0 0 4788 77500 0 349572 0 0 0 6 114 138 0 0 100 0 0
0 0 4788 76972 0 349576 0 0 4 10 171 158 1 1 98 0 0
0 0 4788 77004 0 349576 0 0 0 212 158 153 0 2 98 0 0
当用户要在expect中将多个命令作为一个参数传递时可以将参数写成"ls;cd /tmp;vmstat 1 20";
这里不适合执行vmstat 1 20这种需要长时间执行的命令,若执行该命令则至少需要在终端停留20s,但vmstat只执行了10s就被强制退出终端,在expect中存在超时时间(大约10s),不适合运行需要长时间执行的命令;