第二十章、shell编程(下)

本文介绍了基于expect脚本的文件分发系统,包括远程登录、执行命令、传递参数、同步文件等功能。详细说明了各功能的脚本编写及执行方法,还阐述了构建文件分发系统的思路、核心命令和实现步骤,以及批量远程执行命令的使用场景和脚本实现。

20.27 分发系统介绍

20.28 expect脚本远程登录

20.29 expect脚本远程执行命令

20.30 expect脚本传递参数

20.31 expect脚本同步文件

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

20.33 构建文件分发系统

20.34 批量远程执行命令

20.35 扩展

 

 

20.27 分发系统介绍

shell项目---分发系统

功能:

  • 可以传输文件到大量机器;

  • 可以远程执行命令不需要输入密码;

 

 

20.28 expect脚本远程登录

 yum install -y expect      安装expect包

 

•  自动远程登录

cd  /usr/local/sbin/

vi 1.expect

#! /usr/bin/expect

#定义变量,远程登录机器的ip和密码

set host "192.168.233.129"

set passwd "1234"

#通过一种ssh方式登录机器

spawn ssh root@$host

#第一次远程ssh登录时会问是否建立连接,当遇到yes或no时,发送yes;遇到password时,发送$passwd(定义密码的变量值)

#\r表示回车

expect {

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

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

}

interact

#若没加interact,登录后会退出来,但expect命令还没结束

 

执行expect文件

chmod a+x 1.expect; ./1.expect

[root@xinlinux-03 sbin]# ./1.expect

spawn ssh root@192.168.233.129

root@192.168.233.129's password:

Last login: Fri Oct 26 15:49:08 2018 from 192.168.233.150

[root@xinlinux-01 ~]#

 

 

退出远程机器的三种方式:

  • interact表示停留在远程的机器上,不需要退出;

  • expect eof表示暂停一段时间,再退出来

  • exit 表示直接退出

 

 

20.29 expect脚本远程执行命令

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

vi 2.expect 

#!/usr/bin/expect

#定义user和password的变量

set user "root"

set passwd "1234"

#通过另一种ssh方式登录机器

spawn ssh $user@192.168.233.129

expect {

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

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

}

#普通用户登录后是]$,root用户登陆后是]#,"]*"通配,不管是普通用户还是root用户,遇到"]*"就先后执行touch、echo、exit命令

expect "]*"

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

expect "]*"

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

expect "]*"

send "exit\r"

 

执行expect文件

chmod a+x 2.expect; ./2.expect

[root@xinlinux-03 sbin]# ./2.expect

spawn ssh root@192.168.233.129

root@192.168.233.129's password:

Last login: Fri Oct 26 15:50:55 2018 from 192.168.233.150

[root@xinlinux-01 ~]# touch /tmp/12.txt

[root@xinlinux-01 ~]# echo 1212 > /tmp/12.txt

[root@xinlinux-01 ~]# [root@xinlinux-03 sbin]#

 

检查是否实验成功

./1.expect

ls -l /tmp/12.txt

cat /tmp/12.txt

[root@xinlinux-03 sbin]# ./1.expect

spawn ssh root@192.168.233.129

root@192.168.233.129's password:

Last login: Fri Oct 26 15:51:58 2018 from 192.168.233.150

[root@xinlinux-01 ~]# ls -l /tmp/12.txt

-rw-r--r-- 1 root root 5 10月 26 15:51 /tmp/12.txt

[root@xinlinux-01 ~]# cat /tmp/12.txt

1212

 

 

20.30 expect脚本传递参数

•  传递参数(就是设置内置变量)

vi 3.expect

#!/usr/bin/expect

#set定义内置变量和变量

#[lindex $argv 0]表示第一个参数,user是用户

set user [lindex $argv 0]

#[lindex $argv 1]表示第二个参数,host是ip地址

set host [lindex $argv 1]

set passwd "1234"

#[lindex $argv 2]表示第三个参数,cm表示的是命令

set cm [lindex $argv 2]

spawn ssh $user@$host

expect {

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

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

}

#$cm表示输入命令

expect "]*"

send "$cm\r"

