expect
<一>
#!/usr/bin/expect -f
set password 123456
set date [exec date "+%Y-%m-%d"]
spawn /usr/bin/scp root@1.1.1.1://backup/test-file-$date /test/backup/
expect "assword:"
send -- "$password\r"
expect "assword:"
send -- "$password\r"
expect eof
<二>
#!/usr/bin/expect -f
set password VH>\$hNiu3AUYl=GM?
#download
spawn scp -P 6022 monitor@192.168.50.153:/home/monitor/result.log /home/monitor
expect {
"(yes/no)?"
{send "yes\n"}
"password:"
{send "$password\n"}
}
expect eof
<三>
- [root@bms_mes_app01 backup]# crontab -l
*/3 * * * * /opt/message/disasterrecovery/notify_disasterrecovery.sh >> /opt/message/disasterrecovery/notify_out.log 2>&1
*/3 * * * * /opt/message/disasterrecovery/api_disasterrecovery.sh >> /opt/message/disasterrecovery/api_out.log 2>&1
*/30 * * * * sh /data/messlog/backup/auto-scp.sh
[root@bms_mes_app01 backup]# cat /data/messlog/backup/auto-scp.sh
#!/usr/bin/expect -f
set timeout 36000
set password VH>\$hNiu3AUYl=GM?
#download
set date [exec date "+%Y-%m"]
spawn scp -P 6022 /data/messlog/backup/msg_$date.txt monitor@192.168.76.50:/home/monitor/
#sleep 3600
expect {
"(yes/no)?"
{send "yes\n"}
"password:"
{send "$password\n"}
}
expect eof