How to Use SCP to enter a password in the script

Source: Internet
Author: User

In the past, SCP files were all based on. Ssh.

However, using SCP in the script to copy files between machines becomes a problem.


The first method is CT.


The script is as follows:


#! /Usr/bin/CT-F

Spawn SCP 1 [email protected]:

Reset CT "* password :"

Send "your password \ r"


CT EOF


Of course, it's not just SCP, but other commands can also be automated using reverse CT.


Loop processing:

Spawn SCP 1 [email protected]:

For {set I 1 }{$ I <500} {incr I }{

Keep CT {"* password:" {send "Koven \ r "}

"* (Yes/no) *" {send "Yes \ r "}

}

}

Note that there is a space between braces.



The second method is to use the key file.




Assume that host a (192.168.100.3) is used to obtain the file of host B (192.168.100.4.


Run the following command on host a to generate a pair key:

Ssh-keygen-T RSA


When the prompt "Press ENTER" is displayed, the Public Key is saved to the. Ssh directory under the user directory. For example, root is stored in:


/Root/. Ssh/id_rsa.pub


Copy the id_rsa.pub file in the. Ssh directory to the ~ of host B ~ /. Ssh/directory, and renamed it authorized_keys,

Run the command in host a to establish trust with host B. For example, assume host B's IP address is 192.168.100.4 ):


SCP ~ /. Ssh/id_rsa.pub 192.168.100.4:/root/. Ssh/authorized_keys


The SCP and SSH commands can be used to obtain the files of host B without a password.

SSH 192.168.100.4 and press Enter.


Note: in fact, the id_rsa.pub content can be added to the authorized_keys of the other machine.


Bytes -----------------------------------------------------------------------------------------------------


#! /Usr/bin/CT-F


Set Password


Spawn SCP username @ target machine IP Address: Path of the copy file to store the local file

Set timeout 300

Login CT "username @ destination machine IP address's password:" # Note: The "username @ destination machine IP Address" here is consistent with the above

Set timeout 300

Send "$ password \ r"

Set timeout 300

Send "Exit \ r"


CT EOF


Appendix: SCP Parameters

-R: Copy directory

-C: compression allowed


A complete example


#! /Usr/bin/CT-F

Set Password 123456

# Download

Spawn SCP [email protected]:/root/a. wmv/home/yangyz/

Set timeout 300

Reset CT "[email protected]'s password :"

Set timeout 300

Send "$ password \ r"

Set timeout 300

Send "Exit \ r"

CT EOF


# Upload

Spawn SCP/home/yangyz/ABC. SQL [email protected]:/root/test. SQL

Set timeout 300

Reset CT "[email protected]'s password :"

Set timeout 300

Send "$ password \ r"

Set timeout 300

Send "Exit \ r"

CT EOF




########

PS: Always CT needs to be installed first


How to Use SCP to enter a password in the script

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.