20.27 分发系统介绍
场景:业务逐渐增大,网站的app的后端编程语言是php,想要运行php要配置一个LNMP、LAMP的环境,但是平常业务在不断的迭代,如果服务器增多,那我们就需要的一个分发系统,用来把新的代码传递到各个服务器上去。
这里讲解的expect软件就是一个上线的系统。
首先要准备一台模板的机器,这里是最新的代码,然后需要上线的机器的对应的ip和密码都需要知道,然后就可以推送最新的代码给所有的机器。
20.28 expect脚本远程登录
之前安装过的包有一个叫mkpasswd的命令,安装这个的时候安装的包就是expect。
我们第一个实例就是自动登录远程某一天机器,并执行命令。
脚本如下:
#!/usr/bin/expect
set host "192.168.133.132"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
interact
设置变量要使用set命令,spawn关键字后面跟的就是shell命令,expect就是传递消息。
引号内是截取的信号字段,后面花括号内是执行的命令。
exp_continue是继续执行expect,interact是结束。
root@lhy-server:~/shell_script# ./expect1.sh
spawn ssh [email protected]
The authenticity of host '192.168.0.109 (192.168.0.109)' can't be established.
ECDSA key fingerprint is SHA256:RGujPvieJX8G0MYV7PLypBodAkZ+M5RYrxMnFJeltNE.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.109' (ECDSA) to the list of known hosts.
[email protected]'s password:
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.15.0-34-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
39 packages can be updated.
0 updates are security updates.
New release '18.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Wed Sep 26 23:10:28 2018 from 192.168.0.109
执行完脚本之后就会进入我们的192.168.0.109主机。
这里需要注意的一点是expect后的左花括号要写在一行。
20.29 expect脚本远程执行命令
脚本如下,过程就是登录以后执行命令然后退出。
#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh [email protected]
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"
这里要按照自己的PS1的设置去更改。
结果如下
root@lhy-server:~/shell_script# chmod 700 expect2.sh
root@lhy-server:~/shell_script# ./expect2.sh
spawn ssh [email protected]
[email protected]'s password:
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.15.0-34-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
39 packages can be updated.
0 updates are security updates.
New release '18.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Wed Sep 26 23:11:28 2018 from 192.168.0.109
lhy@lhy-server:~$ touch /tmp/12.txt
lhy@lhy-server:~$ echo 1212 > /tmroot@lhy-server:~/shell_script# ./expect1.sh
spawn ssh [email protected]
[email protected]'s password:
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.15.0-34-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
39 packages can be updated.
0 updates are security updates.
New release '18.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Thu Sep 27 11:13:51 2018 from 192.168.0.109
lhy@lhy-server:~$ ls /tmp/12.txt -l
-rw-rw-r-- 1 lhy lhy 5 Sep 27 11:13 /tmp/12.txt
lhy@lhy-server:~$ cat /tmp/12.txt
1212
最后没有显示完整,应该与脚本执行速度过快而带来的影响。这里我测试的结果就是最后一定要在脚本的最后一行协商interact。
当然不需要留下交互的脚本也可以使用expect eof来结束。
20.30 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"
传参规范就是方括号内写的是内置变量lindex+参数数组。
root@lhy-server:~/shell_script# ./expect3.sh lhy 192.168.0.109 "ll /tmp"
spawn ssh [email protected]
[email protected]'s password:
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.15.0-34-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
39 packages can be updated.
0 updates are security updates.
New release '18.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Thu Sep 27 11:56:59 2018 from 192.168.0.109
lhy@lhy-server:~$ ll /tmp
total 620
drwxrwxrwt 10 root root 4096 Sep 27 12:03 ./
drwxr-xr-x 25 root root 4096 Sep 17 01:06 ../
-rw-rw-r-- 1 lhy lhy 5 Sep 27 11:56 12.txt
drwxrwxrwt 2 root root 4096 Sep 16 15:06 .font-unix/
drwxrwxrwt 2 root root 4096 Sep 16 15:06 .ICE-unix/
drwx------ 3 root root 4096 Sep 16 15:06 systemd-private-10e54c0f924f4fa5bb0916a17c4ee96e-colord.service-6ienvA/
drwx------ 3 root root 4096 Sep 16 15:06 systemd-private-10e54c0f924f4fa5bb0916a17c4ee96e-rtkit-daemon.service-J5oPaN/
drwx------ 3 root root 4096 Sep 16 15:06 systemd-private-10e54c0f924f4fa5bb0916a17c4ee96e-systemd-timesyncd.service-iK7nm1/
-rw-r--r-- 1 root root 253 Sep 25 15:17 test.log
drwxrwxrwt 2 root root 4096 Sep 16 15:06 .Test-unix/
-rw------- 1 root root 12 Sep 18 11:17 tmp0SscPz
-rw-rw---- 1 root root 0 Sep 18 11:17 tmp2dfC8z
-rw------- 1 root root 24 Sep 18 11:17 tmp2xgfN_
-rw-rw---- 1 root root 377 Sep 18 11:17 tmp70bCDG
-rw------- 1 root root 45 Sep 18 11:17 tmp7yVz5Z
-rw------- 1 root root 525838 Sep 18 11:51 tmpaddon
-rw------- 1 root root 12 Sep 18 11:17 tmpaHgRZn
-rw------- 1 root root 12 Sep 18 11:17 tmpB4sAb4
-rw-rw---- 1 root root 0 Sep 18 11:17 tmpEA1aPa
-rw-rw---- 1 root root 41 Sep 18 11:17 tmpK1ZTYd
-rw-rw---- 1 root root 15 Sep 18 11:17 tmpLeVM2c
-rw------- 1 root root 90 Sep 18 11:17 tmpMZC8xK
-rw-rw---- 1 root root 48 Sep 18 11:17 tmpoCCFCH
-rw------- 1 root root 0 Sep 18 11:17 tmppJTX02
-rw-rw---- 1 root root 0 Sep 18 11:17 tmpRkryP2
-rw-rw---- 1 root root 10 Sep 18 11:17 tmprRQNYC
-rw------- 1 root root 478 Sep 18 11:17 tmpTshdUn
-rw-rw---- 1 root root 0 Sep 18 11:17 tmpwsFmBe
-rw------- 1 root root 116 Sep 18 11:17 tmpzjSgya
-r--r--r-- 1 root root 11 Sep 16 15:06 .X0-lock
drwxrwxrwt 2 root root 4096 Sep 16 15:06 .X11-unix/
drwxrwxrwt 2 root root 4096 Sep 16 15:06 .XIM-unix/
lhy@lhy-server:~$ exit
logout
Connection to 192.168.0.109 closed.
执行的命令最好用双引号括起来,多条命令用分号分隔。
expect有一个默认超时时间,超时之后expect的任务都会结束。
所以expect脚本要考虑好超时时间,当然也是可以用set timeout 来设置自定义的超时时间。