Expect implement single and multiple server batch SCP transfer files _linux Shell

Source: Internet
Author: User
Tags ssh

Today, the company colleagues in a server on the other two servers with the computer room for file transfer, the two server files to store the same address, the password and key is consistent (our company in security to do a bit of a pervert), in order to efficiency wrote the following script.

1. Single Transmission script

cat/root/soft_shell/allscp.sh

Copy Code code as follows:

#!/usr/bin/expect
If {$ARGC < 2} {
Send_user "Usage: $ARGV 0 src_file username IP dest_file password\n"
Exit
}
# #set Key [lindex $argv 0]
Set src_file [lindex $argv 0]
Set username [lindex $argv 1]
Set HOST_IP [lindex $argv 2]
Set dest_file [lindex $argv 3]
Set password [lindex $argv 4]
# #spawn scp-i $key $src _file $username @ $host _ip: $dest _file
Spawn SCP $src _file $username @ $host _ip: $dest _file
Expect {
"(yes/no)?"
{
Send "yes\n"
Expect "Password:" {send "$password \ n"}
}
"Password:"
{
Send "$password \ n"
}
}
Expect "100%"
Expect EOF

2. Multiple Transmission scripts

cat/root/soft_shell/mainscp.sh

Copy Code code as follows:

#!/bin/bash
Host_list= "Server_list.conf"
Cat $host _list | While Read line
Todo
Host_ip= ' echo $line |awk ' {print $} '
Username= ' echo $line |awk ' {print $} '
Password= ' echo $line |awk ' {print $} '
Src_file= ' echo $line |awk ' {print $} '
Dest_file= ' echo $line |awk ' {print $} '
# #key = ' echo $line |awk ' {print $} '
##./allscp.sh $key $src _file $username $host _ip $dest _file $password
/allscp.sh $src _file $username $host _ip $dest _file $password
Done

3. Server information file


Copy Code code as follows:

Cat/root/soft_shell/server_list.conf


The format is:


Copy Code code as follows:

IP Username password source file destination file address

PS: Above 3 files, I believe everyone can see that they are placed under the same folder. My local test only use SSH password, without SSH key, if you want to use the same as our company's formal environment security (SSH password +key to log on to the server), then please modify your script file, I'm lazy and I don't have to talk about it so much.

Finally, a picture of the effect, lest people say I have a script problem.

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.