歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> CentOS 7 下 通過shell + expect 實現 scp 文件(目錄)傳輸

CentOS 7 下 通過shell + expect 實現 scp 文件(目錄)傳輸

日期:2017/2/28 13:43:37   编辑:SHELL編程

在CentOS 7 下 通過shell + expect 實現 scp 文件(目錄)傳輸過程記錄, 寫的不多 一點經驗。

scp介紹:

實現終端之間文件的傳輸,即可以將本地文件發送到遠端相應目錄下,也可以將遠端目錄下的文件拷貝到當前目錄

SYNOPSIS

scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2

本地到遠端:

scp -r filelist[or file] $user@$host:/filelist2

遠端到本地

scp -r $user@$host:/filelist /filelist2

下面就來實現文件夾拷貝 本地到遠端(代碼示例)

start_check.sh

#!/bin/sh

#yum -y install expect*
SCP_DIR="/opt/dac/data/attach"
T=0

function CheckDir()
{
if [ "`ls -A $SCP_DIR`" = "" ]; then
T=15
sleep $T
else
T=5;
expect ./expect_scp.exp
rm -rf $SCP_DIR/*
sleep $T
fi
}

while [ 1 ] ; do
CheckDir
done

expect_scp.exp

#!/usr/bin/expect -f

set password tips123
set DIR_SCP /opt/dac/data/attach

#upload remote host
spawn scp -r $DIR_SCP [email protected]:/root/dir_ssh/
set timeout 3
expect {
"yes/no" {send "yes\r";exp_continue}
}
expect "[email protected]'s password:"
set timeout 3
send "$password\r"
set timeout 300
send "exit\r"
expect eof

Copyright © Linux教程網 All Rights Reserved