20.27 分发系统介绍
由于业务迭代更新,需要更改代码,如果机器很多,那么久需要一个分发系统,可以把每段时间更新的代码分别发布到机器上去
分发系统就是上线的shell脚本,核心为expect
expect是一种脚本语言,和shell很像,可以用它去实现传输文件和远程执行命令,不需要去输入密码
20.28 expect脚本远程登录
[[email protected] ~]# yum install -y expect
(过程省略)
·自动远程登录
expect中定义变量和shell中的区别
shell中是:变量=xxx
expect中是:set 变量 "xxx"
spawn ssh [email protected]$host 登录的语句
yes/no 是指第一次远程登录时,会提示是否连接,如下图
当提示为 yes/no 时,怎么做。send "yes\r"; exp_continue
当提示为 password 时,怎么做。 send "$passwd\r" (填写关键词接口,也可以是assword)
interact 表示结束,停留在远程的机器上(如果不加这一行,那么登录后马上回退出)
(如果不用interact,而是expect eof ,那么会停留在机器上几秒,然后退出)
[[email protected] sbin]# chmod a+x 1.expect ##给执行权限 [[email protected] sbin]# ./1.expect spawn ssh [email protected] The authenticity of host '192.168.65.129 (192.168.65.129)' can't be established. ECDSA key fingerprint is SHA256:PXl0tJsOm3464x52jTxX7L+ToKm1Hb6AUMq6lh4ASX0. ECDSA key fingerprint is MD5:2e:9a:f9:25:b3:80:43:05:a4:3d:3c:9b:48:6e:a0:0e. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.65.129' (ECDSA) to the list of known hosts. [email protected]'s password: Last login: Tue Apr 24 22:19:31 2018 from 192.168.65.1 [[email protected] ~]# ifconfig
已经成功登录到了129这台机器
20.29 expect脚本远程执行命令
·自动远程登陆后,执行命令并退出
前一半和之前的登录脚本基本一致,只不过这次set的变量为user
expect "]*" ##遇到 "]*" ,则执行 send "touch /tmp/12.txt\r"
expect "]*" ##再遇到 "]*" ,则 send "echo 1212 > /tmp/12.txt\r"
expect "]*" ##然后 send "exit\r"
因为命令行在未执行命令时都是 ]# 或 ]$ 再最后,因此,这样可以一步一步地执行命令
[[email protected] sbin]# chmod a+x 2.expect [[email protected] sbin]# ./2.expect spawn ssh [email protected] [email protected]'s password: Last login: Tue Apr 24 22:31:51 2018 from 192.168.65.128 [[email protected] ~]# touch /tmp/12.txt [[email protected] ~]# echo 1212 > /tmp/12.txt [[email protected]host ~]# [[email protected] sbin]# ssh 192.168.65.129 [email protected]'s password: Last login: Tue Apr 24 22:56:18 2018 from 192.168.65.128 [[email protected] ~]# ll /tmp/12.txt ##在129上可以看到文本存在 -rw-r--r-- 1 root root 5 4月 24 22:56 /tmp/12.txt [[email protected] ~]# cat /tmp/12.txt ##内容为1212 1212
20.30 expect脚本传递参数
·传递参数
set user [lindex $argv 0],set host [lindex $argv 1],set cm [lindex $argv 2]分别表示第1,2,3个参数
设置用户名,主机,命令(cm)
执行过程和之前脚本一致,只不过在执行命令时,需要向在命令行写出3个参数传递到脚本中
[[email protected] sbin]# chmod a+x 3.expect [[email protected] sbin]# ./3.expect root 192.168.65.129 ls spawn ssh [email protected] [email protected]'s password: Last login: Tue Apr 24 22:57:58 2018 from 192.168.65.128 [[email protected] ~]# ls 1.txt aminglinux a.txt b.txt sed test test1.txt test2.txt test3.txt x.txt 2.txt anaconda-ks.cfg awk grep ssl.conf test1 test2 test3 test.txt zabbix-release-3.2-1.el7.noarch.rpm
(root为参数1,ip为参数2,命令ls为参数3)
如果想要传递多个命令,需要用双引号括起来,并且命令之间用分号隔开
[[email protected] sbin]# ./3.expect root 192.168.65.129 "ls;w;vmstat 1" spawn ssh [email protected] [email protected]'s password: Last login: Tue Apr 24 23:22:08 2018 from 192.168.65.128 [[email protected] ~]# ls;w;vmstat 1 1.txt aminglinux a.txt b.txt sed test test1.txt test2.txt test3.txt x.txt 2.txt anaconda-ks.cfg awk grep ssl.conf test1 test2 test3 test.txt zabbix-release-3.2-1.el7.noarch.rpm 23:22:30 up 1:04, 2 users, load average: 0.02, 0.03, 0.05 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT root pts/0 192.168.65.1 22:19 50:30 0.04s 0.04s -bash root pts/1 192.168.65.128 23:22 0.00s 0.03s 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 2 0 0 187692 2076 203060 0 0 44 68 129 160 0 1 99 0 0 1 0 0 187584 2076 203060 0 0 0 8 137 156 0 0 100 0 0 0 0 0 187584 2076 203060 0 0 0 548 123 142 0 1 99 0 0 0 0 0 187584 2076 203060 0 0 0 5 142 161 0 0 100 0 0 0 0 0 187584 2076 203060 0 0 0 104 121 151 0 0 100 0 0 0 0 0 187584 2076 203060 0 0 0 11 136 167 0 0 100 0 0 0 0 0 187584 2076 203060 0 0 0 6 124 144 0 0 100 0 0 0 0 0 187336 2076 203060 0 0 0 12 159 178 0 1 98 1 0 0 0 0 187368 2076 203060 0 0 0 196 141 165 0 0 100 0 0 0 0 0 187368 2076 203060 0 0 0 5 159 173 1 1 98 0 0
(因为expect有超时时间,所有10秒后会退出)
原文地址:http://blog.51cto.com/11530642/2107496