1、分发系统介绍:expect,也就是一个分发脚本:
场景:业务会越来越大,网站app,后端,服务端所使用的编程语言为php,要想运行这个环境,就需要配置LAMP或者LNMP,最后还需要把代码上传到服务器上去,业务不断的增加,一两台机器还好好说,要是几十台,上百台设备,这时候就需要用到了分发系统了,把每天更新的代码发布到每一台机器上去:
expect是一种脚本语言,通过它可以实现传输,上线代码,可以实现远程执行命令,不需要输入密码:
首先准备一台模板机器(最新的代码待上线),然后给下面分发代码的多台台机器上线,机器的IP,对应用户的密码,然通过expect,借助rsync来上传代码,可以用expect去登录执行某些命令:
1:expect脚本安装:
yum install -y expect (安装包和mkpasswd是一起的)
示例1:自动远程登录,并执行一些命令:
[root@localhost_01 sbin]# vim 1.expect
#! /usr/bin/expect
set host "192.168.149.129"
set passwd "nihao123!"
set port "-p 56888"
spawn ssh $port root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
##expect eof 表示登录远程机器后,暂停一两秒钟后退出:
interact
#脚本结束
注释:在expect中定义变量需要set host才可以:
注释:在文件里 expect { }这里是保证登录信息,第一次登录时候,则需要yes记录登录信息,下一次则不需要了,需要清空 >/root/.ssh/known_hosts才可以:
exp_continue表示继续/r换行, interact表示继续停留在这台机器不退出:如果不加的话则登录后马上退出:
[root@localhost_01 sbin]# ssh -p 56888 192.168.149.129
root@192.168.149.129's password:
#因为我们以前登录过,所有不询问为了安全性,是否需要还继续链接:
[root@localhost_01 sbin]# > /root/.ssh/
authorized_keys authorized_keys_xshell id_rsa.pub
authorized_keys.bakup id_rsa known_hosts
[root@localhost_01 sbin]# > /root/.ssh/known_hosts #清空这个文件里面的信息:
[root@localhost_01 sbin]# ssh -p 56888 192.168.149.129 #再次登录时则会询问:
The authenticity of host '[192.168.149.129]:56888 ([192.168.149.129]:56888)' can't be established.
ECDSA key fingerprint is SHA256:mtOEV1y+2ErXFWqQRL0rYCQGGuVE7z4n1is+2dPQc7E.
ECDSA key fingerprint is MD5:d9:f7:03:de:c4:f5:40:89:be:3c:25:6d:70:6a:49:a5.
Are you sure you want to continue connecting (yes/no)? yes
加入执行权限后成功登录: chmod a+x 1.expect
[root@localhost_01 sbin]# ./1.expect
-bash: ./1.expect: 权限不够
[root@localhost_01 sbin]# chmod a+x 1.expect
[root@localhost_01 sbin]# ./1.expect
spawn ssh -p 56888 root@192.168.149.129
The authenticity of host '[192.168.149.129]:56888 ([192.168.149.129]:56888)' can't be established.
ECDSA key fingerprint is SHA256:mtOEV1y+2ErXFWqQRL0rYCQGGuVE7z4n1is+2dPQc7E.
ECDSA key fingerprint is MD5:d9:f7:03:de:c4:f5:40:89:be:3c:25:6d:70:6a:49:a5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.149.129]:56888' (ECDSA) to the list of known hosts.
root@192.168.149.129's password:
Last login: Thu Oct 4 00:33:35 2018 from 192.168.149.135
[root@localhost_02 ~]#
2:expect远程执行命令:
远程执行完命令,并退出:
[root@localhost_01 sbin]# vim 2.expect
#!/usr/bin/expect
set user "root"
set passwd "nihao123!"
set port "-p 56888"
spawn ssh $port $user@192.168.180.135
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"
注释:expect "]*" 星号表示通配,匹配右边的所有字符(不管是root用户普通用户):
注释:send 表示执行这个命令:
3:需要加执行权限并执行这个命令:
[root@localhost_01 sbin]# chmod a+x 2.expect
[root@localhost_01 sbin]# ll 2.expect
-rwxr-xr-x 1 root root 307 10月 4 16:09 2.expect
[root@localhost_01 sbin]# ./2.expect
spawn ssh -p 56888 root@192.168.149.129
root@192.168.149.129's password:
Last login: Thu Oct 4 06:00:22 2018 from 192.168.149.130
[root@localhost_02 ~]# touch /tmp/12.txt
[root@localhost_02 ~]# echo 1212 > /tmp/12.txt
然后在129这台机器上查看是否创建成功:
[root@localhost_02 ~]# ls /tmp/12.txt
/tmp/12.txt
[root@localhost_02 ~]# cat /tmp/12.txt
1212
4、expect脚本传递参数:
[root@localhost_01 sbin]# vim 3.expect
[root@localhost_01 sbin]# cat 3.expect
#!/usr/bin/expect
set port [lindex $argv 0]
#第一个参数,如果端口还是默认的22,则不需要定义次参数,依次往下定义即可:
set user [lindex $argv 1]
#第二个参数
set host [lindex $argv 2]
#第三个参数
set passwd "nihao123!"
set cm [lindex $argv 3]
#第四参数
spawn ssh -p $port $user@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
1:增加执行权限: chmod +x 3.expect
[root@localhost_01 sbin]# chmod a+x 3.expect
[root@localhost_01 sbin]# ll 3.expect
-rwxr-xr-x 1 root root 369 10月 4 16:37 3.expect
2:执行此脚本:
./3.expect 第一个参数(port) 第二个参数(user) 第三个参数(host) 第四个参数(command)
[root@localhost_01 sbin]# ./3.expect 56888 root 192.168.149.129 ls
spawn ssh -p 56888 root@192.168.149.129
root@192.168.149.129's password:
Last login: Thu Oct 4 06:24:37 2018 from 192.168.149.130
[root@localhost_02 ~]# ls
1 anaconda-ks.cfg bash CentOS7-Base-163.repo link shell test test.txt
同样,也支持多条参数: 多条命令最后用双引号括住,然后用分号分割则可以:
[root@localhost_01 sbin]# ./3.expect 56888 root 192.168.149.129 "ls;w"
spawn ssh -p 56888 root@192.168.149.129
root@192.168.149.129's password:
Last login: Thu Oct 4 06:32:46 2018 from 192.168.149.130
[root@localhost_02 ~]# ls;w
1 anaconda-ks.cfg bash CentOS7-Base-163.repo link shell test test.txt
06:32:57 up 19:42, 2 users, load average: 0.42, 0.23, 0.16
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.149.130 06:32 1.00s 0.02s 0.01s w
root pts/1 192.168.149.135 00:33 26:57 0.03s 0.03s -bash
注释:如果是执行多条命令的话,也可以传递多个参数,如果要是把多个命令当成一个参数来传递的话,则需要用双引号引起来,并且用分号分割才可以:
同样:当我们传递第四个参数时: vmstat模式超时时间是10秒:
[root@localhost_01 sbin]# ./3.expect 56888 root 192.168.149.129 "w;vmstat 1"
spawn ssh -p 56888 root@192.168.149.129
root@192.168.149.129's password:
Last failed login: Thu Oct 4 06:56:10 CST 2018 from 192.168.149.130 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Thu Oct 4 06:37:26 2018 from 192.168.149.130
[root@localhost_02 ~]# w;vmstat 1
07:07:33 up 20:17, 2 users, load average: 0.01, 0.19, 0.20
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.149.130 07:07 0.00s 0.03s 0.03s w
root pts/1 192.168.149.135 00:33 1:01m 0.03s 0.03s -bash
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 0 1026092 2076 240768 0 0 3 42 61 101 0 0 99 1 0
0 0 0 1026108 2076 240820 0 0 0 0 37 78 0 1 99 0 0
0 0 0 1026108 2076 240820 0 0 0 0 42 84 0 0 100 0 0
0 0 0 1026108 2076 240820 0 0 0 0 42 82 0 0 100 0 0
0 0 0 1026108 2076 240820 0 0 0 10 54 90 0 0 100 0 0
0 0 0 1026108 2076 240820 0 0 0 0 37 78 0 0 100 0 0
0 0 0 1026108 2076 240820 0 0 0 103 43 104 0 1 99 0 0
0 0 0 1026108 2076 240820 0 0 0 0 45 86 0 0 100 0 0
0 0 0 1026108 2076 240820 0 0 0 0 37 74 0 0 100 0 0
0 0 0 1026108 2076 240820 0 0 0 0 34 72 0 0 100 0 0
0 0 0 1026108 2076 240820 0 0 0 0 33 70 0 0 100 0 0
0 0 0 1026108 2076 240820 0 0 0 0 35 80 0 0 100 0 0
十秒后自动超时:
注释:也可以设置永不超时: 需要在执行命令(也就是第四个参数)后面添加 set timeout -1
expect "]*"
send "$cm\r"
#set timeout -1 #表示永不超时:
#set timeout 5 #也可以手动指定具体的秒数: 本次5ms:
expect "]*"
send "exit\r"