20.27 分发系统介绍php
20.28 expect脚本远程登陆linux
20.29 expect脚本远程执行命令shell
20.30 expect脚本传递参数编程
20.27 分发系统介绍:vim
场景:业务愈来愈大。用的好比app,后端(也就是服务器)所使用的编程语言为php。那要想运行这个php的代码,就要搞一个LAMP或者LNMP的环境。最后还须要把大家的代码上传达到服务器上去(说白了就是作一个网站嘛)后端
可是在平时工做中业务不断在迭代,有新的功能出现,那这时候就要去改代码。几台机器的话还好,要是几十台的话,就要搞一个分发系统可以把天天或者每一段时间更新的代码,分别的发布到这几十台机器上去。bash
固然还会有开源的一些工具能够帮咱们作到。可是这里咱们要用shell编程可以实现的一种上线的工具。所谓的分发系统也就是上线的shell脚本。核心叫作expect,expect也能够说是一种脚本语言(和shell很是像),咱们能够用它可以实现传输文件,还能够实现远程执行命令,不须要咱们去输入密码。说着这可能会想到用ftp或xftp以及lrzsz上传。之后会讲到很是标准的上线的体系服务器
在这里要准备一台模板的机器,这台机器的代码是最新的代码,是准备要上线的代码。而后给要上线的这几十台机器的IP要知道,还有这几十台机器对应用户的密码。以后就是使用expect借助rsync把这些代码推送到这五十台机器上去。计入须要执行一些命令,还能够用expect登陆进去去执行这些命令。就是这样的一个过程app
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ssh
20.28 expect脚本远程登陆:
~1.yum install -y expect
~2.自动远程登陆
#! /usr/bin/expect
set host "192.168.133.132" #定义host的变量。expect里面定义变量的格式就是这样写的。注意前面要加set
set passwd "123456"
spawn ssh root@$host #登陆机器的语句
expect { #expect核心语句
"yes/no" { send "yes\r"; exp_continue} #至关于用户交互。咱们初次登陆另外一台机器的时候会提示一些。这个就是针对这些提示作出的判断
"assword:" { send "$passwd\r" }
}
interact #结束。
最后结束语是interact:表示可是须要停留在远程的机器上,不须要退出
最后结束语是expect eof:表示登陆后停留一两秒后退出
若是什么都不加: 登陆后立刻退出来
实例:
[root@axinlinux-01 ~]# cd /usr/local/sbin/
[root@axinlinux-01 sbin]# vim 1.expect
#! /usr/bin/expect
set host "192.168.208.130"
set passwd "wangxin789"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact
[root@axinlinux-01 sbin]# ./1.expect #这样执行这个脚本
-bash: ./1.expect: 权限不够
[root@axinlinux-01 sbin]# chmod a+x 1.expect #加上可执行的权限。不然会报权限不够
[root@axinlinux-01 sbin]# ./1.expect #再次执行成功
如下,就是咱们脚本中,作出的一些判断与交互。至关于以脚本的方式登陆了
spawn ssh root@192.168.208.130
The authenticity of host '192.168.208.130 (192.168.208.130)' can't be established.
ECDSA key fingerprint is SHA256:2YEHWSxuaj+NF8PI1ipI8BeYOqoajfpRICmS59xgQEw.
ECDSA key fingerprint is MD5:3e:75:16:b7:8e:40:10:0f:f3:e9:79:34:48:69:2a:e4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.208.130' (ECDSA) to the list of known hosts.
root@192.168.208.130's password:
Last login: Fri Sep 21 21:25:28 2018 from 192.168.208.1
[root@axinlinux-02 ~]# #成功登陆
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20.29 expect脚本远程执行命令:
~1.自动远程登陆后,执行命令并退出
~2.#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh $user@192.168.133.132
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*" #就是咱们在输入命令时前面的主机名[root@axinlinux-01]#,由于root的话是#,其余用户是$。因此这里用了*,无论匹配的是哪一个
send "touch /tmp/12.txt\r"
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r" #最后的命令exit回车。天然脚本也就结束了
实例:
[root@axinlinux-01 sbin]# vim 2.expect
#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh $user@192.168.133.132
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"
[root@axinlinux-01 sbin]# chmod a+x 2.expect #同样设置x权限
[root@axinlinux-01 sbin]# ./2.expect #运行它
spawn ssh root@192.168.208.130
root@192.168.208.130's password:
Last login: Fri Sep 21 21:49:27 2018 from 192.168.208.128
[root@axinlinux-02 ~]# touch /tmp/12.txt #能够看到在02的机器上运行了脚本里的命令
[root@axinlinux-02 ~]# echo 1212 > /tmp/12.txt
[root@axinlinux-02 ~]# [root@axinlinux-01 sbin]# #最后又回到了01上
[root@axinlinux-02 ~]# cat /tmp/12.txt #检查一下是否有
1212
[root@axinlinux-02 ~]# ls -l !$
ls -l /tmp/12.txt
-rw-r--r-- 1 root root 5 9月 21 22:04 /tmp/12.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20.30 expect脚本传递参数:
传递参数(像shell有$1$2。也至关因而他的内置变量)。若是想执行多个命令,就传递多个参数
#!/usr/bin/expect
set user [lindex $argv 0] #这是他的第一个参数(把这个参数的值赋给user,也就是咱们登陆时要输入的)。用方括号括起来的就是user。这也是他的格式
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@axinlinux-01 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@axinlinux-01 sbin]# ./3.expect root 192.168.208.130 ls #咱们输入这三个参数。用户名 目标IP 要执行的命令
spawn ssh root@192.168.208.130
root@192.168.208.130's password:
Last login: Fri Sep 21 22:22:39 2018 from 192.168.208.1
[root@axinlinux-02 ~]# ls #能够看到进去后执行了这个命令
aaa.txt anaconda-ks.cfg shell zabbix-release-3.2-1.el7.noarch.rpm
[root@axinlinux-02 ~]# [root@axinlinux-01 sbin]#
[root@axinlinux-02 ~]# [root@axinlinux-01 sbin]# ./3.expect root 192.168.208.130 "ls;w;vmstat 1"
#以上,若是想进去执行多个命令,就传递多个参数,用分号间隔,而且用双引号引发来(做为一个参数传递)
spawn ssh root@192.168.208.130
root@192.168.208.130's password:
Last login: Fri Sep 21 22:26:20 2018 from 192.168.208.128
[root@axinlinux-02 ~]# ls;w;vmstat 1 #能够看到咱们传递参数的三个命令
aaa.txt anaconda-ks.cfg shell zabbix-release-3.2-1.el7.noarch.rpm
22:28:52 up 1:05, 3 users, load average: 0.01, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 21:24 1:04m 0.05s 0.05s -bash
root pts/0 192.168.208.1 22:22 6:13 0.02s 0.02s -bash
root pts/1 192.168.208.128 22:28 0.00s 0.01s 0.00s w
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 1113344 2076 189972 0 0 21 2 67 65 0 0 99 0 0
0 0 0 1113168 2076 190060 0 0 0 0 138 123 0 1 99 0 0
0 0 0 1113160 2076 190068 0 0 0 0 128 112 0 0 99 0 0
0 0 0 1113128 2076 190104 0 0 0 0 141 131 0 1 99 0 0
0 0 0 1113300 2076 190064 0 0 0 0 167 137 1 0 99 0 0
0 0 0 1113176 2076 190064 0 0 0 0 144 125 0 1 100 0 0
0 0 0 1113052 2076 190064 0 0 0 0 136 117 0 1 99 0 0
0 0 0 1113052 2076 190064 0 0 0 0 100 100 0 0 100 0 0
0 0 0 1113052 2076 190064 0 0 0 0 100 98 0 0 100 0 0
1 0 0 1113176 2076 190068 0 0 0 0 127 116 0 0 100 0 0
#由于vmstat不能自动中止,可是会有超时时间,因此会自动退出来
[root@axinlinux-01 sbin]#