1
1
#! /bin/bash
2
+ # Script for easily sharing files through http, with easy to copy links provided
3
+ # Helper functions and optional argument info from http://mywiki.wooledge.org/BashFAQ/035?action=print
2
4
3
- # Makes different colored text
5
+ # Define ANSI codes for colored text
4
6
GN=" \e[32m"
5
7
RES=" \e[0m"
6
8
CYAN=" \e[1;36m"
7
9
8
- # font=Big http://www.patorjk.com/software/taag/
9
- echo -e " \n$CYAN " "
10
- _____ _ _ ______ _ _ _____
11
- | __ \ | | | | | ____(_) | / ____|
12
- | |__) | _| |_| |__ ___ _ __ | |__ _| | ___| (___ ___ _ ____ _____ _ __
13
- | ___/ | | | __| '_ \ / _ \| '_ \ | __| | | |/ _ \ ___ \ / _ \ '__\ \ / / _ \ '__|
14
- | | | |_| | |_| | | | (_) | | | | | | | | | __/____) | __/ | \ V / __/ |
15
- |_| \__, |\__|_| |_|\___/|_| |_| |_| |_|_|\___|_____/ \___|_| \_/ \___|_|
16
- __/ |
17
- |___/
18
- $RES "
19
- echo -e " Created By$GN Ac1d $RES \n"
20
- echo -e " Updated by$CYAN zweilos $RES \n"
10
+ echo -e " Created By$GN Ac1d $RES "
11
+ echo -e " Updated by$CYAN zweilosec$RES 20FEB2023\n"
12
+
13
+ # For printing usage info
14
+ show_help () {
15
+ cat << EOF
16
+ Usage: ${0##*/ } [-h] [-d DIRECTORY] [-p PORT]
17
+ Share the contents of a directory over HTTP using
18
+ python3 -m http.server.
19
+
20
+ -h display this help and exit
21
+ -d DIRECTORY directory to share files from
22
+ -p port to host the server on
23
+ EOF
24
+ }
25
+
26
+ die () {
27
+ printf ' %s\n' " $1 " >&2
28
+ exit 1
29
+ }
30
+
31
+ # Initialize all the option variables.
32
+ # This ensures we are not contaminated by variables from the environment.
33
+ port=8099
34
+ directory=" ."
35
+
36
+ while : ; do
37
+ case $1 in
38
+ -h|--help)
39
+ show_help
40
+ exit 0
41
+ ;;
42
+ -d|--dir)
43
+ if [[ -d $2 ]]; then
44
+ directory=" $2 "
45
+ shift
46
+ else
47
+ die ' ERROR: $2 is not a directory!'
48
+ fi
49
+ ;;
50
+ -p|--port)
51
+ port=$2
52
+ shift
53
+ ;;
54
+ * )
55
+ break
56
+ esac
57
+
58
+ shift
59
+ done
21
60
22
61
# list IPs associated with current hostname
23
62
HN=" hostname -I"
24
63
# put the IPs into a list
25
64
res=$( eval $HN )
26
65
arrIN=(${res// / } )
27
66
IP=" "
28
- # TODO:Make GetPort function that takes either user input or cmdline argument for custom port
29
- PORT=8099
30
67
31
68
# if there is more than one IP available, list the first two as options
32
69
# TODO: make a way to list all options
@@ -48,33 +85,32 @@ if [ ${#arrIN[@]} -gt 1 ]; then
48
85
;;
49
86
# Selecting "3" will exit the program
50
87
" Quit" )
51
- exit 0
88
+ break
52
89
;;
53
-
54
90
# Any input other than 1-3 will result in this error message
55
- * ) echo " Invalid option: $REPLY "
56
- ;;
91
+ * ) echo " Invalid option: $REPLY " ;;
57
92
esac
58
93
done
59
94
else
60
- IP=$arrIN
95
+ IP=$arrIN
61
96
62
97
fi
63
98
echo " "
64
99
echo " IP: " $IP
65
100
echo " "
66
101
echo -e " Download files using these links:\n"
67
- for entry in ` ls` ; do
102
+ # For each file in the shared directory, create a wget link
103
+ for entry in ` ls $directory ` ; do
68
104
if [ ! -d $entry ]; then
69
- wgetCmd=$( echo " wget http://${IP##* ( )} :8099 /$entry " | xargs)
105
+ wgetCmd=$( echo " wget http://${IP##* ( )} :$port /$entry " | xargs)
70
106
echo -e " \t$GN$wgetCmd$RES "
71
107
fi
72
108
done
73
109
echo " "
74
- echo -e " \nCurrent Directory Contents: "
75
- ls --color .
110
+ echo -e " \nDirectory Contents of $directory "
111
+ ls --color $directory
76
112
echo " "
77
- echo -e " \nStarting Server... "
113
+ echo -e " \nStarting Server"
78
114
79
- # Opens HTTP Server in the folder the command is run from on port $PORT
80
- python3 -m http.server $PORT -d .
115
+ # Opens HTTP file server from the specified $directory on port $port
116
+ python3 -m http.server $port -d $directory
0 commit comments