Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 98542abb36 | |||
| 412744520e | |||
| b95b557fc1 | |||
| 4ae4fc5e07 | |||
| ac5d6f6cf6 | |||
| 2d30be9cf9 | |||
| 58e086416b | |||
| e978d05450 | |||
| 8448282afa | |||
| 0d5cf8f506 |
@@ -1,19 +1,18 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
function showHelp() {
|
function showHelp() {
|
||||||
echo "ssh-proxy [args] command proxy"
|
echo "ssh-proxy [args] command cmd_args"
|
||||||
echo ""
|
echo ""
|
||||||
echo "args :"
|
echo "args :"
|
||||||
echo " --help Show help"
|
echo " --help Show help"
|
||||||
echo "command;"
|
echo "command :"
|
||||||
|
echo " status"
|
||||||
echo " start profile|port target"
|
echo " start profile|port target"
|
||||||
echo " stop profile|port target"
|
echo " stop port"
|
||||||
#echo " stop profile|port target|tmpFile"
|
echo "cmd_args :"
|
||||||
echo "proxy:"
|
|
||||||
echo " profile Name of the profile (~/.config/ssh-proxy/*)"
|
echo " profile Name of the profile (~/.config/ssh-proxy/*)"
|
||||||
echo " port Proxy local port"
|
echo " port Proxy local port (1024-65535)"
|
||||||
echo " target Proxy target [user@]server[:port]"
|
echo " target Proxy target [user@]server[:port]"
|
||||||
#echo " tmpFile Proxy instance pid file (/tmp/ssh-proxy/*)"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ "${1}" == "" ]; then
|
if [ "${1}" == "" ]; then
|
||||||
@@ -25,25 +24,40 @@ case ${1} in
|
|||||||
showHelp
|
showHelp
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
"start" | "stop");;
|
"status");;
|
||||||
|
"start" | "stop")
|
||||||
|
if [ "${2}" == "" ] || [[ "${2}" == */* ]]; then
|
||||||
|
showHelp
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
showHelp
|
showHelp
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
if [ "${2}" == "" ]; then
|
|
||||||
showHelp
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
function showProxyStatus() {
|
||||||
|
echo -e "Port\tProxy"
|
||||||
|
if [ -d /tmp/ssh-proxy-"${USER}" ]; then
|
||||||
|
if [ "$(find /tmp/ssh-proxy-"${USER}" -maxdepth 0 -empty -exec echo 1 \;)" == "" ]; then
|
||||||
|
for FILE in /tmp/ssh-proxy-"${USER}"/*; do
|
||||||
|
PORT=$(basename "${FILE}")
|
||||||
|
PROXY=$(grep 'PROXY=' "${FILE}" | sed 's/PROXY=//')
|
||||||
|
echo -e "${PORT}\t${PROXY}"
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function startProxy() {
|
||||||
LOCAL_PORT=0
|
LOCAL_PORT=0
|
||||||
PROXY=""
|
PROXY=""
|
||||||
|
|
||||||
if [ "${3}" == "" ]; then
|
if [ "${2}" == "" ]; then
|
||||||
#echo "Use Profile - ${2}"
|
#echo "Use Profile - ${1}"
|
||||||
##echo "Use Profile or tmpFile - ${2}"
|
#PROFILE_NAME=${1}
|
||||||
PROFILE_NAME=${2}
|
PROFILE_FILE=~/.config/ssh-proxy/${1}
|
||||||
PROFILE_FILE=~/.config/ssh-proxy/${2}
|
|
||||||
if [ ! -f "${PROFILE_FILE}" ]; then
|
if [ ! -f "${PROFILE_FILE}" ]; then
|
||||||
echo "Error: Profile doesn't exist"
|
echo "Error: Profile doesn't exist"
|
||||||
exit 1
|
exit 1
|
||||||
@@ -51,9 +65,9 @@ if [ "${3}" == "" ]; then
|
|||||||
LOCAL_PORT=$(grep 'PORT=' "${PROFILE_FILE}" | sed 's/PORT=//')
|
LOCAL_PORT=$(grep 'PORT=' "${PROFILE_FILE}" | sed 's/PORT=//')
|
||||||
PROXY=$(grep 'PROXY=' "${PROFILE_FILE}" | sed 's/PROXY=//')
|
PROXY=$(grep 'PROXY=' "${PROFILE_FILE}" | sed 's/PROXY=//')
|
||||||
else
|
else
|
||||||
# echo "Use args - ${2} ${3}"
|
# echo "Use args - ${1} ${2}"
|
||||||
LOCAL_PORT="${2}"
|
LOCAL_PORT="${1}"
|
||||||
PROXY="${3}"
|
PROXY="${2}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ! ${LOCAL_PORT} =~ ^[0-9]+$ ]] || [[ ! ${LOCAL_PORT} -lt 65536 ]]; then
|
if [[ ! ${LOCAL_PORT} =~ ^[0-9]+$ ]] || [[ ! ${LOCAL_PORT} -lt 65536 ]]; then
|
||||||
@@ -65,6 +79,7 @@ PROXY_USER=""
|
|||||||
#PROXY_PASSWORD=""
|
#PROXY_PASSWORD=""
|
||||||
PROXY_SERVER=""
|
PROXY_SERVER=""
|
||||||
PROXY_SERVER_PORT="22"
|
PROXY_SERVER_PORT="22"
|
||||||
|
PROXY_ADDRESS=""
|
||||||
|
|
||||||
if [[ "${PROXY}" == *@* ]]; then
|
if [[ "${PROXY}" == *@* ]]; then
|
||||||
PROXY_USER="${PROXY%@*}"
|
PROXY_USER="${PROXY%@*}"
|
||||||
@@ -83,49 +98,49 @@ if [[ "${PROXY_SERVER}" == *:* ]]; then
|
|||||||
PROXY_SERVER="${PROXY_SERVER%:*}"
|
PROXY_SERVER="${PROXY_SERVER%:*}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Debug Point
|
TMP_FILE=/tmp/ssh-proxy-${USER}/${LOCAL_PORT}
|
||||||
#echo "PROXY_USER: ${PROXY_USER}"
|
|
||||||
##echo "PROXY_PASSWORD: ${PROXY_PASSWORD}"
|
|
||||||
#echo "PROXY_SERVER: ${PROXY_SERVER}"
|
|
||||||
#echo "PORT: ${PROXY_SERVER_PORT}"
|
|
||||||
|
|
||||||
TMP_FILE=/tmp/ssh-proxy-$(whoami)/proxy-${LOCAL_PORT}-${PROXY_USER}@${PROXY_SERVER}
|
|
||||||
|
|
||||||
# Debug Point
|
|
||||||
#echo "${TMP_FILE}"
|
|
||||||
|
|
||||||
if [ ! -d /tmp/ssh-proxy-"${USER}" ]; then
|
if [ ! -d /tmp/ssh-proxy-"${USER}" ]; then
|
||||||
mkdir /tmp/ssh-proxy-"${USER}"
|
mkdir /tmp/ssh-proxy-"${USER}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
function startProxy() {
|
|
||||||
if [ -f "${TMP_FILE}" ]; then
|
if [ -f "${TMP_FILE}" ]; then
|
||||||
echo "Error: tmpFile exist"
|
echo "Error: tmpFile exist"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
PROXY_ADDRESS="${PROXY_SERVER}"
|
||||||
|
if [ ! "${PROXY_USER}" == "" ]; then
|
||||||
|
PROXY_ADDRESS="${PROXY_USER}@${PROXY_SERVER}"
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Start proxy ${LOCAL_PORT}"
|
echo "Start proxy ${LOCAL_PORT}"
|
||||||
if ! ssh -fND 127.0.0.1:"${LOCAL_PORT}" "${PROXY_USER}"@"${PROXY_SERVER}" -p "${PROXY_SERVER_PORT}"; then
|
if ! ssh -fND 127.0.0.1:"${LOCAL_PORT}" "${PROXY_ADDRESS}" -p "${PROXY_SERVER_PORT}"; then
|
||||||
echo "Error: Proxy start error"
|
echo "Error: Proxy start error"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
PID=$(lsof -i -P | grep ":${LOCAL_PORT} (LISTEN)" | awk -F ' ' '{print $2}')
|
PID=$(lsof -i -P | grep ":${LOCAL_PORT} (LISTEN)" | awk -F ' ' '{print $2}')
|
||||||
if [ "${PID}" == "" ]; then
|
if [ "${PID}" == "" ]; then
|
||||||
echo "Error: No PID found"
|
echo "Error: No PID found"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
{
|
{
|
||||||
if [ ! "${PROFILE_NAME}" == "" ]; then
|
#if [ ! "${PROFILE_NAME}" == "" ]; then
|
||||||
echo "PROFILE_NAME=${PROFILE_NAME}"
|
# echo "PROFILE_NAME=${PROFILE_NAME}"
|
||||||
fi
|
#fi
|
||||||
echo "LOCAL_PORT=${LOCAL_PORT}"
|
#echo "LOCAL_PORT=${LOCAL_PORT}"
|
||||||
#echo "PROXY_USER=${PROXY_USER}"
|
#echo "PROXY_USER=${PROXY_USER}"
|
||||||
echo "PROXY_SERVER=${PROXY_SERVER}"
|
#echo "PROXY_SERVER=${PROXY_SERVER}"
|
||||||
#echo "PROXY_SERVER_PORT=${PROXY_SERVER_PORT}"
|
#echo "PROXY_SERVER_PORT=${PROXY_SERVER_PORT}"
|
||||||
|
echo "PROXY=${PROXY_ADDRESS}"
|
||||||
echo "PID=${PID}"
|
echo "PID=${PID}"
|
||||||
} > "${TMP_FILE}"
|
} > "${TMP_FILE}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function stopProxy() {
|
function stopProxy() {
|
||||||
|
LOCAL_PORT=${1}
|
||||||
|
TMP_FILE=/tmp/ssh-proxy-${USER}/${LOCAL_PORT}
|
||||||
if [ ! -f "${TMP_FILE}" ]; then
|
if [ ! -f "${TMP_FILE}" ]; then
|
||||||
echo "Error: tmpFile doesn't exist"
|
echo "Error: tmpFile doesn't exist"
|
||||||
exit 1
|
exit 1
|
||||||
@@ -137,7 +152,8 @@ function stopProxy() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case ${1} in
|
case ${1} in
|
||||||
"start") startProxy;;
|
"status") showProxyStatus;;
|
||||||
"stop") stopProxy;;
|
"start") startProxy "${2}" "${3}";;
|
||||||
|
"stop") stopProxy "${2}";;
|
||||||
*) exit 1;;
|
*) exit 1;;
|
||||||
esac
|
esac
|
||||||
|
|||||||
54
completion/bash/ssh-proxy
Normal file
54
completion/bash/ssh-proxy
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
_ssh-proxy() {
|
||||||
|
# shellcheck disable=SC2034
|
||||||
|
local cur prev words cword args
|
||||||
|
_init_completion || return
|
||||||
|
|
||||||
|
local -r cmdargs="status start stop"
|
||||||
|
local -r cnfargs="--help"
|
||||||
|
local profiledir=~/.config/ssh-proxy
|
||||||
|
local tempdir="/tmp/ssh-proxy-${USER}"
|
||||||
|
|
||||||
|
if [[ ${COMP_WORDS[*]} == *"--help"* ]]; then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
args=${cmdargs}
|
||||||
|
for arg in ${cnfargs}; do
|
||||||
|
if [[ ${COMP_WORDS[*]} != *"${arg}"* ]]; then
|
||||||
|
args+=" ${arg}"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
for arg in ${cmdargs}; do
|
||||||
|
if [[ ${COMP_WORDS[*]} == *" ${arg} "* ]]; then
|
||||||
|
args=""
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
case $prev in
|
||||||
|
--help)
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
status)
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
start)
|
||||||
|
local -r PROFILELIST=$(\ls ${profiledir})
|
||||||
|
# shellcheck disable=SC2207
|
||||||
|
COMPREPLY=($(compgen -W "${PROFILELIST}" -- "${COMP_WORDS[COMP_CWORD]}"))
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
stop)
|
||||||
|
local -r PROFILELIST=$(\ls ${tempdir})
|
||||||
|
# shellcheck disable=SC2207
|
||||||
|
COMPREPLY=($(compgen -W "${PROFILELIST}" -- "${COMP_WORDS[COMP_CWORD]}"))
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
# shellcheck disable=SC2207
|
||||||
|
COMPREPLY=($(compgen -W "${args}" -- "${COMP_WORDS[COMP_CWORD]}"))
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
} && complete -F _ssh-proxy ssh-proxy
|
||||||
Reference in New Issue
Block a user