expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

expect脚本当中去把一台机器的文件同步到另外一台机器上去,自动同步文件

[root@100xuni1 sbin]# vim 4.expect         ##编辑脚本
写入一下内容:
#!/usr/bin/expect
set passwd "hanshuo"
spawn rsync -av [email protected]:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

加个执行权限

[root@100xuni1 sbin]# chmod a+x 4.expect

测试这个脚本

[root@100xuni1 sbin]# ./4.expect

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

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

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

指定host和要同步的文件

[root@100xuni1 sbin]# vim 5.expect     ##编辑脚本
写入一下内容:
#!/usr/bin/expect
set passwd "hanshuo"
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

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

加个执行权限

[root@100xuni1 sbin]# chmod a+x 5.expect

测试这个脚本

[root@100xuni1 sbin]# ./5.expect 192.168.63.101 "/tmp/12.txt"    ##把本地的12.txt同步到了远程

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

构建文件分发系统

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

掌握了expect的基础知识,用所学的东西构建个文件分发系统,以上是同步一个文件,我的需求同步一堆,这些文件需要写到一个文件列表里边去,我们使用rsync的files-from这个参数就能够实现把列表里边的文件给同步到远程去,那么在列表的路径必须要绝对路径

文件分发系统的实现
首先定义rsync.expect

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命
expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命


[root@100xuni1 sbin]# vim rsync.expect    ##编辑rsync.expect
写入一下内容:
#!/usr/bin/expect
set passwd "hanshuo"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -avR --files-from=$file / root@$host:/       ##大写R是如果同步机器路径不相同自动创建
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

定义file.list,这个file你可以写到tmp下名字叫file.list

[root@100xuni1 sbin]# vim /tmp/file.list              ##编辑文件列表file.list,这个文件里边是你要同步到另一台的文件
写入一下内容:
/tmp/12.txt
/root/shell/1.sh
/root/111.txt

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

定义ip.list

[root@100xuni1 sbin]# vim /tmp/ip.list    ##编辑一个ip.list里边写入你要同步文件的ip
写入以下内容:
192.168.63.101
192.168.63.104

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

创建rsync.sh编辑它

[root@100xuni1 sbin]# vim rsync.sh     ##编辑rsync.sh
写入以下内容:
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
     ./rsync.expect $ip /tmp/file.list
done

给rsync.expect执行权限

[root@100xuni1 sbin]# chmod a+x rsync.expect

执行rsync.expect

[root@100xuni1 sbin]# sh -x rsync.sh

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

批量远程执行命令

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

构建命令批量执行

编辑exe.expect

[root@100xuni1 sbin]# vim exe.expect      
写入下列内容:

#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "hanshuo"
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.expect执行权限

[root@100xuni1 sbin]# chmod a+x exe.expect

定义一个exe.sh的shell脚本

[root@100xuni1 sbin]# vim exe.sh
写入下列内容:
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
     ./exe.expect $ip "hostname"
done

执行exe.sh

[root@100xuni1 sbin]# sh exe.sh

expect脚本同步文件、expect脚本指定host和同步的文件、构建文件分发系统、批量远程执行命

扩展

shell多线程 http://blog.lishiming.net/?p=448

猜你喜欢

转载自blog.51cto.com/8043410/2285375