improvements to ssh-serve-files

This commit is contained in:
Daylin Morgan 2023-01-13 22:48:30 -06:00
parent bbe08b942a
commit ed7bd87ff6

View file

@ -1,6 +1,10 @@
#!/usr/bin/env bash #!/usr/bin/env bash
#colors BOLD="\033[1m"
CYAN="\033[1;36m"
GREEN="\033[1;33m"
END="\033[0m"
help() { help() {
cat <<EOF cat <<EOF
@ -21,19 +25,27 @@ if [[ $# -eq 0 ]]; then
exit 1 exit 1
fi fi
if [[ -z $BROWSER ]]; then
echo 'please set $BROWSER'
exit 1
fi
PORT=8090 PORT=8090
DIR="~/" DIR='~/'
USER_HOST=$1 USER_HOST=$1
shift case $USER_HOST in
while [[ $1 =~ ^- && $1 != "--" ]]; do
echo checking $1
case $1 in
-h | --help) -h | --help)
help help
exit 0 exit 0
;; ;;
*)
shift
;;
esac
while [[ $1 =~ ^- && $1 != "--" ]]; do
case $1 in
-p | --port) -p | --port)
shift shift
PORT=${PORT:+$1} PORT=${PORT:+$1}
@ -42,8 +54,8 @@ while [[ $1 =~ ^- && $1 != "--" ]]; do
shift shift
DIR=${DIR:+$1} DIR=${DIR:+$1}
;; ;;
-* | --*) -*)
echo "Invalid option: $opt" echo "Invalid option: $1"
help help
exit 1 exit 1
;; ;;
@ -53,13 +65,15 @@ done
URL="http://localhost:$PORT" URL="http://localhost:$PORT"
echo "connecting to $USER_HOST with port $PORT" printf "connecting to ${BOLD}%s${END} with port ${GREEN}%s${END}\n" \
"$USER_HOST" "$PORT"
echo "serving directory:" echo "serving directory:"
echo "->>$DIR" printf " ${BOLD}->>${CYAN} %s${END}\n" "${DIR}"
echo "opening $URL using $BROWSER" printf "opening ${BOLD}%s${END} using %s\n" "$URL" "$BROWSER"
$BROWSER $URL >/dev/null 2>&1 & echo '---------------'
"$BROWSER" "$URL" >/dev/null 2>&1 &
# first change directory in case python<3.7 # first change directory in case python<3.7
ssh -tL localhost:$PORT:localhost:$PORT $USER_HOST \ ssh -tL localhost:"$PORT":localhost:"$PORT" "$USER_HOST" \
"cd $DIR && python3 -m http.server $PORT" "cd $DIR && python3 -m http.server $PORT"