## 背景
在日常开发或者运维中,经常会遇到开发环境与线上环境网络隔离,需要通过跳板机连接的场景,如果需要将目标机器上的定位信息搬迁到开发机做进一步排查时,经常取文件比较费劲,一般操作是将目标文件拷贝到跳板机,再从跳板机拷贝到开发机。本文介绍两种好用的方式。
## 解决办法
### 办法一:编写脚本实现通过跳板机拷贝
- 以下脚本实现了通过跳板机取目标服务器的文件,支持正则表达式匹配。
- tips1:跳板机和目标服务器的密码作为命令行参数传递,未加密。
- tips2:如果sshpass命令不可用,先到跳板机上安装一下yum install sshpass
#!/bin/bash
# 拷贝远程目录下后缀为gz的文件到本地
# ./get_trace.sh -jump_ip xx.xx.xx.xx -jump_pwd xxx -remote_ip xx.xx.xx.xx -remote_pwd xxx -remote_path /root/logs/ -remote_file_suffix gz -local_path /home/bigdisk/05-test/copy_remote/logs/
# 拷贝远程目录/root/logs/下0号pod的所有日志内容到本地
#./get_trace.sh -jump_ip xx.xx.xx.xx -jump_pwd xxx -remote_ip xx.xx.xx.xx -remote_pwd xxx -remote_path /root/logs/ -pod_id 0 -local_path /home/bigdisk/05-test/copy_remote/logs/
# 拷贝远程目录下目录的正则表达式符合.*/i-.*-0的目录下所有内容到本地
# ./get_trace.sh -jump_ip xx.xx.xx.xx -jump_pwd xxx -remote_ip xx.xx.xx.xx -remote_pwd xxx -remote_path /root/logs/ -remote_path_regex .*/i-.*-0 -local_path /home/bigdisk/05-test/copy_remote/logs/
# 跳板机和远程服务器的配置
JUMP_SERVER_USER="root"
JUMP_SERVER="xx.xx.xx.xx" # 改成跳板机IP
JUMP_SERVER_PWD=""
REMOTE_SERVER_USER="root"
REMOTE_SERVER=""
REMOTE_SERVER_PWD=""
REMOTE_PATH_REGEX=""
REMOTE_FILE_SUFFIX=""
REMOTE_PATH="/root/logs/" # 支持正则表达式,例如:
LOCAL_PATH="./"
# 创建本地路径,如果它不存在的话
# mkdir -p "$LOCAL_PATH"
# ###################### script started ######################
while [ $# -gt 0 ]; do
key="$1"
case $key in
-h)
shift
echo "use like this: -jump_ip xx.xx.xx.xx -jump_pwd xxxxx -remote_ip xx.xx.xx.xx -remote_pwd xxxxx -remote_path /path/to/remote/directory/* -local_path /path/to/local/directory"
echo "or use like this: -jump_ip xx.xx.xx.xx -jump_pwd xxxxx -remote_ip xx.xx.xx.xx -remote_pwd xxxxx -remote_path_regex .*/i-.*-19 -local_path /path/to/local/directory"
;;
-jump_ip)
shift
JUMP_SERVER="$1"
echo "jump_ip is ${JUMP_SERVER}"
;;
-jump_pwd)
shift
JUMP_SERVER_PWD="$1"
;;
-remote_ip)
shift
REMOTE_SERVER="$1"
echo "remote_ip is ${REMOTE_SERVER}"
;;
-remote_pwd)
shift
REMOTE_SERVER_PWD="$1"
;;
-remote_path)
shift
REMOTE_PATH="$1"
echo "remote_path is ${REMOTE_PATH}"
;;
-pod_id)
shift
REMOTE_POD_ID="$1"
REMOTE_PATH_REGEX=".*/i-.*-$REMOTE_POD_ID"
echo "remote_pod id is ${REMOTE_POD_ID}, remote_path_regex is ${REMOTE_PATH_REGEX}"
;;
-remote_path_regex)
shift
REMOTE_PATH_REGEX="$1"
echo "remote_path_regex is ${REMOTE_PATH_REGEX}"
;;
-remote_file_suffix)
shift
REMOTE_FILE_SUFFIX="$1"
echo "remote_file_suffix is ${REMOTE_FILE_SUFFIX}"
;;
-local_path)
shift
LOCAL_PATH="$1"
echo "local_path is ${LOCAL_PATH}"
;;
*)
echo "ERROR: unknown option \"$key\""
echo
usage
exit 1
;;
esac
shift
done
# 使用 sshpass 和 SSH ProxyCommand 通过跳板机获取文件
if [ -z "$REMOTE_PATH_REGEX" ]; then
echo "remote_path_regex is empty"
else
echo "remote_path is ${REMOTE_PATH}/${REMOTE_PATH_REGEX}"
REMOTE_PATH=$(sshpass -p ${REMOTE_SERVER_PWD} ssh -o ProxyCommand="sshpass -p ${JUMP_SERVER_PWD} ssh -W %h:%p -o StrictHostKeyChecking=no ${JUMP_SERVER_USER}@${JUMP_SERVER}" -o StrictHostKeyChecking=no ${REMOTE_SERVER_USER}@${REMOTE_SERVER} "find ${REMOTE_PATH} -type d -regextype egrep -regex '${REMOTE_PATH_REGEX}' -print -quit")
if [ -z "$REMOTE_PATH" ]; then
echo "没有找到匹配的远程目录"
else
echo ${REMOTE_PATH}/${REMOTE_PATH_REGEX}
fi
fi
# find remote path and get all files
if [ -z "$REMOTE_FILE_SUFFIX" ]; then
echo "remote_file_suffix is empty, get all files"
sshpass -p ${REMOTE_SERVER_PWD} ssh -o ProxyCommand="sshpass -p ${JUMP_SERVER_PWD} ssh -W %h:%p -o StrictHostKeyChecking=no ${JUMP_SERVER_USER}@${JUMP_SERVER}" -o StrictHostKeyChecking=no ${REMOTE_SERVER_USER}@${REMOTE_SERVER} "tar czf - -C ${REMOTE_PATH} ." | tar xzf - -C ${LOCAL_PATH}
else
echo "remote path:${REMOTE_PATH} remote_file_suffix is ${REMOTE_FILE_SUFFIX}, get all this type files"
sshpass -p ${REMOTE_SERVER_PWD} ssh -o ProxyCommand="sshpass -p ${JUMP_SERVER_PWD} ssh -W %h:%p -o StrictHostKeyChecking=no ${JUMP_SERVER_USER}@${JUMP_SERVER}" -o StrictHostKeyChecking=no ${REMOTE_SERVER_USER}@${REMOTE_SERVER} "cd ${REMOTE_PATH} && tar czf - -C ${REMOTE_PATH} -- *.${REMOTE_FILE_SUFFIX}" | tar xzf - -C ${LOCAL_PATH}
fi
### 办法二:安装软件WindTerm
- github地址:GitHub - kingToolbox/WindTerm: A professional cross-platform SSH/Sftp/Shell/Telnet/Serial terminal.A professional cross-platform SSH/Sftp/Shell/Telnet/Serial terminal. - kingToolbox/WindTermhttps://github.com/kingToolbox/WindTerm
- 方法:1)先创建跳板机的ssh连接 2)再创建远程服务器的ssh,同时选择代理链接为跳板机。