11选5定位独胆99:在shell脚本中用expect 实现 scp 文件的时候不手动输入密码

来源:百度文库 编辑:中财网 时间:2024/05/10 13:08:49
脚本如下:

#!/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