20.27 分发系统介绍
所谓上线:就是将开发人员的代码 发布到线上环境去!
ftp/xftp/rzsz
svn/git里面拉取下来,再上线 准备模版机器:(包含最新的代码 即要上线的最新内容的机器一台),给其他机器推送(要知道ip及用户密码) 再用expect 脚本,借助于rsync 把这些代码推送到指定机器; 也可以expect单独登录某一机器执行命令
20.28 expect脚本远程登录
准备:yum install -y expect
实现:自动远程登录#! /usr/bin/expectset host "192.168.133.132"set passwd "123456"spawn ssh root@$hostexpect {"yes/no" { send "yes\r"; exp_continue}"assword:" { send "$passwd\r" }}interact
expect脚本解析:
#! /usr/bin/expectset host "192.168.60.12" ##定义变量:host;和shell定义变量有所不同set passwd "123456" ##定义变量;spawn ssh root@$host ##登录机器命令;expect { ##真正核心脚本,在expect里面:"yes/no" { send "yes\r"; exp_continue} ##"yes/no"是通配符,匹配到这个 就发送{}里面的内容;\r表示回车;exp_continue表示继续"assword:" { send "$passwd\r" }}interact ##表示停留在这个登录的机器,不加这个就直接退出了;或者替换为expect eof表示停留2秒 再退出
执行结果:
[root@DasonCheng sbin]# vim 1.expect[root@DasonCheng sbin]# chmod +x 1.expect [root@DasonCheng sbin]# ./1.expect spawn ssh root@192.168.60.12root@192.168.60.12's password: Last login: Wed Sep 20 19:57:44 2017 from 192.168.60.1[root@aming2 ~]# pwd/root[root@aming2 ~]# whoamiroot[root@aming2 ~]# exit登出Connection to 192.168.60.12 closed.
20.29 expect脚本远程执行命令
自动远程登录后,执行命令并退出;
#!/usr/bin/expectset user "root"set passwd "123456"spawn ssh $user@192.168.133.132expect {"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"
脚本解析:
#!/usr/bin/expectset user "root"set passwd "123456"spawn ssh $user@192.168.133.132expect {"yes/no" { send "yes\r"; exp_continue}"password:" { send "$passwd\r" }} ##以上和前一节脚本是一样的,用于登录远程机器;下面才是执行的命令!expect "]*" ##这里"]*"通配符,用来匹配]#或者]$等……send "touch /tmp/12.txt\r" ##执行命令touch /tmp/12.tst 回车;expect "]*"send "echo 1212 > /tmp/12.txt\r"expect "]*"send "exit\r"
执行结果:
[root@DasonCheng sbin]# vim 2.expect[root@DasonCheng sbin]# chmod +x 2.expect [root@DasonCheng sbin]# ./2.expect spawn ssh root@192.168.60.12root@192.168.60.12's password: Last login: Thu Sep 21 08:46:38 2017 from 192.168.60.1[root@aming2 ~]# touch /tmp/12.txt[root@aming2 ~]# echo 1212 > /tmp/12.txt[root@aming2 ~]#
60.12机器查看:
[root@aming2 ~]# ll /tmp/12.txt -rw-r--r--. 1 root root 5 9月 21 08:52 /tmp/12.txt
20.30 expect脚本传递参数
传递参数:
#!/usr/bin/expectset user [lindex $argv 0]set host [lindex $argv 1]set passwd "123456"set cm [lindex $argv 2]spawn ssh $user@$hostexpect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect "]*"send "$cm\r"expect "]*"send "exit\r"
脚本解析:
#!/usr/bin/expectset user [lindex $argv 0] ##和$1一样,调用内置变量参数;只不过定义 和调用方式不同!set host [lindex $argv 1]set passwd "123456" ##定义passwordset cm [lindex $argv 2] ##定义cm变量,为第三个参数spawn ssh $user@$hostexpect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect "]*" ##通配符匹配send "$cm\r" ##执行第三个参数命令;expect "]*"send "exit\r" ##退出
执行结果:
[root@DasonCheng sbin]# ./3.expect root 192.168.60.12 lsspawn ssh root@192.168.60.12root@192.168.60.12's password: Last failed login: Thu Sep 21 09:07:52 CST 2017 from test.com on ssh:nottyThere was 1 failed login attempt since the last successful login.Last login: Thu Sep 21 09:03:28 2017 from test.com[root@aming2 ~]# lsaming anaconda-ks.cfg##下面是同时执行两个命令;[root@DasonCheng sbin]# ./3.expect root 192.168.60.12 "ls;pwd"spawn ssh root@192.168.60.12root@192.168.60.12's password: Last login: Thu Sep 21 09:08:31 2017 from test.com[root@aming2 ~]# ls;pwdaming anaconda-ks.cfg/root
指定超时时间:
#!/usr/bin/expectset user [lindex $argv 0]set host [lindex $argv 1]set passwd "p@ssw0rd"set cm [lindex $argv 2]spawn ssh $user@$hostexpect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect "]*"send "$cm\r"set timeout 3 ##指定超时时间为3秒;-1为永久超时expect "]*"send "exit\r"
超时3秒结果:
[root@DasonCheng sbin]# ./3.expect root 192.168.60.12 "pwd ;vmstat 1"spawn ssh root@192.168.60.12root@192.168.60.12's password: Last failed login: Thu Sep 21 15:35:24 CST 2017 from test.com on ssh:nottyThere were 2 failed login attempts since the last successful login.Last login: Thu Sep 21 15:29:36 2017 from test.com[root@aming2 ~]# pwd ;vmstat 1/rootprocs -----------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 56944 4 315292 0 0 4 3 53 88 0 0 100 0 0 0 0 0 56992 4 315320 0 0 0 0 77 102 0 0 100 0 0 0 0 0 56992 4 315320 0 0 0 0 53 81 0 0 100 0 0[root@DasonCheng sbin]#