2018年10月25日第三十三节课笔记——shell脚本(下)

内容摘要:

  • 分发系统介绍
  • expect脚本远程登录
  • expect脚本远程执行命令
  • expect脚本传递参数
  • expect脚本同步文件
  • expect脚本指定host和要同步的文件
  • 构建文件分发系统
  • 批量远程执行命令
  • 扩展

一、分发系统介绍

当业务逐渐越来越大,服务端上传网站或者APP代码时,一旦服务器数量较多时就变的极为不方便,而公司本身并没有上线自动化运维工具时,可以使用shell脚本来编写一个分发系统,其中核心使用expect脚本语言,他可以实现远程执行命令,远程传输数据等操作。

二、expect脚本远程登录

  • 准备两台实验机器
  1. expect脚本机器 aminglinux-01  192.168.157.128
  2. 被远程的机器 aminglinux-02  19168.157.130
  • 安装expect语言:yum install -y expect
  • 自动远程登录,编辑并加入以下内容:vim expect.exp

 #! /usr/bin/expect

set host "192.168.157.130"

set passwd "123456"

spawn ssh [email protected]$host

expect {

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

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

}

interact

  1. set用于定义变量
  2. spawn登录命令
  3. expect与远程机器交互,截取特定的提示,并send(发送)对应的变量
  4. 在/root/.ssh/known_hosts文件中记录有已经ssh登录过的服务器
  5. \r表示回车
  6. exp_contunue表示继续
  7. interact表示继续交互
  8. expect eof表示停留远程机器上一会儿再退出

  • 添加执行权限:chmod a+x expect.exp
  • 执行脚本:./expect.exp

2018年10月25日第三十三节课笔记——shell脚本(下)

三、expect脚本远程执行命令

  • 自动远程登录后,执行命令并退出,编辑脚本:vim expect1.exp

#!/usr/bin/expect

set user "root"

set passwd "123456"

spawn ssh [email protected]

expect {

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

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

}

expect "]*"

send "touch /tmp/expect.txt\r"

expect "]*"

send "echo 1212 > /tmp/expect.txt\r"

expect "]*"

send "exit\r"

  • 添加执行权限:chmod a+x expect.exp
  • 执行脚本:./expect.exp

2018年10月25日第三十三节课笔记——shell脚本(下)

  1. 在aminglinux-02上查看

2018年10月25日第三十三节课笔记——shell脚本(下)

四、expect脚本传递参数

  • 编辑脚本:vim expect3.exp

#!/usr/bin/expect

set user [lindex $argv 0]

set host [lindex $argv 1]

set passwd "123456"

set cm [lindex $argv 2]

spawn ssh [email protected]$host

expect {

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

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

}

expect "]*"

send "$cm\r"

expect "]*"

send "exit\r"

  1. [lindex $argv 0]表示要输入的第一个参数 如此类推
  • 添加执行权限:chmod a+x expect.exp
  • 执行脚本:

2018年10月25日第三十三节课笔记——shell脚本(下)

  • 传递参数中有多个命令时需要用双引号,并以;分隔,例:"ls;w;pwd"
  • expect默认的超时时间10秒,当执行vmstat等命令时会在10秒时退出。可以在命令行的后面增加set timeout 来指定命令的超时时间 -1为永远

#!/usr/bin/expect

set user [lindex $argv 0]

set host [lindex $argv 1]

set passwd "123456"

set cmd [lindex $argv 2]

spawn ssh [email protected]$host

expect {

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

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

}

expect "]*"

send "$cmd\r"

set timeout -1

expect "]*"

send "exit\r"

  • 添加执行权限并测试

2018年10月25日第三十三节课笔记——shell脚本(下)

  1. 持续不断的执行vmstat命令,直到用户手动停止。

五、expect脚本同步文件

  • 编辑脚本:vim expect5.exp

#!/usr/bin/expect

set passwd "123456"

spawn rsync -av [email protected]:/tmp/expect.txt /tmp/

expect {

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

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

}

expect eof

  • 添加执行权限并执行脚本

2018年10月25日第三十三节课笔记——shell脚本(下)

  • 将192.168.157.130的expect.txt同步到本机上
  • 脚本中如果不使用expect eof 会导致传输没有完成便会退出登录。

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

  • 编辑脚本:vim expect6.exp

#!/usr/bin/expect

set passwd "123456"

set host [lindex $argv 0]

set file [lindex $argv 1]

spawn rsync -av $file [email protected]$host:$file

expect {

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

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

}

expect eof

  • 添加执行权限并执行脚本

2018年10月25日第三十三节课笔记——shell脚本(下)

七、构建文件分发系

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

#!/usr/bin/expect

set passwd "123456"

set host [lindex $argv 0]

set file [lindex $argv 1]

#无法保证远程同步的机器有相同目录时,可以加上-R选项,级联的创建目录。

spawn rsync -avR --files-from=$file / [email protected]$host:/

expect {

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

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

}

expect eof

  • 创建ip.list 同步机器的IP列表:vim ip.list

2018年10月25日第三十三节课笔记——shell脚本(下)

  • 创建file.list 同步机器的文件列表(必须是绝对路径):

2018年10月25日第三十三节课笔记——shell脚本(下)

  • 创建 rsync.sh 脚本:vim rsync.sh

#!/bin/bash

for ip in `cat ip.list`

do

    echo $ip

    ./rsync.exp $ip file.list

done

  • 添加执行权限并执行sh -x rsync.sh

2018年10月25日第三十三节课笔记——shell脚本(下)

八、批量远程执行命令

当同步完代码后有可能需要批量地重启服务,因此还需要批量远程执行命令,类似于自动化。

  • 编辑expect脚本:vim exe.exp

#!/usr/bin/expect

set host [lindex $argv 0]

set passwd "123456"

set cm [lindex $argv 1]

spawn ssh [email protected]$host

expect {

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

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

}

expect "]*"

send "$cm\r"

expect "]*"

send "exit\r"

  • 编写shell脚本:vim exe.sh

#!/bin/bash

for ip in `cat ip.list`

do

    echo $ip

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

done

  • 添加执行权限并执行脚本

2018年10月25日第三十三节课笔记——shell脚本(下)

九、扩展:

  • shell多线程: http://blog.lishiming.net/?p=448
  • shell习题做一下 http://www.apelearn.com/study_v2/chapter15.html#shll