Expect bulk SCP transfer files in Linux

Source: Internet
Author: User
Tags ssh

1. Single Transmission script

The code is as follows Copy Code
cat/root/soft_shell/allscp.sh
#!/usr/bin/expect
If {$ARGC < 2} {
Send_user "Usage: $ARGV 0 src_file username IP dest_file Passwordn"
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 "YESN"
Expect "Password:" {send "$passwordn"}
}
"Password:"
{
Send "$passwordn"
}
}
Expect "100%"
Expect EOF


2. Multiple Transmission scripts

  code is as follows copy code
cat/root/soft _shell/mainscp.sh
#!/bin/bash
host_list= "server_list.conf"
Cat $host _list | While read line
do
  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 $pa ssWOrd
 /allscp.sh $src _file $username $host _ip $dest _file $password
Done


3. Server information file

The code is as follows Copy Code
Cat/root/soft_shell/server_list.conf

The format is:
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.

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.