• 20.27 分发系统介绍
  • 20.28 expect脚本远程登录
  • 20.29 expect脚本远程执行命令
  • 20.30 expect脚本传递参数

    20.27 分发系统介绍

  • 分发系统,什么叫分发系统,首先说一个场景,以后在工作中会遇到的场景
  • 场景:业务越来越大,网站app,后端,编程语言是php,所以就需要配置lamp或者lnmp,最后还需要把你们的代码上传到服务器上;但是在平时工作中,因为业务增加,代码增加,一台机器还好点,甚至在服务器上直接改,但是这样不规范,多台机器,就会非常麻烦;这是只需要一个分发系统,就可以把每次更新的代码发布到需要更新的服务器上去。
  • 核心的东西是expect,是一种脚本语言;通过他可以实现传输文件,还可以实现远程输入命令(上线代码),不需要输入密码
  • 首先要准备一台模板机器这台机器上代码是最新的代码,上线的代码,机器的IP,对应用户的密码,通过rsync同步代码,还可以通过expect去执行某些命令,这样的一个过程

20.28 expect脚本远程登录

  • 首先检查下机器里面有没有安装expect
  • 如果没有需要安装下,因为当中有epel源(国外源,所以下载会很慢)解决办法:修改epel.repo文件名,改为epel.repo1 再yum install -y expect 即可。
    [root@aming-01 sbin]# cd /etc/yum.repos.d/
    [root@aming-01 yum.repos.d]# ls
    CentOS-Base.repo       CentOS-fasttrack.repo  CentOS-Vault.repo  zabbix.repo
    CentOS-CR.repo         CentOS-Media.repo      epel.repo
    CentOS-Debuginfo.repo  CentOS-Sources.repo    epel-testing.repo
    [root@aming-01 yum.repos.d]# mv epel.repo epel.repo1
  • 安装expect包
    
    [root@aming-01 yum.repos.d]# cd /usr/local/sbin
    [root@aming-01 sbin]# yum install -y expect
    已加载插件:fastestmirror
    Determining fastest mirrors
    * base: mirrors.163.com
    * extras: mirrors.163.com
    * updates: mirrors.163.com
    正在解决依赖关系
    --> 正在检查事务
    ---> 软件包 expect.x86_64.0.5.45-14.el7_1 将被 安装
    --> 正在处理依赖关系 libtcl8.5.so()(64bit),它被软件包 expect-5.45-14.el7_1.x86_64 需要
    --> 正在检查事务
    ---> 软件包 tcl.x86_64.1.8.5.13-8.el7 将被 安装
    --> 解决依赖关系完成

依赖关系解决

==========================================================================================
Package 架构 版本 源 大小

正在安装:
expect x86_64 5.45-14.el7_1 base 262 k
为依赖而安装:
tcl x86_64 1:8.5.13-8.el7 base 1.9 M

事务概要

安装 1 软件包 (+1 依赖软件包)

总下载量:2.1 M
安装大小:4.9 M
Downloading packages:
(1/2): expect-5.45-14.el7_1.x86_64.rpm | 262 kB 00:00:02 
(2/2): tcl-8.5.13-8.el7.x86_64.rpm | 1.9 MB 00:00:06

总计 324 kB/s | 2.1 MB 00:00:06 
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : 1:tcl-8.5.13-8.el7.x86_64 1/2 
正在安装 : expect-5.45-14.el7_1.x86_64 2/2 
验证中 : 1:tcl-8.5.13-8.el7.x86_64 1/2 
验证中 : expect-5.45-14.el7_1.x86_64 2/2

已安装:
expect.x86_64 0:5.45-14.el7_1

作为依赖被安装:
tcl.x86_64 1:8.5.13-8.el7

完毕!
[root@aming-01 sbin]#

- 安装好expect包之后就可以去写expect脚本了,
- 自动远程登录后

[root@aming-01 sbin]# vi 1.expect

#! /usr/bin/expect
set host "192.168.202.132"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact

~



:wq

- 登录192.168.202.132 也就是aming-02机器
- 第一次登录会提示是否要继续登录,因为这个机器,在本机的known_host文件里面不存在,登录一个陌生的机器,会提示你一下,是否要建立连接
- 这个文件是就保证登录信息的,清空的话,重新远程登录ssh 会有提示是否要建立连接
/root/.ssh/known_hosts   

[root@aming-01 sbin]# vi 1.expect
[root@aming-01 sbin]# ssh 192.168.202.132
The authenticity of host '192.168.202.132 (192.168.202.132)' can't be established.
ECDSA key fingerprint is 00:22:2a:58:d5:c3:79:dc:8b:d7:b1:91:5c:ec:27:83.
Are you sure you want to continue connecting (yes/no)? no
Host key verification failed.
[root@aming-01 sbin]#

- 然后我们需要给这个脚本加一个执行权限

[root@aming-01 sbin]# vi 1.expect
[root@aming-01 sbin]# ./1.expect
spawn ssh root@192.168.202.132
The authenticity of host '192.168.202.132 (192.168.202.132)' can't be established.
ECDSA key fingerprint is 00:22:2a:58:d5:c3:79:dc:8b:d7:b1:91:5c:ec:27:83.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.202.132' (ECDSA) to the list of known hosts.
root@192.168.202.132's password: 
Last login: Tue Nov 28 21:09:45 2017 from 192.168.202.1
[root@aming-02 ~]#

