1. 安装expect
[root@centos7 ~]# yum install expect
2. 写入脚本test2.sh
[root@centos7 ~]# vi test2.sh
#!/bin/bash
ip=$1
filename=$2
expect -c"
set timeout 1
spawn scp root@${ip}:/路径/${filename} .
expect {
*yes/no* {send \"yes\r\";exp_continue}
*password:* {send \"密码\r\"}
}
interact
"
echo "Copy is successful!"
3.修改权限test2.sh
[root@centos7 ~]# chmod 755 test2.sh
3.执行test2.sh
[root@centos7 ~]# ./test2.sh ip地址 文件名
4.另一种方法,个人更喜欢test2.sh
[root@smx ~]# vi test.sh
#!/usr/bin/expect
set timeout 1
#复制文件
spawn scp root@ip地址:/路径/文件名 .
send "yes\r"
expect "*password:"
send "密码\r"
interact
[root@smx ~]# chmod 755 test.sh
[root@smx ~]# ./test.sh