2018.10.25日,预习笔记,分发系统介绍

笔记目录

  1. 分发系统介绍
  2. expect脚本远程登录
  3. expect脚本远程执行命令
  4. expect脚本传递参数
  5. expect脚本同步文件
  6. expect脚本指定host和要同步的文件
  7. 构建文件分发系统
  8. 批量远程执行命令
  9. 扩展:

shell多线程 http://blog.lishiming.net/?p=448
给你提供一本电子书 链接:http://pan.baidu.com/s/1mg49Taw 密码:yk4b
shell习题做一下 http://www.apelearn.com/study_v2/chapter15.html#shll

 

一.分发系统介绍

  • 使用场景:网站,app,后端批量更改,上线代码
  • 其实这个分发系统就是一个上线的脚本
  • 上线代码,就是讲开发人员的代码,放入到线上的服务里面去

 

二.expect脚本远程登录

2c45a60a37d70e8412b46a94d7974424dc6.jpg

  • yum install -y 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}

"assword:" { send "$passwd\r" }

}

interact

 

三. expect脚本远程执行命令
b23f0ff83779d0168f07ea2292c990d61e4.jpg

  • 自动远程登录后,执行命令并退出

#!/usr/bin/expect

set user "root"

set passwd "123456"

spawn ssh $user@192.168.133.132

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"

 

四.expect脚本传递参数

bc4b278374e38d0dd16d585f82ea0f917ed.jpg

  • 传递参数

#!/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"

 

五.expect脚本同步文件

c562b29ec8db1b1632ef82a92c383965ab0.jpg

  • 自动同步文件

#!/usr/bin/expect

set passwd "123456"

spawn rsync -av root@192.168.133.132:/tmp/12.txt /tmp/

expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect eof

 

六.expect脚本指定host和要同步的文件

5e4547582d9c06d03bf1631b6a02894394d.jpg

  • 指定host和要同步的文件

#!/usr/bin/expect

set passwd "123456"

set host [lindex $argv 0]

set file [lindex $argv 1]

spawn rsync -av $file root@$host:$file

expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect eof

 

七.构建文件分发系统

521534349d1d3f764bb634df81f174add11.jpg

105516d9abaa013cffece7154abc988c017.jpg

02280c20574447707f5048abee84dd153a8.jpg

  • 需求背景 对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。  
  • 实现思路 首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。  
  • 核心命令 rsync -av --files-from=list.txt / root@host:/
  • 文件分发系统的实现  
  • rsync.expect 内容

#!/usr/bin/expect

set passwd "123456"

set host [lindex $argv 0]

set file [lindex $argv 1]

spawn rsync -av --files-from=$file / root@$host:/

expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect eof  

  • ip.list内容

192.168.133.132

192.168.133.133 ......

  • rsync.sh 内容

#!/bin/bash

for ip in `cat ip.list`

do    

echo $ip    

./rsync.expect $ip list.txt

done

 

八.批量远程执行命令

dfd3047bbfeb1bf7e989cc1bc63ddc6b62a.jpg

3443b7de449e3989835af2adf408215ac5a.jpg

  • exe.expect 内容

#!/usr/bin/expect

set host [lindex $argv 0]

set passwd "123456"

set cm [lindex $argv 1]

spawn ssh root@$host

expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect "]*"

send "$cm\r"

expect "]*"

send "exit\r"

  • exe.sh 内容

#!/bin/bash

for ip in `cat ip.list`

do    

echo $ip    

./exe.expect $ip "w;free -m;ls /tmp"

done

 

 

转载于:https://my.oschina.net/u/3912766/blog/2253167

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值