1
1
#! /bin/bash
2
- # (C) 30.01.2015 zhgzhg
2
+ # (C) 03.11.2016 zhgzhg
3
3
# silent mode format: sshfsmount.sh [--silent password username machine_ip_address port]
4
-
5
- # ############## configuration #####################
4
+ # semi-interactive mode format: sshfsmount.sh username machine_ip_address [port]
5
+
6
+ function help()
7
+ {
8
+ echo " Format:"
9
+ echo
10
+ echo " Interactive mode:"
11
+ echo " sshfsmount"
12
+ echo
13
+ echo " Semi-interactive mode:"
14
+ echo " sshfsmount <username> <machine_ip_address> [<port>]"
15
+ echo
16
+ echo " Silent mode:"
17
+ echo " sshfsmount --silent [<password>] [<username>] [<machine_ip_addr>] [[<port>]]"
18
+ echo " Each of the above parameters is required, but optional starting from left to"
19
+ echo " right. Not specifying it will turn off the silent mode and make sshfsmount to"
20
+ echo " ask for it. The only exception makes the port parameter."
21
+ echo
22
+ echo " sshfsmount -h | --h | --help - displays this help"
23
+ }
24
+
25
+ # #### the default configuration suggested during interactive mode #####
6
26
7
27
IPADDRESS=" 192.168.36.98"
8
28
PORT=" 22"
9
29
USERNAME=" root"
10
30
MOUNTPATH=" $HOME /sshfsmount"
11
31
REMOTEMOUNTPATH=" /"
12
32
13
- # ##################################################
33
+ # ######################################################################
14
34
15
35
IP=" "
16
36
PRT=" "
@@ -19,9 +39,17 @@ PASSWORD=""
19
39
20
40
INSILENTMODE=0
21
41
42
+ if [ -n " $1 " ]; then
43
+ if [[ " $1 " == " -h" || " $1 " == " --h" || " $1 " == " --help" ]]; then
44
+ help
45
+ exit 0
46
+ fi
47
+ fi
48
+
49
+
22
50
if [ -n " $1 " ]; then
23
51
if [ " $1 " == " --silent" ]; then
24
-
52
+
25
53
if [ -n " $2 " ]; then
26
54
PASSWORD=$2
27
55
fi
@@ -32,17 +60,27 @@ if [ -n "$1" ]; then
32
60
IP=$4
33
61
fi
34
62
if [ -n " $5 " ]; then
35
- PORT =$5
63
+ PRT =$5
36
64
fi
37
-
38
-
65
+
66
+
39
67
if [[ " $USERNM " != " " && " $IP " != " " && " $PASSWORD " != " " ]]; then
40
68
INSILENTMODE=1
41
69
fi
42
70
else
43
- echo -e " Invalid first argument! (Must be --silent)! See comments inside this script!"
44
- exit 1
45
- fi
71
+ if [[ $# -ge 2 && $# -lt 4 ]]; then
72
+ USERNM=$1
73
+ IP=$2
74
+ if [ -n " $3 " ]; then
75
+ PRT=$3
76
+ else
77
+ PRT=$PORT
78
+ fi
79
+ else
80
+ help
81
+ exit 1
82
+ fi
83
+ fi
46
84
fi
47
85
48
86
typeset RETCODE
@@ -57,15 +95,15 @@ if [ ! -d $MOUNTPATH ]; then
57
95
58
96
mkdir $MOUNTPATH > /dev/null 2>&1
59
97
RETCODE=$?
60
-
98
+
61
99
if [ $RETCODE -ne 0 ]; then
62
-
100
+
63
101
echo Fail!
64
- if [[ -f $MOUNTPATH ]]; then
102
+ if [[ -f $MOUNTPATH ]]; then
65
103
echo -e " '${MOUNTPATH} ' is a file!\nRename it or remove it from there!"
66
104
exit 1
67
105
fi
68
-
106
+
69
107
if [ " $( id -u) " != " 0" ]; then
70
108
echo [You need to run this script as root! ]
71
109
exit 1
@@ -75,23 +113,23 @@ if [ ! -d $MOUNTPATH ]; then
75
113
fi
76
114
else
77
115
echo Created!
78
- fi
116
+ fi
79
117
else
80
- echo Presented !
118
+ echo Present !
81
119
fi
82
120
83
121
echo Testing for write permissions...
84
-
122
+
85
123
TEMPWDIRTEST=" write_test_$RANDOM " ;
86
124
mkdir $MOUNTPATH /$TEMPWDIRTEST > /dev/null 2>&1
87
125
RETCODE=$?
88
126
89
127
if [ $RETCODE -ne 0 ]; then
90
128
echo Fail! You do not have write permissions in $MOUNTPATH !
91
-
129
+
92
130
if [ " $( id -u) " != " 0" ]; then
93
131
echo [Try to run this script as root! ]
94
- fi
132
+ fi
95
133
exit 1
96
134
else
97
135
echo Success!
@@ -105,7 +143,7 @@ RETCODE=$?
105
143
106
144
if [ $RETCODE -eq 127 ]; then
107
145
echo -e " You need to install sshfs!" ;
108
- echo -e " For Fedora under root run \" yum install sshfs\" and \" yum install fuse-sshfs\" ." ;
146
+ echo -e " For Fedora under root run \" dnf install sshfs\" and \" dnf install fuse-sshfs\" ." ;
109
147
echo -e " For Ubuntu run \" sudo apt-get install sshfs\" and \" sudo apt-get install fuse-utils\" ." ;
110
148
echo -e " For Mandriva run \" urpmi fuse-utils sshfs\" ." ;
111
149
exit 1
@@ -123,22 +161,22 @@ if [ $ISNOTMACOS -eq 1 ]; then
123
161
124
162
thunar -h > /dev/null 2>&1
125
163
RETCODE=$?
126
-
164
+
127
165
if [ $RETCODE -eq 127 ]; then
128
166
nautilus -h > /dev/null 2>&1
129
167
RETCODE=$?
130
-
168
+
131
169
if [ $RETCODE -eq 127 ]; then
132
170
dolphin -h > /dev/null 2>&1
133
171
RETCODE=$?
134
-
135
- if [ $RETCODE -eq 127 ]; then
172
+
173
+ if [ $RETCODE -eq 127 ]; then
136
174
nemo -h > /dev/null 2>&1
137
175
RETCODE=$?
138
-
139
- if [ $RETCODE -ne 127 ]; then
176
+
177
+ if [ $RETCODE -ne 127 ]; then
140
178
FAVOURITEFILEMANAGER=" nemo"
141
- fi
179
+ fi
142
180
else
143
181
FAVOURITEFILEMANAGER=" dolphin"
144
182
fi
@@ -195,54 +233,76 @@ else
195
233
echo -e " Set username => $USERNAME " ;
196
234
fi
197
235
236
+ MNT=" ${MOUNTPATH} /VM_${IPADDRESS} _${PORT} _${USERNAME} "
237
+ echo -e " Checking for ${MNT} ..." ;
198
238
199
- echo -e " Checking for $MOUNTPATH /VM_${IPADDRESS} _${PORT} _${USERNAME} ..." ;
200
-
201
- if [ ! -d $MOUNTPATH /VM_${IPADDRESS} _${PORT} _${USERNAME} ]; then
202
- echo Missing! Creating one...
203
- mkdir $MOUNTPATH /VM_${IPADDRESS} _${PORT} _${USERNAME}
239
+ if [ ! -d " ${MNT} " ]; then
240
+ echo Missing! Creating one...
241
+ mkdir " ${MNT} "
204
242
RETCODE=$?
205
243
if [ $RETCODE -gt 0 ]; then
206
- echo -e " Error! Cannot create that directory!" ;
207
- exit 1;
244
+ grep -qs " ${MNT} " /proc/mounts
245
+ RETCODE=$?
246
+ if [ $RETCODE -eq 0 ]; then
247
+ echo -e " Error! Cannot create that directory!"
248
+ exit 1;
249
+ else
250
+ if [ $ISNOTMACOS -eq 1 ]; then
251
+ fusermount -u " ${MNT} " > /dev/null 2>&1
252
+ else
253
+ umount " ${MNT} " > /dev/null 2>&1
254
+ fi
255
+ fi
208
256
fi
209
257
else
210
- echo Presented!
258
+ echo Present!
259
+ grep -qs " ${MNT} " /proc/mounts
260
+ RETCODE=$?
261
+ if [ $RETCODE -eq 0 ]; then
262
+ echo -e " Error! The directory is already mounted!"
263
+ exit 1;
264
+ else
265
+ if [ $ISNOTMACOS -eq 1 ]; then
266
+ fusermount -u " ${MNT} " > /dev/null 2>&1
267
+ else
268
+ umount " ${MNT} " > /dev/null 2>&1
269
+ fi
270
+ fi
211
271
fi
212
272
213
273
echo Mounting...
214
274
215
275
if [ " $PASSWORD " = " " ]; then
216
- sshfs $USERNAME @$IPADDRESS :$REMOTEMOUNTPATH $MOUNTPATH /VM_ ${IPADDRESS} _ ${PORT} _ ${USERNAME } / -C -p $PORT
276
+ sshfs $USERNAME @$IPADDRESS :$REMOTEMOUNTPATH ${MNT } / -C -p $PORT
217
277
else
218
- bash -c " echo $PASSWORD | sshfs $USERNAME @$IPADDRESS :$REMOTEMOUNTPATH $MOUNTPATH /VM_ ${IPADDRESS} _ ${PORT} _ ${USERNAME } / -C -p $PORT -o password_stdin"
278
+ bash -c " echo $PASSWORD | sshfs $USERNAME @$IPADDRESS :$REMOTEMOUNTPATH ${MNT } / -C -p $PORT -o password_stdin"
219
279
fi
220
280
RETCODE=$?
221
281
222
282
if [[ $RETCODE -ge 0 && $RETCODE -le 1 ]]; then
223
283
ANS=" " ;
224
- echo -e " \nShould be mounted under $MOUNTPATH /VM_ ${IPADDRESS} _ ${PORT} _ ${USERNAME }" ;
225
-
226
- # check if nohup is presented
284
+ echo -e " \nShould be mounted under ${MNT } " ;
285
+
286
+ # check if nohup is present
227
287
228
288
nohup --help > /dev/null 2>&1
229
289
RETCODE=$?
230
290
231
291
if [[ " $FAVOURITEFILEMANAGER " != " your favourite file manager" && $INSILENTMODE -ne 1 ]]; then
232
-
292
+
233
293
while [[ " $ANS " != " Y" && " $ANS " != " y" && " $ANS " != " N" && " $ANS " != " n" ]]; do
234
294
echo -ne " Do you want to open it with $FAVOURITEFILEMANAGER [Y/N]? " ;
235
295
read -e -n1 ANS;
236
296
done
237
297
238
298
if [[ " $ANS " = " Y" || " $ANS " = " y" ]]; then
239
-
299
+
240
300
if [ $RETCODE -ne 127 ]; then
241
- FAVOURITEFILEMANAGERCMD=" ${FAVOURITEFILEMANAGER} $MOUNTPATH /VM_ ${IPADDRESS} _ ${PORT} _ ${USERNAME} " ;
301
+ FAVOURITEFILEMANAGERCMD=" ${FAVOURITEFILEMANAGER} \" ${MNT} \" " ;
242
302
nohup bash -c " $FAVOURITEFILEMANAGERCMD &" > /dev/null 2>&1
243
303
rm nohup.out > /dev/null 2>&1
244
304
else
245
- $FAVOURITEFILEMANAGER $MOUNTPATH /VM_ ${IPADDRESS} _ ${PORT} _ ${USERNAME}
305
+ $FAVOURITEFILEMANAGER " ${MNT} "
246
306
fi
247
307
else
248
308
echo Autoopen canceled!
0 commit comments