#expect有默认超时时间(是10秒),可以设定超时时间,这里timeout是10秒

set timeout 10

expect "]*"

send "exit\r"

 

 

执行expect文件

chmod a+x 3.expect

./3.expect  root 192.168.233.129 ls

#登录机器执行ls命令(执行单条命令)

[root@xinlinux-03 sbin]# ./3.expect  root 192.168.233.129 ls

spawn ssh root@192.168.233.129

root@192.168.233.129's password:

Last login: Fri Oct 26 16:06:36 2018 from 192.168.233.150

[root@xinlinux-01 ~]# ls

1  1.txt  2_heard.txt  2.txt  anaconda-ks.cfg  test  yum.log

[root@xinlinux-01 ~]# [root@xinlinux-03 sbin]#

 

./3.expect  root 192.168.233.129  "ls;vmstat 1 1"

#登录机器执行ls、vmstat命令(执行多条命令)

 

#如果expect的终端结束expect,那远程登录的expect命令也会结束

 

 

20.31 expect脚本同步文件

• 自动同步文件

vi 4.expect

#!/usr/bin/expect

set passwd "1234"

#通过rsync方式登录同步文件

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

expect {

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

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

}

#同步文件时设置time out是行不通的

expect eof

 

执行expect文件

chmod a+x 4.expect; ./4.expect

[root@xinlinux-03 sbin]# chmod a+x 4.expect; ./4.expect

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

root@192.168.233.129's password:

receiving incremental file list

12.txt

 

sent 43 bytes  received 97 bytes  280.00 bytes/sec

total size is 5  speedup is 0.04

 

 

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

• 指定host和要同步的文件(一次只能同步一个文件)

vi 5.expect

#!/usr/bin/expect

set passwd "1234"

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

##注意,file要写绝对路径

 

执行expect文件

chmod a+x 5.expect

./5.expect 192.168.233.129  "/tmp/12.txt"

[root@xinlinux-03 sbin]# ./5.expect 192.168.233.129  "/tmp/12.txt"

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

root@192.168.233.129's password:

sending incremental file list

 

sent 45 bytes  received 12 bytes  38.00 bytes/sec

total size is 5  speedup is 0.09

 

 

20.33 构建文件分发系统

• 需求背景

对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。

• 实现思路

首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。

• 核心命令

rsync -av --files-from=list.txt  /  root@host:/

#list.txt文件列表,将要同步的大量文件写入到这里面;

#list.txt的路径要用绝对路径

 

• 文件分发系统的实现

• rsync.expect 内容

vi  rsync.expect

#!/usr/bin/expect

set passwd "1234"

set host [lindex $argv 0]

set file [lindex $argv 1]

#如果不能保证同步机器上有相对应文件的路径,可在rsync处加上-R参数

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

expect {

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

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

}

expect eof

 

创建文件列表文件file.list

#同步时要保证传输文件的路径在同步机器上有相同的路径,例如同步/tmp/12.txt,那同步机器上要有/tmp/目录

vi /tmp/file.list

例如:

/tmp/12.txt

/root/shell/1.sh

 

• ip.list内容(写远程同步机器ip)

vi /tmp/ip.list

192.168.233.129

192.168.233.132

......

#可用密钥认证,这样避免每台机器都要输入密码

 

 

• rsync.sh 内容

#将需要同步机器的ip进行分发同步文件

vi rsync.sh

#!/bin/bash

for ip in `cat ip.list`

do

    echo $ip

    ./rsync.expect $ip /tmp/file.list

done

 

•开始执行分发文件

chmod a+x  rsync.expect

sh   rsync.sh

 

20.34 批量远程执行命令

使用场景:同步完完文件后可能需要重启一些服务

 

• exe.expect 内容

vi exe.expect

#!/usr/bin/expect

#host作为第一个参数

set host [lindex $argv 0]

set passwd "1234"

#执行的命令作为第二个参数

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 内容(执行同步机器的执行命令)

vi exe.sh

#!/bin/bash

for ip in `cat /tmp/ip.list`

do

    echo $ip

    ./exe.expect $ip "hostname"

done

 

开始执行

chmod a+x exe.expect

sh exe.sh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值