On Linux, we can easily use the SCP command to copy files remotely, but because the SCP command itself has no save password option, so in the shell can not be like samba that way to save the password, more trouble, the following two implementation methods are described below.
Method One:
SSH Public key authentication
Method Two:
Expect (expect-programmed dialogue with interactive programs, Version 5) implementation
Default installation requires manual installation of the expect package: Yum-y Install expect Expect-devel
#! /bin/bash
Expect-c "
Set timeout 1200; # #设置拷贝的时间, depending on the size of the directory, I'm here for 1200 seconds.
Spawn/usr/bin/scp-r 192.168.0.201:/work/backup/db_back//work/dbback/
Expect {
\ "*yes/no*\" {send \ "yes\r\"; Exp_continue}
\ "*password*\" {send \ "123456\r\";} # #远程IP的密码.
}
Expect EOF; "
If you do not set timeout, you will automatically exit, so you must set or use the default field to set the expect timeout or exit action.
This article is from the "Wine" blog, please be sure to keep this source http://knowledge92.blog.51cto.com/7143076/1677865
SHELL-SCP automatically enter the password