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

20.31 expect脚本同步文件;20.32 expect脚本指定host和要同步的文件;20.33 构建文件分发系统;20.34 批量远程执行命令shell

20.31 expect脚本同步文件
vim

自动同步文件安全

1. 同步远程机器hao2/tmp/12.txt文件 到本机/tmp/下 :bash

[root@hao-01 ~]# vim 4.expectssh

添加内容:ide

#!/usr/bin/expect网站

set passwd "admin"spa

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

expect {ip

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

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

}expect eof

2. 增长4.expect脚本x权限

[root@hao-01 ~]# chmod a+x 4.expect

3. 执行4.expect脚本(自动同步文件)

[root@hao-01 ~]# ./4.expect

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

1. 执行脚本须要:指定host(主机ip)和要同步的文件

[root@hao-01 ~]# vim 5.expect

添加内容:

#!/usr/bin/expect

set passwd "admin"

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

2. 增长5.expect脚本x权限

[root@hao-01 ~]# chmod a+x 5.expect

3. 执行5.expect脚本(执行一次只能同步一个文件!)

[root@hao-01 ~]# ./5.expect 192.168.211.128 "tmp/12.txt"

20.33 构建文件分发系统

shell项目-分发系统-构建文件分发系统

1. 需求背景对于大公司而言,确定时不时会有网站或者配置文件更新,并且使用的机器确定也是好多台,少则几台,多则几十甚至上百台。因此,自动同步文件是相当重要的。

2. 实现思路首先要有一台模板机器,把要分发的文件准备好,而后只要使用expect脚本批量把须要同步的文件分发到目标机器便可。

3. 核心命令rsync -av --files-from=list.txt  /  root@host:/

文件分发系统的实现

1. 建立rsync.expect核心脚本 :

(从本地/根目录  到远程/根目录)

[root@hao-01 ~]# vim rsync.expect

(远程ip机器密码须要和本地一致;也能够作用户认证,便可不用密码一致,安全)

添加内容:

#!/usr/bin/expect

set passwd "admin"

set host [lindex $argv 0]

set file [lindex $argv 1]

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

expect {

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

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

}

expect eof

2. 建立本地同步文件列表文件

(本地同步远程机器文件;绝对路径)

[root@hao-01 ~]# vim /tmp/file.list

添加内容:

/tmp/12.txt

/root/1.txt

3. 建立远程机器ip列表文件

(远程ip机器密码须要和本地一致;也能够作用户认证,便可不用密码一致,安全)

[root@hao-01 ~]# vim /tmp/ip.list

添加内容:

192.168.211.129

127.0.0.1

4. 建立 rsync.sh循环脚本 :

(对应指定: 同步到远程机器ip列表文件 本地同步文件列表文件)

[root@hao-01 ~]# vim rsync.sh

添加内容:

#!/bin/bash

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

do

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

done

5. 增长rsync.sh脚本x权限

[root@hao-01 ~]# chmod a+x rsync.sh

6. 执行rsync.sh脚本

[root@hao-01 ~]# sh -x rsync.sh

20.34 批量远程执行命令

1. 建立exe.expect核心脚本:

[root@hao-01 ~]# chmod a+x exe.expect

添加内容:

#!/usr/bin/expect

set host [lindex $argv 0]

set passwd "admin"

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"

2. 增长exe.expec脚本x权限

[root@hao-01 ~]# chmod a+x exe.expec

3. 建立exe.sh脚本 :

(设定 访问远程机器执行的命令)

[root@hao-01 ~]# vim exe.sh

#/bin/bash

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

do

   ./exe.expect $ip "ls"

done

4. 执行exe.sh脚本 :

[root@hao-01 ~]# sh exe.sh

clipboard.png

相关文章
相关标签/搜索