Skip to content

Instantly share code, notes, and snippets.

@ionauq
Last active July 11, 2019 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ionauq/5bd7e84f885ab21da7cb31c9bb38b5a4 to your computer and use it in GitHub Desktop.
Save ionauq/5bd7e84f885ab21da7cb31c9bb38b5a4 to your computer and use it in GitHub Desktop.
跳板机通过scp上传文件
#! /bin/zsh
# 上传或者下载文件
# 基于scp
# 传入路径
s_info=('IP1' 'IP2')
u_info=user
bastion_ip='跳板机IP'
bastion_port=跳板机端口
reply_info='-JUMPER-0'
rsa_path='/Users/user/.ssh/server_id_rsa'
send_file=$1
if [ -z "$send_file" ]; then
echo "请输入要上传的文件"
return
fi
set timeout 10
/usr/bin/expect <(cat << EOF
spawn scp -P $bastion_port -i $rsa_path $send_file $u_info@$bastion_ip:/home/$u_info/
expect "*${send_file}*100\%*"
send "${send_file}已上传到${bastion_ip}\r"
EOF
)
echo "\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 /home/$u_info/$send_file $u_info@$l_info:/home/$u_info/\r"
expect "*${send_file}*100\%*"
send "exit\r"
expect "Connection to * closed."
EOF
)
echo "\n\n~~${send_file}已上传到${l_info}~~\n\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment