Expect batch SCP script

Source: Internet
Author: User

Experimental environment

1. Local Host

192.168.1.17


2. Remote Host

192.168.1.18

192.168.1.19


Experimental objectives

Use expect non-interactive scripting with SCP commands to implement SCP bulk transfer of local files to a remote host.


Scripting considerations

1. Ensure that the local host is installed expect

2. The target host does not need to enter "yes" when the SCP is not the first time to login

3. Note The order of the list files and variables

############################################################################################

Specific operation

1. Create the required transfer files and list files Iplist.txt

[Email protected] sh]# echo "HI" >/tmp/hi.txt

[Email protected] sh]# echo "Hello" >/home/hello.txt

[email protected] sh]# cat Iplist.txt

Remote IP remote password local file destination path #注意: This line does not appear

192.168.1.18123456/tmp/hi.txt/tmp

192.168.1.19654321/home/hello.txt/Home


2. Create an execution script auto_ssh.sh

[email protected] sh]# cat auto_ssh.sh

#!/bin/bashfor i in ' awk ' {print $} ' iplist.txt ' dox= ' awk/${i}/' {print $} ' iplist.txt ' y= ' awk/${i}/' {print $} ' iplist.t XT ' z= ' awk/${i}/' {print $4} ' iplist.txt './expect.sh ${i} ${x} ${y} ${z}done


3. Create a expect script expect.sh

[email protected] sh]# cat expect.sh

#!/usr/bin/expect-f

Set Timeout-1

Set DIP [lindex $argv 0]

Set PASS [lindex $argv 1]

Set src_file [lindex $argv 2]

Set dest_file [lindex $argv 3]


Spawn SCP $SRC _file $DIP: $DEST _file

Expect "Password:" {

Send "$PASS \ n"

}

Expect "100%"

Expect EOF

--------------------------------------------------

Code when #需要输入 "Yes"

Spawn SCP $SRC _file $DIP: $DEST _file

Expect {

"(yes/no)?"

{

Send "yes\n"

Expect "Password:" {

Send "$PASS \ n"

}

}

}

Expect "100%"

Expect EOF

----------------------------------------------------


4. Authorization

[Email protected] sh]# chmod 755 auto_ssh.sh expect.sh


5. Execute Script auto_ssh.sh

[Email protected] sh]#./auto_ssh.sh

Spawn Scp/tmp/hi.txt 192.168.1.18:/tmp

[email protected] ' s password:

Hi.txt 100% 3 0.0kb/s 00:00

Spawn Scp/home/hello.txt 192.168.1.19:/home

[email protected] ' s password:

Hello.txt 100% 6 0.0kb/s 00:00

[Email protected] sh]#


6. Remote host authentication

192.168.1.18

[Email protected] ~]# Cat/tmp/hi.txt

Hi


192.168.1.19

[Email protected] ~]# Cat/home/hello.txt

Hello


############################################################################################

Summarize

Although the above method can achieve the purpose of SCP bulk copying, but it is cumbersome, and does not combine if judgment and for Loop statement, the efficiency is not high.


Expect batch SCP script

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.