Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2752395
  • 博文数量: 587
  • 博客积分: 6356
  • 博客等级: 准将
  • 技术积分: 6410
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-23 10:54
个人简介

器量大者,福泽必厚

文章分类

全部博文(587)

文章存档

2019年(3)

2018年(1)

2017年(29)

2016年(39)

2015年(66)

2014年(117)

2013年(136)

2012年(58)

2011年(34)

2010年(50)

2009年(38)

2008年(16)

分类: LINUX

2013-06-28 16:03:05

[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

阅读(1564) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~