Batch scp script tool written by shell in combination with reverse CT

Source: Internet
Author: User

When deploying a task, one of the necessary processes is to send some files, such as the installation package, to a large number of servers. Although yuge's script is available, it is a python script written through the ssh and scp functions provided by the paramiko module. But now I am still in fear of python (although I have tried to learn it in my spare time), so I used a combination of shell and CT scripts, write the script tool for batch scp.

Trusted CT is used to automatically execute command line interaction tasks in linux, such as scp and ssh tasks that require manual Password Input and confirmation. With this tool, you can define the situations that may be encountered in the scp process, and then write the corresponding processing statement to automatically complete the scp operation.

You can use the apt-get or pacman package management tools in a linux environment to get the installation, or to the website of the trusted CT open-source project: http://expect.sourceforge.net.

After CT is installed, you can use the following code to complete the scp task for a single server:

Copy codeThe Code is as follows :#! /Usr/bin/CT
Set timeout 10
Set host [lindex $ argv 0]
Set username [lindex $ argv 1]
Set password [lindex $ argv 2]
Set src_file [lindex $ argv 3]
Set dest_file [lindex $ argv 4]
Spawn scp $ src_file $ username @ $ host: $ dest_file
CT {
"(Yes/no )? "
{
Send "yes \ n"
Wrong CT "* assword:" {send "$ password \ n "}
}
"* Assword :"
{
Send "$ password \ n"
}
}
Secondary CT "100%"
CT eof

Note that the first line of the Code specifies the secondary CT path, which is the same as that of the shell script. This statement specifies where the program will find the corresponding Startup Program during execution. At the beginning of the Code, the timeout time is set to 10 seconds. If an exception is not specified in the Code when the scp task is executed, the script is automatically terminated after 10 seconds.

From the beginning of the code, we can see that I have set five parameters to be manually entered for this script: the IP address, user name, password, local file path, and file path of the target host. If you save the preceding script as an expect_scp file, run the following command in shell:
./Expect_scp 192.168.75.130 root 123456/root/src_file/root/dest_file

After the preceding command is executed, the src_file file in the local/root directory will be copied to/root in host 192.168.75.130 with the username root and password 123456, the source file is also renamed as dest_file.

Spawn indicates the statement executed on the local terminal. After the statement is executed, CT capture the terminal output information and perform the corresponding operations. The (yes/no) content captured in the keep CT code is used to save the key when you access the target host for the first time. With this sentence, scp tasks reduce interruptions. The caught CT eof at the end of the Code corresponds to spawn, indicating the termination of capturing terminal output information.

With this round CT code, you can only complete scp tasks for a single remote host. If you need to implement a batch scp task, you need to write another shell script to call this round CT script.

Shell script:

Copy codeThe Code is as follows :#! /Bin/sh
List_file = $1
Src_file = $2
Dest_file = $3
Cat $ list_file | while read line
Do
Host_ip = 'echo $ line | awk '{print $1 }''
Username = 'echo $ line | awk '{print $2 }''
Password = 'echo $ line | awk' {print $3 }''
Echo "$ host_ip"
./Expect_scp $ host_ip $ username $ password $ src_file $ dest_file
Done

Three parameters are specified: the location of the list file, the path of the local source file, and the path of the target file of the remote host. Note that the list file specifies the remote host ip address, user name, and password. The information must be written in the following format:
IP username password

Separated by spaces or the tab key. multiple lines of information are required for multiple hosts, for example:
192.168.75.130 root 123456
192.168.75.131 knktc testpass

In this way, the information of the two remote hosts is specified. Note: If the remote host password contains special characters such as "$" and "#", you must add escape characters before these special characters when writing list files, otherwise, the wrong CT will enter the wrong password during execution.

For this shell script, save it as the batch_scp.sh file, and the saved expect_scp file and list file (it is defined as hosts. in the same directory, run the following command:
./Batch_scp.sh./hosts. list/root/src_file/root/destfile
With these two script files, you can simply complete the batch scp task.
In fact, it is not difficult to batch scp tasks, but it may be difficult to batch ssh tasks.

Related Article

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.