-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_me.sh
executable file
·227 lines (177 loc) · 5.69 KB
/
upload_me.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/bin/bash
# Created by Jason Hardman for uploading files to linux servers
function usage() {
# The Usage
if [[ -n "${@}" ]]; then
echo -e "ERROR: ${@}\n"
fi
cat <<EOF
USAGE: ${0##*/} [-i | -u <user> -s <target server> [-d <target directory>] -f <the file> | *] [-F]
-i Interactive mode
-u The user with which the connection is made This is
optional, current user is assumed if not provided.
-s The remote server hostname (domain is added)
-d Which directory is being targeted? This is optional,
/home/user is assumed if not provided.
-f Which file is being uploaded?
-F Force, or don't confirm before sending (useful for scripting)
* Anything else gets you this lovely usage output.
NOTE:
The DOMAIN can be left off, hard-coded (check parameter in the main function), or added when prompted
EOF
exit 1
}
function show_prompt() {
# The prompt
if [[ "${1}" == "-d" ]]; then
local default="${2}"; shift 2
fi
local the_prompt="${@}"
while true; do
echo -n "${the_prompt}$([[ -n "${default}" ]] && echo " [${default}]"): "
read -e User_Input
if [[ -n "${User_Input}" ]]; then
break
elif [[ -n "${default}" ]]; then
User_Input="${default}";
break
fi
done
}
function get_hash ()
{
local the_f="${1}"; shift # The file
local full_p="realpath ${the_f}" # Full path of file
local full_h="$(sha256sum ${full_p})" # Hash and file
local just_h="${full_h%% *}" # Just the hash
local file_basename="$(basename ${the_f})" # Just the base filename
echo "sha256sum -c -<<<'${just_h} ${file_basename}'"
}
function upload_file() {
# Uses rsync to upload the file and returns the sha256sum
# Global variable utilized
if ! ${Force}; then # If the -F is used, then no verification is done
local User_Input="" # is used in show_prompt function
echo -e "\nUploading ${THE_FILE} to ${THE_SERVER}${THE_DOMAIN}${TARGET_DIR} as ${THE_USER}?"
show_prompt -d 'n' "Confirm to continue ('y')"
else
local User_Input="y"
fi
if [[ "${User_Input}" == "y" ]]; then
rsync -avP "${THE_FILE}" ${THE_USER}@${THE_SERVER}${THE_DOMAIN}${TARGET_DIR}
echo -e "\n$(get_hash "${THE_FILE}")\n" # buffer hash-in-sha command with newlines
else
echo ""
usage "ABORTED! Nothing was uploaded."
fi
}
function interactive_mode() {
# Get's necessary variables from user interactively
local User_Input="" # is used in show_prompt function
if [[ -z "${THE_USER}" ]]; then
show_prompt -d "$(whoami)" "The user"
THE_USER="${User_Input}"; User_Input=""
fi
if [[ -z "${THE_SERVER}" ]]; then
show_prompt "The server"
THE_SERVER="${User_Input}"; User_Input=""
fi
if [[ "${TARGET_DIR}" == ':' ]]; then
show_prompt -d ':' "The target directory"
TARGET_DIR="$( [[ "$User_Input" != ":" ]] && echo ":" )${User_Input}"; User_Input="" # Procede with ':' if value is other than ':'
fi
if [[ -z "${THE_FILE}" ]]; then
show_prompt "The file"
THE_FILE="${User_Input}"; User_Input=""
fi
if [[ ! -f "${THE_FILE}" ]]; then
usage "I can't find file: ${THE_FILE}. Are you in the right directory?"
fi
upload_file
}
function opt_2_actions() {
# Parses options and does the needful per the options
# defaults to 'false'
##JH local int_mode=false
##JH local has_user=false
##JH local has_server=false
##JH local has_t_dir=false
##JH local has_file=false
local Force=false
local OPTIND OPTION i u s d f F
while getopts "iu:s:d:f:F" OPTION; do
case "$OPTION" in
'i' ) # interactive mode
local int_mode=true
;;
'u' ) # designate a user (default will get user from 'whoami')
THE_USER="$OPTARG"
local has_user=true
;;
's' ) # designate a server
THE_SERVER="$OPTARG"
local has_server=true
;;
'd' ) # designate a target directory if there is one
TARGET_DIR=":$OPTARG"
local has_t_dir=true
;;
'f' ) # desgnate the filename
THE_FILE="$OPTARG"
local has_file=true
;;
'F' ) # Force (true if designated)
Force=true
;;
? )
usage
;;
esac
done
##JH if ${int_mode} ; then
interactive_mode
##JH else
##JH
##JH if ! ${has_user} ; then
##JH THE_USER="$(whoami)"
##JH fi
##JH
##JH if ! ${has_server} ; then
##JH usage "Where are we sending the file? Please specify a target server hostname."
##JH elif ! ${has_file} ; then
##JH usage "What are we uploading? Please specifie a file to upload."
##JH elif [[ ! -f "${THE_FILE}" ]]; then
##JH usage "I can't find file: ${THE_FILE}. Are you in the right directory?"
##JH fi
##JH
##JH upload_file
##JH
##JH fi
}
function check_domain() {
# manages domain specification
if [[ -z "${THE_DOMAIN}" ]]; then
local User_Input=""
show_prompt "No domain is hard-coded. Continue without a specified domain (y/n)?"
local ysno="${User_Input}"; User_Input=""
if [[ "${ysno}" != 'y' ]]; then
show_prompt "Specify domain with preceding \".\" (type 'EXIT' or 'CTRL+c' to quit)"
if [[ "${User_Input}" != "EXIT" ]]; then
THE_DOMAIN="${User_Input}"; unset User_Input
else
usage "Quitting with no domain!"
fi
fi
fi
}
function main() {
# The main event
THE_DOMAIN="" ## Include the "." before the domain name
check_domain
THE_USER=""
THE_SERVER=""
TARGET_DIR=':'
THE_FILE=""
opt_2_actions "${@}"
}
main ${@}