Réécriture : logiciel ssh-fwd

This commit is contained in:
Hippolyte Chauvin 2023-06-17 19:04:53 +02:00
parent 5930748fe5
commit 067d6f722d

View file

@ -1,64 +1,67 @@
#!/bin/bash -e #!/bin/bash
shopt -s expand_aliases # Alias si le terminal est kitty
[[ $TERM = xterm-kitty ]] && alias ssh='kitty +kitten ssh' if [ "$TERM" = xterm-kitty ]; then
ssh=(kitty +kitten ssh)
else
ssh=(ssh)
fi
# Variables # Variables
ssh=ssh
declare -a args declare -a args
# Display the usage # Foncitons
function usage { error() {
>&2 printf '\033[1;31m%s\033[0m \033[1m%s\033[0m\n' ERROR: "$*"
}
help() {
local name
name="$(basename "$0")"
cat << EOF cat << EOF
Usage: $(basename "$0") SSH_HOST LOCAL_PORT DISTANT_PORT [TARGET] [-- SSH_OPTIONS] ${name}: utilisation:
${name} SERVEUR PORT_LOCAL PORT_DISTANT [CIBLE] [-- ARGUMENTS_SSH...]
EOF EOF
} }
# Show an error error_help() {
function error { error "Invalid usage"
>&2 usage >&2 help
exit "${1:-1}" exit 1
} }
# Check if TCP port number is free requested_port_bridge_pid() {
port_is_free() { lsof -nP -i TCP -s TCP:LISTEN | awk '($1 == "ssh" && $9 ~ /.*:'"${1}"'/) { print $2 }' | uniq | head -n 1
! lsof -i -P -n | grep -q ':'"${1}"' (LISTEN)'
} }
# Arguments # Analyser les arguments
while [[ -n $* ]]; do while [ -n "$1" ]; do
case "$1" in case "$1" in
"--") --)
shift break
ssh_options=("$@") ;;
set --
;;
*) *)
args+=("$1") args+=("$1")
shift shift
;; ;;
esac esac
done done
# Checking the validity of the args # Il doit y avoir au moins 3 arguments dans le tableau
[[ -z ${args[2]} ]] && error 1 if [ "${#args[@]}" -lt 3 ]; then
[[ -z ${args[3]} ]] && args[3]=localhost error_help
fi
# Command building pid="$(requested_port_bridge_pid "${args[1]}")"
ssh_com=( if [ -z "$pid" ]; then
"$ssh" exec "${ssh[@]}" \
"${ssh_options[@]}" "$@" \
-f -f \
-N -N \
-L "${args[1]}:${args[3]}:${args[2]}" -L "${args[1]}:${args[3]:-localhost}:${args[2]}" \
"${args[0]}" "${args[0]}"
) else
echo "Suppression de la redirection..."
# Debug kill "$pid"
#echo "${ssh_com[@]}" fi
#exit 0
# Execution
set -x
"${ssh_com[@]}"