Shell combines expect to write bulk SCP scripting tools _linux Shell

Source: Internet
Author: User
Tags python script

When deploying a task, one of the necessary procedures is to send some files, such as installation packages, to a large number of servers. Although there is already a Brother Woo script available: A Python script written by the SSH and SCP features provided by the Paramiko module. But I'm still in the fear of Python (though I've worked hard in my spare time), so I've written this batch SCP scripting tool using a combination of shell and expect scripts.

Expect is used to automate command-line interaction tasks in a Linux environment, such as SCP, SSH, and so on that require users to manually enter a password and then confirm it. With this tool, you define what you might encounter in the SCP process, and then write the appropriate processing statements to automate the SCP operation.

If you need expect tools, you can use Apt-get or pacman these package management tools in a Linux environment to get the installation, or to the expect open source project's website: http://expect.sourceforge.net/to get it.

After you install expect, you can try to complete the SCP task for a single server by using the following code:

#!/usr/bin/expect
set timeout
set host [lindex $ARGV 0]
set username [lindex $argv 1]
set password [Lin Dex $ARGV 2]
set src_file [lindex $argv 3]
set dest_file [lindex $argv 4]
spawn SCP $src _file $username @ $host: $dest _file
 Expect {
 "(yes/no)?"
  {
  send ' yes\n '
  expect ' *assword: ' {send ' $password \ n '}
 }
 ' *assword: '
{
 send ' $ password\n "
}
}
expect" 100% "
expect EOF


Note that the first line of code, which specifies the path of the expect, is the same as the shell script, which specifies where the program will go to find the appropriate launcher at execution time. The code initially also sets the timeout time to 10 seconds, and if you encounter an exception that is not specified in the code when you perform the SCP task, the execution of the script is automatically terminated after 10 seconds of waiting.

From the first few lines of the above code, I have set up 5 parameters that need to be manually entered for this script: IP, username, password, local file path, and file path of the target host. If you save the above script as a EXPECT_SCP file, you need to enter the command in the following specification when executing under the shell:
./EXPECT_SCP 192.168.75.130 Root 123456/root/src_file/root/dest_file

After the above command is executed, the Src_file file in the local/root directory will be copied to the/root under the user name root, password 123456, and the source file will be renamed to Dest_file.

Spawn represents the statement executed at the local terminal, after which the statement begins expect capturing the output information of the terminal and then making the corresponding action. The captured (yes/no) content in the expect code is used to save the key when the first access to the target host is done. With this, the task of the SCP reduced the interruption. The expect EOF at the end of the code corresponds to the spawn, indicating the termination of capturing terminal output information.

With this expect code, you can only complete the SCP task for a single remote host. If you need to implement a bulk SCP task, you will need to write another shell script to invoke this expect.

Shell script:

Copy Code code as follows:

#!/bin/sh
List_file=$1
Src_file=$2
Dest_file=$3
Cat $list _file | While Read line
Todo
Host_ip= ' echo $line | awk ' {print '} '
Username= ' echo $line | awk ' {print $} '
Password= ' echo $line | awk ' {print $} '
echo "$host _ip"
/EXPECT_SCP $host _ip $username $password $src _file $dest _file
Done

3 parameters were specified: The location of the list file, the local source file path, and the remote host destination file path. What you need to note is that the list file specifies the remote host IP, username, password, which needs to be written in the following format:
IP username Password

The middle space or the TAB key to separate, more than one host of information needs to write multiple lines of content, such as:
192.168.75.130 Root 123456
192.168.75.131 KNKTC Testpass

This specifies the information for both remote hosts. Note that if there are special characters such as "$" and "#" in the remote host password, you need to precede these special characters with the escape characters when writing the list file, otherwise expect will enter the wrong password when executing.

For this shell script, save it as a batch_scp.sh file, put it in the same directory as the EXPECT_SCP file and list file that you just saved (defined as Hosts.list file), and then enter the command as follows:
./ batch_scp.sh./hosts.list/root/src_file/root/destfile
With these two script files, you can simply complete the bulk of the SCP task.
In fact, the bulk of the SCP task is not difficult, but the bulk of the SSH task may encounter problems.

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.