[root@s4 scripts]# cat copy.exp
#!/usr/bin/expect -f
#Author by lvs
#date is 2013-06-21
set password s******_*****@123
spawn bash -c "scp /root/dbsh/data/1* root@192.1**.1**.34:/root/db"
set timeout 2
expect {
"yes/no" {send "yes\r";exp_continue}
}
expect "Password:"
set timeout 3
send "$password\r"
set timeout 300
expect eof
spawn bash -c "scp /root/dbsh/data/2* root@192.1**.1**.42:/root/db"
set timeout 2
expect {
"yes/no" {send "yes\r";exp_continue}
}
expect "Password:"
set timeout 3
send "$password\r"
set timeout 300
expect eof
spawn bash -c "scp /root/dbsh/data/3* root@192.1**.1**.242:/root/db"
set timeout 2
expect {
"yes/no" {send "yes\r";exp_continue}
}
expect "Password:"
set timeout 3
send "$password\r"
set timeout 300
expect eof
spawn bash -c "scp /root/dbsh/data/5* root@192.1**.1**.242:/root/db"
set timeout 2
expect {
"yes/no" {send "yes\r";exp_continue}
}
expect "Password:"
set timeout 3
send "$password\r"
set timeout 300
expect eof
在命令前加上bash -c即可,否则会报文件不存在的错误,即不能解析通配符*。
###本人是写expect脚本的新手,这是第一个,留为笔记,呵呵
感谢:
http://www.cnblogs.com/codefor/archive/2011/07/21/2112503.html
执行方法:
sh *.exp这种方式是不工作的!
或另外一种工作方式:expect -f rsync.exp 也可以
expect脚本中获取日期的两种方式:
[root@db5 scripts]# cat expectscp.exp
#!/usr/bin/expect -f
#Author by lvs
#date is 2013-06-21
set password sz***z@123
set clicks [clock scan "-1 day" -base [clock seconds]]
set date [clock format $clicks -format "%y%m%d"] ##获取前一天的日期
spawn bash -c "scp /home/nfsshare/*20${date}* root@**.*.*.**:/home/dbbak"
set timeout 8
expect {
"yes/no" {send "yes\r";exp_continue}
}
expect "Password:"
set timeout 3
send "$password\r"
set timeout 1800
expect eof
还有一种方式:获取当天的日期:
[root@hy1 scripts]# cat /home/scripts/rsync_db.exp
#!/usr/bin/expect -f
#Author by lvs
#date is 2013-06-21
set password s**z@123
set date [exec date "+%Y%m%d"] ##获取当天的日期
spawn bash -c "scp /home/db_backup/*${date}* root@*.*1.*.74:/home/db_backup"
set timeout 8
expect {
"yes/no" {send "yes\r";exp_continue}
}
expect "Password:"
set timeout 3
send "$password\r"
set timeout 1800
expect eof
阅读(1620) | 评论(0) | 转发(0) |