Skip to content

Instantly share code, notes, and snippets.

@ionauq
Created July 11, 2019 03:31
Show Gist options
  • Save ionauq/86eb5477662bf3ad100375cfd861e9a1 to your computer and use it in GitHub Desktop.
Save ionauq/86eb5477662bf3ad100375cfd861e9a1 to your computer and use it in GitHub Desktop.
跳板机通过scp及expect下载文件
#! /bin/zsh
# 从远程服务器下载文件
# 基于scp
# 传入路径
s_info=('IP1' 'IP2')
u_info=用户名
bastion_ip='跳板机IP'
bastion_port=跳板机端口
reply_info='-JUMPER-0'
rsa_path='/Users/用户名/.ssh/server_id_rsa'
# 下载文件路径
target_file=$1
if [ -z "$target_file" ]; then
echo "请传入需要下载的文件"
return
fi
echo "\n\n"
print $s_info
echo -n "Enter Server No.:"
read s_no
if [ -z "$s_no" ]; then
s_no=1
fi
l_info=$s_info[$s_no]
set timeout 10
/usr/bin/expect <(cat << EOF
spawn ssh -A -p ${bastion_port} -i ${rsa_path} ${u_info}@${bastion_ip}
expect "${u_info}(0)@*${reply_info}*"
send "scp -P $bastion_port $u_info@$l_info:$target_file /home/$u_info/\r"
expect "*${send_file}*100\%*"
send "exit\r"
expect "Connection to * closed."
EOF
)
file_name=${target_file##*/}
scp -P $bastion_port -i $rsa_path $u_info@$bastion_ip:/home/$u_info/$file_name ./;
echo "\n\n"
echo "~~${target_file}已下载到当前目录~~";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment