expect脚本同步文件

分发系统(下)_第1张图片

1.创建脚本:

[root@weix-01 sbin]# vi 4.expect
#!/usr/bin/expect
set passwd "w14"
spawn rsync -av [email protected]:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

2.添加权限并执行:

[root@weix-01 sbin]# chmod a+x 4.expect
[root@weix-01 sbin]# ./4.expetc

分发系统(下)_第2张图片

3.修改脚本:

[root@weix-01 sbin]# vi 5.expect
#!/usr/bin/expect
set passwd "we14"
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

4.修改权限并执行:

[root@weix-01 sbin]# chmod a+x 5.expect

[root@weix-01 sbin]# ./5.expect  192.168.127.132 "/tmp/12.txt"

构建文件分发系统

分发系统(下)_第3张图片

1.创建脚本:

[root@weix-01 sbin]# vi rsync.expect
#!/usr/bin/expect
set passwd "w14"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
[root@weix-01 sbin]# vi /tmp/list.txt
/tmp/12.txt
/root/shell/1.sh
[root@weix-01 sbin]# vi /tmp/ip.txt
192.168.133.132
127.0.0.1
[root@weix-01 sbin]# vi rsync.sh
#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./rsync.expect $ip list.txt
done

2.添加权限并执行:

[root@weix-01 sbin]# chmod a+x rsync.expect
[root@weix-01 sbin]# ./rsync.expect

3.批量远程执行:

[root@weix-01 sbin]# vi exe.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "123456"
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"
[root@weix-01 sbin]# vi exe.sh
#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./exe.expect $ip "w;free -m;ls /tmp"
done