安装

1
yum -y install expect

使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/expect
# 本脚本需要安装 
# yum -y install expect
# yum install rsync -y

# 该脚本的作用为将指定名称的文件同步到其他节点
# params
# fname: 要同步的文件名
# hostip:目的节点
# user:目的节点用户名
# passwd:目的节点密码

# 设置变量
set fname [lindex $argv 0]
set hostip  [lindex $argv 1]
set user [lindex $argv 2]
set passwd [lindex $argv 3]
puts "fname=$fname"
puts "hostip=$hostip"
puts "user=$user"
puts "passwd=$passwd"

spawn rsync -rvl $fname $user@$hostip:/root/
expect {
 "*yes/no" {send "yes\r";exp_continue}
 "*password*:" {send "$passwd\r"}
}

expect eof