shell编程(3)

shell项目——分发系统


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

而要如何实现自动同步呢?首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。整个过程也称为代码上线。
 

expect脚本远程登录

  • 安装expect:
[root@xzl-linux:~]# yum install -y expect  //由于我们之前安装过mkpasswd这个工具,这个工具会把expect一并安装的。
  • 编辑自动远程登录expect脚本:
[root@localhost sbin]# vim login.expect          //写入下面内容
#! /usr/bin/expect
set host "192.168.33.128"           //设置变量
set passwd "19940406"               //设置变量
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue }  //‘\r’表示回车键
"password:" { send "$passwd\r" }
}
interact            //表示停留在远程机器上,而不需要退出,不加interact会退出

expect脚本远程登录,执行命令并退出

  • 编辑expect脚本:
[root@localhost sbin]# vim login2.expect         //写入下面内容
#! /usr/bin/expect
set user "root"
set passwd "19940406"
spawn ssh $user@192.168.33.128

expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$passwd\r" }
}
expect "]*"              //不管是root提示符还是普通用户,都匹配到
send "touch /tmp/12.txt\r"         //执行命令
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"

expect脚本传递参数

  • 编辑expect脚本:
[root@localhost sbin]# vim login3.expect
#! /usr/bin/expect
set user [lindex $argv 0]            //第一个参数的值赋给user
set host [lindex $argv 1]            //第二个参数的值赋给host
set passwd "19940406"
set cm [lindex $argv 2]              //第三个参数的值赋给cm,表示命令
spawn ssh $user@$host
expect {
"yes/no" { send "yes\r" }
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"

expect脚本同步文件

  • 编辑expect脚本:
[root@localhost sbin]# vim login4.expect              //写入下面内容
#! /usr/bin/expect
set passwd "19940406"
spawn rsync -av root@192.168.33.128:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r" }
"password:" { send "$passwd\r" }
}
expect eof             //表示expect脚本结束,如果不加expect eof会导致还没开始传输就会结束

如果想让expect永远不超时,可以在上面添加一行:

set timeout -1        //-1表示永不超时,设置为其它数字就会以其它数字为超时时间,以秒为单位,但是去掉expect eof 仍会出问题

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

[root@localhost sbin]# vim login5.expect               //写入下面内容
#! /usr/bin/expect
set passwd "19940406"
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

构建文件分发系统

核心命令:
rsync -av --files-from=list.txt / root@host:/            //文件路径必须是绝对路径

编辑expect脚本:

[root@localhost sbin]# vim rsync.expect                 //写入下面内容
#! /usr/bin/expect
set passwd "19940406"
set host [lindex $argv 0]
set file [lindex $argv 1]               //指的是list.txt ,表示一个列表而不仅仅是一个文件
spawn rsync -avR --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r" }
"password:" { send "$passwd\r" }
}
export eof

编辑file.list:

[root@localhost sbin]# vim /tmp/file.list               //写入下面内容
/tmp/12.txt
/tmp/1.txt
/tmp/2.txt
/tmp/3.txt
/tmp/111/111.txt

编辑ip.list:

[root@localhost sbin]# vim /tmp/ip.list                  //写入下面内容
192.168.33.128
127.0.0.1

编辑 rsync.sh:

[root@localhost sbin]# vim rsync.sh                     //写入下面内容
#! /bin/bash
for ip in `cat /tmp/ip.list`
do
        ./rsync.expect $ip /tmp/file.list
done

执行脚本:

[root@localhost sbin]# chmod a+x rsync.expect
[root@localhost sbin]# sh rsync.sh 
spawn rsync -av --files-from=/tmp/file.list / root@192.168.33.128:/
root@192.168.33.128's password: 
building file list ... done
tmp/
tmp/1.txt
tmp/2.txt
tmp/3.txt
tmp/111/
tmp/111/111.txt/

sent 311 bytes  received 82 bytes  262.00 bytes/sec
total size is 5  speedup is 0.01               //192.168.33.128传输成功
spawn rsync -av --files-from=/tmp/file.list / root@127.0.0.1:/
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.          //127.0.0.1出现问题,暂且不用管
ECDSA key fingerprint is SHA256:y/jrhxRLVYWO6YhN5a2DSQePmWH3CCVWcV578JDKbPg.
ECDSA key fingerprint is MD5:1b:86:bc:80:c5:c6:14:7a:4f:6c:29:b4:14:8f:86:d1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.

查看192.168.33.128:

[root@localhost ~]# ls -l /tmp/
总用量 4
drwxr-xr-x 3 root  root  21 8月   1 14:47 111
-rw-r--r-- 1 root  root   5 8月   1 14:00 12.txt
-rw-r--r-- 1 root  root   0 8月   1 14:47 1.txt
-rw-r--r-- 1 root  root   0 8月   1 14:47 2.txt
-rw-r--r-- 1 root  root   0 8月   1 14:47 3.txt

分发系统命令批量执行

有时候我们在批量传输完文件还不够,还需要在目标机器上执行命令来操作一些服务,这时候就需要对机器进行命令的批量执行。

  • 编辑exe.expect:
[root@localhost sbin]# vim exe.expect                    //写入下面内容
#! /usr/bin/expect
set host [lindex $argv 0]
set passwd "19940406"
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
[root@localhost sbin]# vim exe.sh                        //写入下面内容
#! /bin/bash
for ip in `cat /tmp/ip.list`
do
        ./exe.expect $ip "hostname"
done
  • 执行脚本:
[root@localhost sbin]# chmod a+x exe.expect 
[root@localhost sbin]# sh exe.sh 
spawn ssh root@192.168.33.128
root@192.168.33.128's password: 
Last login: Wed Aug  1 15:06:08 2018 from 192.168.33.129
[root@localhost ~]# hostname
localhost.localdomain            //192.168.33.128主机名
[root@localhost ~]# spawn ssh root@127.0.0.1
root@127.0.0.1's password: 
Last failed login: Wed Aug  1 15:17:30 CST 2018 from 127.0.0.1 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Wed Aug  1 13:54:06 2018 from 192.168.33.1
[root@localhost ~]# hostname
localhost.localdomain            //127.0.0.1本地主机名

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值