A batch scp script tool written by shell combined with expect

    expect is used to automate command line interaction tasks in the Linux environment, such as scp, ssh and other tasks that require users to manually enter passwords and then confirm them. With this tool, define the situations that may be encountered during the scp process, and then write the corresponding processing statements, the scp operation can be automatically completed. 

    If you need the expect tool, you can use apt-get or yun install expect in the Linux environment to obtain and install it, or go to the website of the expect open source project: http://expect.sourceforge.net/  to obtain it. 

    After installing expect, you can try the following code to complete the scp task to a single server: 

 

#!/usr/bin/expect  
set timeout 30
set host [lindex $argv 0]
set port [lindex $argv 1]
set username [lindex $argv 2]
set password [lindex $argv 3]
set src_file [lindex $argv 4]
set dest_file [lindex $ argv 5]
spawn scp -P $port $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 at the beginning of the code specifies the path of expect, which is the same as the shell script. This sentence specifies where the program will look for the corresponding startup program when it is executed. At the beginning of the code, the timeout time is also set to 30 seconds. If an exception that is not specified in the code is encountered during the execution of the scp task, the execution of the script will be automatically terminated after waiting for 30 seconds.

 

    As can be seen from the first few lines of the above code, I have set 6 parameters for this script that need to be entered manually, namely: IP of the target host, ssh port, username, password, local file path, and the file path. If you save the above script as an expect_scp file, you need to enter the command according to the following specification when executing it under the shell:
./expect_scp 192.168.1.10 22 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 /root in the host 192.168.1.10 with the username root and password 123456 , and the source file will be renamed dest_file.

    spawn represents a statement executed on the local terminal. After the statement starts to execute, expect starts to capture the output information of the terminal, and then makes the corresponding operation. The captured (yes/no) content in the expect code is used to complete the operation of saving the key when accessing the target host for the first time. With this sentence, the task of scp reduces the interruption of the situation. The expect eof at the end of the code corresponds to spawn, indicating the termination of capturing terminal output information.

    With this expected code, only the scp task to a single remote host can be completed. If you need to implement batch scp tasks, you need to write another shell script to call this expect script:

 

#!/bin/sh
list_file=$1
src_file=$2
dest_file = $ 3
cat $list_file | while read line
do
   host=`echo $line | awk '{print $1}'`
   port=`echo $line | awk '{print $2}'`
   username=`echo $line | awk '{print $3}'`
   password=`echo $line | awk '{print $4}'`
   echo "$host"
   ./expect_scp $host $port $username $password $src_file $dest_file
done

    Three parameters are specified: the location of the listing file, the local source file path, and the remote host target file path. It should be noted that the list file specifies the remote host ip, ssh port, user name, and password. These information need to be written in the following format:

host port username password

The information of multiple hosts needs to be written in multiple lines, such as:
192.168.75.130 22 root 123456
192.168.75.131 10022 knktc testpass

This specifies the information of the two remote hosts. Note that if there are special characters such as "$" and "#" in the remote host password, you need to add escape characters before these special characters when writing the list file, otherwise expect will enter the wrong password during execution.

For this shell script, save it as the batch_scp.sh file, and put it in the same directory as the expect_scp file and the list file (just define it as the hosts.list file) just saved. When executing, 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 task of batch scp.
In fact, the task of batch scp is not difficult, but the task of batch ssh may encounter trouble.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326478949&siteId=291194637