Linux Shell + expect: Bulk SCP scripting Tools __linux

Source: Internet
Author: User
Tags python script


[Turn]http://www.knktc.com/2011/08/20/linux-shell-expect%e6%89%b9%e9%87%8fscp%e8%84%9a%e6%9c%ac%e5%b7%a5%e5%85%b7/






One of the most recent tasks in preparing a deployment 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 my spare time), so I've written this batch of SCP scripting tools 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:


1: #!/usr/bin/expect


3:set Timeout 10

4:set host [lindex $ARGV 0]

5:set username [lindex $argv 1]

6:set Password [lindex $argv 2]

7:set Src_file [lindex $argv 3]

8:set Dest_file [lindex $argv 4]


10:spawn SCP $src _file $username @ $host: $dest _file

11:expect {

     "(yes/no)?"

:         {

:             send "yes\n"

:             expect "*assword:" {send $password \ n}

:         }

     "*assword:"

:         {

:             send "$password \ n"

:         }

:     }

22:expect "100%"

23: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.



The shell script I wrote is as follows:


1: #!/bin/sh


3:list_file=$1

4:src_file=$2

5:dest_file=$3


7:cat $list _file | While Read line

8:do

9:     host_ip= ' echo $line | awk ' {print '} '

     username= ' echo $line | awk ' {print $} '

One:     password= ' echo $line | awk ' {print $} '

     echo "$host _ip"

     /expect_scp $host _ip $username $password $src _file $dest _file

15:done

 


Very simple code that specifies 3 parameters: 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, and 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 in the following manner:



./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. The bulk SSH function is learning ... (It's already available but there are still a lot of bugs)



I hope this script will help everyone, thank you.


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.