在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 root@192.168.2.81:/root/dir_ssh/
set timeout 3
expect {
"yes/no" {send "yes\r";exp_continue}
}
expect "root@192.168.2.81's password:"
set timeout 3
send "$password\r"
set timeout 300
send "exit\r"
expect eof