博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
20.27 分发系统介绍
阅读量:7252 次
发布时间:2019-06-29

本文共 4538 字,大约阅读时间需要 15 分钟。

hot3.png

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.

mark

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]#

转载于:https://my.oschina.net/u/3651233/blog/1540799

你可能感兴趣的文章
redis事务需要注意的坑------RedisConnectionFailureException
查看>>
SPOJ 4110 Fast Maximum Flow (最大流模板)
查看>>
ECMAScript面向对象(二)——之创建对象方法总结
查看>>
git实践:对比svn
查看>>
1 管理入门
查看>>
C#递归遍历指定目录下的所有文件(包括子目录下的文件)
查看>>
SpringMVC的工作流程
查看>>
JS比较好用的一些方法搜集
查看>>
React Native导航器之react-navigation使用
查看>>
百度2016笔试题第一题:页面请求失败值
查看>>
实现网站图片瀑布流重点记录
查看>>
软件测试全职以及兼职平台以及薪酬报价
查看>>
Javascript:日期排班功能实现
查看>>
git push之后回滚(撤销)代码
查看>>
数组,字符串互相转化
查看>>
linux centos下配置静态ip地址
查看>>
Maven学习总结(三)——使用Maven构建项目
查看>>
Computer Vision & MultiMedia 相关国际会议汇总
查看>>
vs2008在win7系统中安装不问题
查看>>
HDU-1520 Anniversary party
查看>>