20.31 expect script to synchronize files; 20.32 expect script to specify host and files to be synchronized;

20.31 expect script to synchronize files; 20.32 expect script to specify host and files to be synchronized; 20.33 build a file distribution system; 20.34 batch remote command execution

20.31 expect script to synchronize files

Automatically sync files

1. Synchronize the /tmp/12.txt file on the remote machine hao2 to the local machine /tmp/ :

[root@hao-01 ~]# vim4.expect

Added content:

#!/usr/bin/expect

set passwd "admin"

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

expect {

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

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

}expect eof

2. Increase 4.expect script x permissions :

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

3. Execute the 4.expect script ( automatically synchronize files) :

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

20.32 The expect script specifies the host and the files to be synchronized

1. Executing the script requires: specify the host (host ip) and the file to be synchronized

[root@hao-01 ~]# vim5.expect

Added content:

#!/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. Increase 5.expect script x permissions :

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

3. Execute the 5.expect script ( only one file can be synchronized at a time!) :

[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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325205092&siteId=291194637