#!/usr/bin/expect -f
set password 密码
spawn scp 用户名@目标机器ip:拷贝文件的路径 存放本地文件的路径
set timeout 300
expect "用户名@目标机器ip's password:" #注意:这里的“用户名@目标机器ip” 跟上面的一致
set timeout 300
send "$password\r"
set timeout 300
send "exit\r"
expect eof
附:scp参数
-r:拷贝目录
-c:允许压缩
一个完整的例子
#!/usr/bin/expect -f
set password 123456
#download
spawn scp root@192.168.1.218:/root/a.wmv /home/yangyz/
set timeout 300
expect "root@192.168.1.218's password:"
set timeout 300
send "$password\r"
set timeout 300
send "exit\r"
expect eof
#upload
spawn scp /home/yangyz/abc.sql root@192.168.1.218:/root/test.sql
set timeout 300
expect "root@192.168.1.218's password:"
set timeout 300
send "$password\r"
set timeout 300
send "exit\r"
expect eof
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1697005