- 就等登录一台机器,这是第一个expect脚本
- 登录机器之后还要退出来,执行一条命令再退出来,当然可以

# 20.29 expect脚本远程执行命令
- 刚才我们的第一个脚本,只是登录,
- 现在我们来写第二个脚本,不仅仅要远程登录,还要执行一条命令,最后再退出出来
- 自动远程登录后,执行命令并退出

[root@aming-02 ~]# 登出
Connection to 192.168.202.132 closed.
[root@aming-01 sbin]#

[root@aming-01 sbin]# vi 2.expect

#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh $user@192.168.202.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"


:wq

- 来运行下这个脚本

[root@aming-01 sbin]# chmod a+x 2.expect
[root@aming-01 sbin]# ./2.expect
spawn ssh root@192.168.202.132
root@192.168.202.132's password: 
Last login: Tue Nov 28 21:43:52 2017 from 192.168.202.130
[root@aming-02 ~]# touch /tmp/12.txt
[root@aming-02 ~]# echo 1212 > /tmp/12.txt
[root@aming-02 ~]# [root@aming-01 sbin]#

- 现在到aming-02上面去

[root@aming-02 ~]# [root@aming-01 sbin]# ./1.expect
spawn ssh root@192.168.202.132
root@192.168.202.132's password: 123456

Last login: Tue Nov 28 22:09:17 2017 from 192.168.202.130
[root@aming-02 ~]#

- 看下有没有12.txt

[root@aming-02 ~]# ls -l /tmp/12.txt
-rw-r--r-- 1 root root 5 11月 28 22:09 /tmp/12.txt
[root@aming-02 ~]# 
[root@aming-02 ~]# cat /tmp/12.txt
1212
[root@aming-02 ~]#

- 退出来看下

[root@aming-02 ~]# logout
Connection to 192.168.202.132 closed.
[root@aming-01 sbin]# cat 1.expect
#! /usr/bin/expect
set host "192.168.202.132"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no:" { send "yes\r"; exp_continue}
}
interact
[root@aming-01 sbin]# cat 2.expect
#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh $user@192.168.202.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@aming-01 sbin]#

- 脚本里的interact停留在远程的机器上不需要退出来,如果不加interact就会退出来

# 20.30 expect脚本传递参数
- 传递参数

[root@aming-01 sbin]# vi 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"

~



:wq

- 下面来执行下这个脚本
- ./3.expect root 192.168.202.132 ls 执行脚本  用户名 主机名 执行的命令

[root@aming-01 sbin]# vi 3.expect
[root@aming-01 sbin]# chmod a+x 3.expect
[root@aming-01 sbin]# ./3.expect root 192.168.202.132 ls
spawn ssh root@192.168.202.132
root@192.168.202.132's password: 
Last login: Tue Nov 28 22:15:08 2017 from 192.168.202.130
[root@aming-02 ~]# ls
anaconda-ks.cfg zabbix-release-3.2-1.el7.noarch.rpm
[root@aming-02 ~]# [root@aming-01 sbin]#

- 先登录这个机器,然后执行ls命令
- 假如我想执行多条命令ls;w;vmstat 1; 最后退出出来,需要用双引号引起来
- expect有一个默认超时时间,所以这里使用vmstat 1 不合适

[root@aming-02 ~]# [root@aming-01 sbin]# ./3.expect root 192.168.202.132 "ls;w;vmstat 1"
spawn ssh root@192.168.202.132
root@192.168.202.132's password: 
Last login: Tue Nov 28 22:31:05 2017 from 192.168.202.130
[root@aming-02 ~]# ls;w;vmstat 1
anaconda-ks.cfg zabbix-release-3.2-1.el7.noarch.rpm
22:34:10 up 1:26, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 21:09 1:24m 0.01s 0.01s -bash
root pts/0 192.168.202.1 21:09 1:24m 0.01s 0.01s -bash
root pts/1 192.168.202.130 22:34 0.00s 0.01s 0.01s w
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 284016 876 169392 0 0 27 3 65 134 0 0 99 0 0
0 0 0 284016 876 169392 0 0 0 0 60 123 0 0 100 0 0
0 0 0 284016 876 169392 0 0 0 0 69 135 0 1 99 0 0
0 0 0 284016 876 169392 0 0 0 0 62 117 0 0 100 0 0
0 0 0 284016 876 169392 0 0 0 0 54 110 1 0 99 0 0
0 0 0 284016 876 169392 0 0 0 0 60 118 0 1 99 0 0
0 0 0 284016 876 169392 0 0 0 0 49 106 0 0 100 0 0
0 0 0 284016 876 169392 0 0 0 0 55 114 0 0 100 0 0
0 0 0 284016 876 169392 0 0 0 0 56 114 0 0 100 0 0
2 0 0 284016 876 169392 0 0 0 0 54 111 0 0 100 0 0
[root@aming-01 sbin]#

- 再去另一台机器上看下vmstat 1 这个命令有没有继续在运行

[root@aming-01 sbin]# ./1.expect
spawn ssh root@192.168.202.132
root@192.168.202.132's password: 123456

Last login: Tue Nov 28 22:34:10 2017 from 192.168.202.130
[root@aming-02 ~]# ps aux |grep vmstat
root 3771 0.0 0.0 112680 976 pts/1 R+ 22:36 0:00 grep --color=auto vmsta
[root@aming-02 ~]#


- 没有了,因为之前vmstat 1 在一个终端上执行得,最后那个终端都退出来了,相当于我们中断了,ctrl c了。