Linux expect automatic SCP data with no password

Source: Internet
Author: User

Upload the backup SQL files from one server to the corporate local server but the company's local server did key verification, that is, to use key to log on to the local server, as well as the external network of the server did not do key verification, OK, then directly from that server to drag data to the local server bar.

The script is as follows:
VI Down.exp

The code is as follows Copy Code

#!/usr/bin/expect-f
Set Timeout-1
Set passwd your password.
Spawn Scp-r-p18330 root@72.11.x.x:/root/newfile/*/root/bak/
Match_max 100000
Expect-exact "Password:"
Send--"$PASSWDR"
Expect EOF

The following methods are implemented:

The code is as follows Copy Code
chmod +x Down.exp
Expect Down.exp

This allows you to copy the backup data from the public network server to the local server without entering a password.

PS: To install expect
CentOS Direct Yum-y Install expect

In this based on I need today company requirements Both original weekly down data back, but also manually to down data is the latest backup on the server, OK, in order to meet this requirement, I made some changes in the original script.

Manual Down Data script:

The code is as follows Copy Code

#!/usr/bin/expect-f
Set Timeout-1
Set passwd passwd
Spawn ssh-p18330 root@72.11.x.x
Expect {
"Yes/no" {send "YESR"; Exp_continue}
"Password:" {send "$PASSWDR"}
}
Expect "*#*" {send "Sh/root/soft_shell/copy_newfile.shr"}
Spawn Scp-r-p18330 root@72.11.x.x:/root/newfile/*/root/sqlbak/
Match_max 100000
Expect "*assword*" {send "$PASSWDR"}
Expect EOF

copy_newfile.sh Script content:

The code is as follows Copy Code

#!/bin/bash
location= "/root/sqlbak/"
Newfile= "/root/newfile"
Old_file= '/root/newfile '
Rm-f ${old_file}/*
file=$ (Find $location-type f-mtime-1)
CP $file $newfile

Explanation: Let the script log on to the server first, execute the copy_newfile.sh script, filter out the latest backup, and then automatically down to the local.
It's easy. Minor installation expect, use the following command:

The code is as follows Copy Code
Yum-y Install expect

All right, that's it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.