[root@zabbix-server]# yum install expect -y
[root@zabbix-server script]# cat scp.sh
#! /bin/sh
source /etc/profile ######引入环境变量
source ~/.bash_profile
export pass=xxxx
export name=xxxx
expect -c "
spawn scp -r xxxx@X.X.X.X:/var/opt/gitlab/git-data/repositories/ /data/repositories
expect {
\"*assword\" {set timeout -1; send \"$pass\r\"; exp_continue;}
\"yes/no\" {send \"yes\r\";}
}
#expect eof
exit"
date +%F-%T
(2)、做定时任务
[```
root@zabbix-server script]# crontab -l
0 1 * /root/script/scp.sh >>/tmp/scp.log 2 >& 1
查看日志,可以先测试,如果手动执行时成功的,通过定时任务怎么都不成功的话,一般都是环境变量的问题,所以开头引入环境变量source /etc/profile source ~/.bash_profile
原文:https://blog.51cto.com/liqingbiao/2407913