expect脚本当中去把一台机器的文件同步到另一台机器上去,自动同步文件shell
[root@100xuni1 sbin]# vim 4.expect ##编辑脚本 写入一下内容: #!/usr/bin/expect set passwd "hanshuo" spawn rsync -av root@192.168.63.101:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof
加个执行权限vim
[root@100xuni1 sbin]# chmod a+x 4.expect
测试这个脚本bash
[root@100xuni1 sbin]# ./4.expect
指定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
加个执行权限ssh
[root@100xuni1 sbin]# chmod a+x 5.expect
测试这个脚本ide
[root@100xuni1 sbin]# ./5.expect 192.168.63.101 "/tmp/12.txt" ##把本地的12.txt同步到了远程
掌握了expect的基础知识,用所学的东西构建个文件分发系统,以上是同步一个文件,个人需求同步一堆,这些文件须要写到一个文件列表里边去,咱们使用rsync的files-from这个参数就可以实现把列表里边的文件给同步到远程去,那么在列表的路径必需要绝对路径
测试
文件分发系统的实现
首先定义rsync.expectspa
[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
定义file.list,这个file你能够写到tmp下名字叫file.list.net
[root@100xuni1 sbin]# vim /tmp/file.list ##编辑文件列表file.list,这个文件里边是你要同步到另外一台的文件 写入一下内容: /tmp/12.txt /root/shell/1.sh /root/111.txt
定义ip.list线程
[root@100xuni1 sbin]# vim /tmp/ip.list ##编辑一个ip.list里边写入你要同步文件的ip 写入如下内容: 192.168.63.101 192.168.63.104
建立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
构建命令批量执行
编辑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
shell多线程 http://blog.lishiming.net/?p=448