dotfiles/home/private_bin/executable_ssh-serve-files

80 lines
1.3 KiB
Text
Raw Normal View History

2022-07-25 12:27:55 -05:00
#!/usr/bin/env bash
2023-01-13 22:48:30 -06:00
BOLD="\033[1m"
CYAN="\033[1;36m"
GREEN="\033[1;33m"
END="\033[0m"
2022-07-25 12:27:55 -05:00
help() {
cat <<EOF
ssh-serve-files
usage: ssh-serve-files [user@]hostname [-p port] [-d dir]
optional flags:
-h, --help show this help text
-p, --port port to tunnel and start http server at
-d, --dir directory of files to serve [default: ~/]
EOF
}
if [[ $# -eq 0 ]]; then
echo "please provide [user@]hostname"
help
exit 1
fi
2023-01-13 22:48:30 -06:00
if [[ -z $BROWSER ]]; then
echo 'please set $BROWSER'
exit 1
fi
2022-07-25 12:27:55 -05:00
PORT=8090
2023-01-13 22:48:30 -06:00
DIR='~/'
2022-07-25 12:27:55 -05:00
USER_HOST=$1
2023-01-13 22:48:30 -06:00
case $USER_HOST in
-h | --help)
help
exit 0
;;
*)
shift
;;
esac
2022-07-25 12:27:55 -05:00
while [[ $1 =~ ^- && $1 != "--" ]]; do
case $1 in
-p | --port)
shift
PORT=${PORT:+$1}
;;
-d | --dir)
shift
DIR=${DIR:+$1}
;;
2023-01-13 22:48:30 -06:00
-*)
echo "Invalid option: $1"
2022-07-25 12:27:55 -05:00
help
exit 1
;;
esac
shift
done
2022-07-28 15:57:02 -05:00
URL="http://localhost:$PORT"
2023-01-13 22:48:30 -06:00
printf "connecting to ${BOLD}%s${END} with port ${GREEN}%s${END}\n" \
"$USER_HOST" "$PORT"
2022-07-25 12:27:55 -05:00
echo "serving directory:"
2023-01-13 22:48:30 -06:00
printf " ${BOLD}->>${CYAN} %s${END}\n" "${DIR}"
2022-07-25 12:27:55 -05:00
2023-01-13 22:48:30 -06:00
printf "opening ${BOLD}%s${END} using %s\n" "$URL" "$BROWSER"
echo '---------------'
"$BROWSER" "$URL" >/dev/null 2>&1 &
2022-07-28 15:57:02 -05:00
2022-07-25 12:27:55 -05:00
# first change directory in case python<3.7
2023-01-13 22:48:30 -06:00
ssh -tL localhost:"$PORT":localhost:"$PORT" "$USER_HOST" \
2022-07-25 12:27:55 -05:00
"cd $DIR && python3 -m http.server $PORT"