-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshfsmount.sh
327 lines (269 loc) · 7.04 KB
/
sshfsmount.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/bin/bash
# (C) 27.05.2019 zhgzhg
# silent mode format: sshfsmount.sh [--silent password username machine_ip_address port]
# semi-interactive mode format: sshfsmount.sh username machine_ip_address [port]
function help()
{
echo "Format:"
echo
echo "Interactive mode:"
echo " sshfsmount"
echo
echo "Semi-interactive mode:"
echo " sshfsmount <username> <machine_ip_address> [<port>]"
echo
echo "Silent mode:"
echo " sshfsmount --silent [<password>] [<username>] [<machine_ip_addr>] [[<port>]]"
echo "Each of the above parameters is required, but optional starting from left to"
echo "right. Not specifying it will turn off the silent mode and make sshfsmount to"
echo "ask for it. The only exception makes the port parameter."
echo
echo " sshfsmount -h | --h | --help - displays this help"
}
##### the default configuration suggested during interactive mode #####
IPADDRESS="192.168.36.98"
PORT="22"
USERNAME="root"
MOUNTPATH="$HOME/sshfsmount"
REMOTEMOUNTPATH="/"
#######################################################################
IP=""
PRT=""
USERNM=""
PASSWORD=""
INSILENTMODE=0
if [[ -n "$1" ]]; then
if [[ "$1" == "-h" || "$1" == "--h" || "$1" == "--help" ]]; then
help
exit 0
fi
fi
if [[ -n "$1" ]]; then
if [[ "$1" == "--silent" ]]; then
if [[ -n "$2" ]]; then
PASSWORD=$2
fi
if [[ -n "$3" ]]; then
USERNM=$3
fi
if [[ -n "$4" ]]; then
IP=$4
fi
if [[ -n "$5" ]]; then
PRT=$5
fi
if [[ "$USERNM" != "" && "$IP" != "" && "$PASSWORD" != "" ]]; then
INSILENTMODE=1
fi
else
if [[ $# -ge 2 && $# -lt 4 ]]; then
USERNM=$1
IP=$2
if [[ -n "$3" ]]; then
PRT=$3
else
PRT=${PORT}
fi
else
help
exit 1
fi
fi
fi
typeset RETCODE
# check if mount path exists
echo -e "Checking for '${MOUNTPATH}' directory..."
if [[ ! -d ${MOUNTPATH} ]]; then
echo Missing! Trying to create it!
mkdir "${MOUNTPATH}" >/dev/null 2>&1
RETCODE=$?
if [[ ${RETCODE} -ne 0 ]]; then
echo Fail!
if [[ -f ${MOUNTPATH} ]]; then
echo -e "'${MOUNTPATH}' is a file!\nRename it or remove it from there!"
exit 1
fi
if [[ "$(id -u)" != "0" ]]; then
echo [You need to run this script as root!]
exit 1
else
echo Cannot create ${MOUNTPATH} !
exit 1
fi
else
echo Created!
fi
else
echo Present!
fi
echo Testing for write permissions...
TEMPWDIRTEST="write_test_$RANDOM";
mkdir "${MOUNTPATH}/${TEMPWDIRTEST}" >/dev/null 2>&1
RETCODE=$?
if [[ ${RETCODE} -ne 0 ]]; then
echo Fail! You do not have write permissions in ${MOUNTPATH} !
if [[ "$(id -u)" != "0" ]]; then
echo [Try to run this script as root!]
fi
exit 1
else
echo Success!
rmdir "${MOUNTPATH}/${TEMPWDIRTEST}" >/dev/null 2>&1
fi
# check if this is Mac OS
uname -a | grep "Darwin" >/dev/null
ISNOTMACOS=$?
# check for available sshfs and fusermount executable
typeset FUSERMOUNT
sshfs -h >/dev/null 2>&1
RETCODE=$?
if [[ ${ISNOTMACOS} -eq 1 ]]; then
FUSERMOUNT="fusermount"
if [[ ${RETCODE} -ne 127 ]]; then
${FUSERMOUNT} -h >/dev/null 2>&1
RETCODE=$?
if [[ ${RETCODE} -eq 127 ]]; then
FUSERMOUNT="fusermount3"
${FUSERMOUNT} -h >/dev/null 2>&1
RETCODE=$?
fi
fi
fi
if [[ ${RETCODE} -eq 127 ]]; then
echo -e "You need to install sshfs!";
echo -e "For Fedora under root run \"dnf install sshfs\" and \"dnf install fuse-sshfs\".";
echo -e "For Ubuntu run \"sudo apt-get install sshfs\" and \"sudo apt-get install fuse-utils\".";
echo -e "For Mandriva run \"urpmi fuse-utils sshfs\".";
exit 1
fi
#check for available file managers
FAVOURITEFILEMANAGER="your favourite file manager"
if [[ ${ISNOTMACOS} -eq 1 ]]; then
thunar -h >/dev/null 2>&1
RETCODE=$?
if [[ ${RETCODE} -eq 127 ]]; then
nautilus -h >/dev/null 2>&1
RETCODE=$?
if [[ ${RETCODE} -eq 127 ]]; then
dolphin -h >/dev/null 2>&1
RETCODE=$?
if [[ ${RETCODE} -eq 127 ]]; then
nemo -h >/dev/null 2>&1
RETCODE=$?
if [[ ${RETCODE} -ne 127 ]]; then
FAVOURITEFILEMANAGER="nemo"
fi
else
FAVOURITEFILEMANAGER="dolphin"
fi
else
FAVOURITEFILEMANAGER="nautilus"
fi
else
FAVOURITEFILEMANAGER="thunar"
fi
else
FAVOURITEFILEMANAGER="open"
fi
echo -ne "\n"
if [[ ${INSILENTMODE} -ne 1 && "$IP" = "" ]]; then
echo -e "Hostname/IP Address (default $IPADDRESS): ";
read -e IP;
echo -en "\033[1A\033[2K";
fi
if [[ -n "$IP" ]]; then
echo -e "Set machine address => $IP";
IPADDRESS=${IP};
else
echo -e "Set machine address => $IPADDRESS";
fi
if [[ ${INSILENTMODE} -ne 1 && "$PRT" = "" ]]; then
echo -e "Port (default $PORT): ";
read -e PRT;
echo -en "\033[1A\033[2K";
fi
if [[ -n "$PRT" ]]; then
echo -e "Set machine port => $PRT";
PORT=${PRT};
else
echo -e "Set machine port => $PORT";
fi
if [[ ${INSILENTMODE} -ne 1 && "$USERNM" = "" ]]; then
echo -e "Username (default $USERNAME): ";
read -e USERNM;
echo -en "\033[1A\033[2K";
fi
if [[ -n "$USERNM" ]]; then
echo -e "Set username => $USERNM";
USERNAME=${USERNM};
else
echo -e "Set username => $USERNAME";
fi
MNT="${MOUNTPATH}/VM_${IPADDRESS}_${PORT}_${USERNAME}"
echo -e "Checking for ${MNT}...";
if [[ ! -d "${MNT}" ]]; then
echo Missing! Creating one...
mkdir "${MNT}"
RETCODE=$?
if [[ ${RETCODE} -gt 0 ]]; then
mount | grep -qs "${MNT}"
RETCODE=$?
if [[ ${RETCODE} -eq 0 ]]; then
echo -e "Error! Cannot create that directory!"
exit 1;
else
if [[ ${ISNOTMACOS} -eq 1 ]]; then
fusermount -u "${MNT}" >/dev/null 2>&1
else
umount "${MNT}" >/dev/null 2>&1
fi
fi
fi
else
echo Present!
mount | grep -qs "${MNT}"
RETCODE=$?
if [[ ${RETCODE} -eq 0 ]]; then
echo -e "Error! The directory is already mounted!"
exit 1;
else
if [[ ${ISNOTMACOS} -eq 1 ]]; then
${FUSERMOUNT} -u "${MNT}" >/dev/null 2>&1
else
umount "${MNT}" >/dev/null 2>&1
fi
fi
fi
echo Mounting...
if [[ "$PASSWORD" = "" ]]; then
sshfs $USERNAME@$IPADDRESS:$REMOTEMOUNTPATH ${MNT}/ -C -p $PORT
else
bash -c "echo $PASSWORD | sshfs $USERNAME@$IPADDRESS:$REMOTEMOUNTPATH ${MNT}/ -C -p $PORT -o password_stdin"
fi
RETCODE=$?
if [[ ${RETCODE} -ge 0 && ${RETCODE} -le 1 ]]; then
ANS="";
echo -e "\nShould be mounted under ${MNT}";
# check if nohup is present
nohup --help >/dev/null 2>&1
RETCODE=$?
if [[ "$FAVOURITEFILEMANAGER" != "your favourite file manager" && ${INSILENTMODE} -ne 1 ]]; then
while [[ "$ANS" != "Y" && "$ANS" != "y" && "$ANS" != "N" && "$ANS" != "n" ]]; do
echo -ne "Do you want to open it with $FAVOURITEFILEMANAGER[Y/N]? ";
read -e -n1 ANS;
done
if [[ "$ANS" = "Y" || "$ANS" = "y" ]]; then
if [[ ${RETCODE} -ne 127 ]]; then
FAVOURITEFILEMANAGERCMD="${FAVOURITEFILEMANAGER} \"${MNT}\"";
nohup bash -c "$FAVOURITEFILEMANAGERCMD &" >/dev/null 2>&1
rm nohup.out >/dev/null 2>&1
else
${FAVOURITEFILEMANAGER} "${MNT}"
fi
else
echo Autoopen canceled!
fi
else
echo -e "To open it run use $FAVOURITEFILEMANAGER."
fi
fi