20.27分发系统介绍 分发系统,用在什么场景 php
例如公司业务作的愈来愈大,公司的网站或App,它们的后端和服务端所用的编程语言为PHP,因此说要想运行这个环境,PHP的代码,须要配置LAMP或LNMP的环境,还须要把代码上传到服务器上去,说白了就是一个网站. 在日常的工做中,业务不断在迭代,有新的功能出现,那么这时候就须要去更改代码,如果一两台机器的话还好一点,能够直接在服务器上更改,可是这样的话不规范. 如果十几台甚至是上百台机器的话,存贮都是一站点,好比说有一个接口,App访问量很大,App须要调用服务端的接口,那服务器接口有50台机器去承载,这时候就须要弄一个分发系统,可以让咱们把天天或每段时间更新的代码,分别的发布到这50台机器上去.shell
用shell 编程可以实现上线的工具 编程
说明:所说的分发系统,也就是上线的shell脚本vim
核心的东西叫作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 , 分发 , 执行 介绍 ,以便于您获取更多的相关知识。ssh