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

20.27分发系统介绍 分发系统,用在什么场景 

例如公司业务做的越来越大,公司的网站或App,它们的后端和服务端所用的编程语言为PHP,所以说要想运行这个环境,PHP的代码,需要配置LAMP或LNMP的环境,还需要把代码上传到服务器上去,说白了就是一个网站. 在平常的工作中,业务不断在迭代,有新的功能出现,那么这时候就需要去更改代码,若是一两台机器的话还好一点,可以直接在服务器上更改,但是这样的话不规范. 若是十几台甚至是上百台机器的话,存贮都是一站点,比如说有一个接口,App访问量很大,App需要调用服务端的接口,那服务器接口有50台机器去承载,这时候就需要弄一个分发系统,能够让我们把每天或每段时间更新的代码,分别的发布到这50台机器上去.


用shell 编程能够实现上线的工具 

说明:所说的分发系统,也就是上线的shell脚本

核心的东西叫做expect, expect也可以说是一种脚本语言,和shell非常的像,我们可以用expect去实现传输文件;还可以实现远程执行命令,不需要我们去输入密码; 所谓上线代码,就是把开发的语言or代码发布到线上环境上去. 在这里的分发系统里,首先要准备一台模版的机器,这台机器上的代码是最新的代码,是准备要上线的代码. 再就是例如要给50台机器上线,得知道这50台机器的IP地址,还有这50台机器对应的用户密码也得知道. 最后就是用expect脚本,借助于rsnc把这些代码推送到这50台机器.

20.28expect脚本远程登录 
yum安装expect 
[[email protected] ~]# yum install -y expect 写expect脚本--自动远程登录 
#自动远程登录脚本
[[email protected] test]# vim 1.expect

#! /usr/bin/expect
#host 是变量,后面跟登录的IP
set host "192.168.2.116"
#passwd是变量,后面跟IP的密码
set passwd "123456" 
#登录语句 
spawn ssh [email protected]$host
#当首次登录的时候,会提示是否要继续登录 
expect {
"yes/no" {send"yes/r";exp_continue}
"password:"{send "$passwd/r"}
}
interact注释:/r -->表示后回车的意思
interact -->表示需要停留在远程的机器上,如果不加这个参数那么就会退出远程的脚本
或者使用expect eof --> 表示登录到远程机器上,会停留一两秒钟,然后自动退出来# 添加执行权限
[[email protected] test]# chmod a+x 1.expect
#执行
[[email protected] test]# ./1.expect 
spawn ssh [email protected]
The authenticity of host '192.168.2.116 (192.168.2.116)' can't be established.
ECDSA key fingerprint is bd:fd:56:cf:07:80:0d:a6:9c:38:00:be:33:1f:f5:7a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.116' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Last login: Wed Sep 20 10:25:06 2017 from 192.168.2.102
[[email protected] ~]# 
说明:成功登录到192.168.2.116机器 
20.29expect脚本远程执行命令 
自动远程登录后--执行命令并退出 
[[email protected] test]# vim 2.expect
#! /usr/bin/expect
#user是变量
set user "root"
#passwd是变量,后面跟user的密码
set passwd "123456"
#登录语句
spawn ssh [email protected]
#当首次登录的时候,会提示是否要继续登录 
expect {
"yes/no" {send "yes/r"; exp_continue}
"password:" {send "$passwd/r"}
}
expect "]*"
send "touch /tmp/66.txt/r"
expect "]*"
send "echo This is test > /tmp/66.txt/r"
expect "]*"
send "exit/r"注释: expect "]*" 当root用户登录的时候 后半个括号后面跟的是# ([[email protected] ~]#)
普通用户登录的时候,后半个括号后面跟的是~.
expect "]*" 这里用*号,表示通配,不管是root用户还是普通用户,都将执行下面的命令#添加执行权限
[[email protected] test]# chmod a+x 2.expect 
#执行
[[email protected] test]# ./2.expect 
spawn ssh [email protected]
[email protected]'s password: 
Last login: Wed Sep 20 10:30:43 2017 from 192.168.2.115
[[email protected] ~]# touch /tmp/66.txt
[[email protected] ~]# echo This is test > /tmp/66.txt
[[email protected] ~]# [[email protected] test]# 
说明:登录到root-02 执行了两条命令,然后退出来#可以执行1.expect登录到root-02,然后查看一下是否创建了66.txt
[[email protected] test]# ./1.expect 
spawn ssh [email protected]
[email protected]'s password: 
Last login: Wed Sep 20 11:08:09 2017 from 192.168.2.115
[[email protected] ~]# ls /tmp/66.txt 
/tmp/66.txt
[[email protected] ~]# cat /tmp/66.txt 
This is test
说明:root-02 /tmp/下有66.txt,并且也有“This is test”,说明执行成功了. 
20.30expect脚本传递参数 
传递参数 
[[email protected] test]# vim 3.expect
#! /usr/bin/expect
#user第一个参数
set user [lindex $argv 0]
#host第二个参数
set host [lindex $argv 1]
set passwd "123456"
#cm第三个参数,也就是要执行的命令
set cm [lindex $argv 2]
spawn ssh [email protected]
expect {
"yes/no" {send "yes/r"}
"password:" {send "$passwd/r"}
}
expect "]*"
send "$cm/r"
expect "]*"
send "exit/r"#添加执行权限
[[email protected] test]# chmod a+x 3.expect #执行
[[email protected] test]# ./3.expect root 192.168.2.116 ls
spawn ssh [email protected]
[email protected]'s password: 
Last login: Wed Sep 20 11:12:05 2017 from 192.168.2.115
[[email protected] ~]# ls
anaconda-ks.cfg
[[email protected] ~]# [[email protected] test]# 
说明:root是第一个参数; 192.168.2.116是第二个参数; ls是第三个参数;#若需要执行多条命令的时候,需要用冒号以及分号,把命令括起来
[[email protected] test]# ./3.expect root 192.168.2.116 "ls;touch 1.txt;echo "this is test" > 1.txt"
spawn ssh [email protected]
[email protected]'s password: 
Last login: Wed Sep 20 11:39:34 2017 from 192.168.2.115
[[email protected] ~]# ls;touch 1.txt;echo this
1.txtanaconda-ks.cfg
this
说明:touch 1.txt,并写入this is test
#执行1.expect登录root-02,查看一下是否touch 1.txt 以及写入的内容
[[email protected] test]# ./1.expect
spawn ssh [email protected]
[email protected]'s password: 
Last login: Wed Sep 20 11:40:25 2017 from 192.168.2.115
[[email protected] ~]# cat 1.txt 
this is test
[[email protected] ~]# ls
1.txtanaconda-ks.cfg
说明:很明显执行成功了

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索登录 , 脚本 , 命令 , 参数 , 远程 , 传递 , expect , 分发 , 执行 介绍 ,以便于您获取更多的相关知识。

原文地址:https://www.cnblogs.com/pta188/p/9336118.html