Skip to content

Commit 429a80c

Browse files
committed
Improved help and mounting process
1 parent d382536 commit 429a80c

File tree

3 files changed

+160
-62
lines changed

3 files changed

+160
-62
lines changed

README.md

+24-8
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,32 @@ To mount drive run:
1313

1414
To mount in silent mode run (useful when you want drives to be mounted at startup; also the RSA key should have already been saved):
1515

16-
bash ./sshfsmount.sh --silent PASSWORD_OF_THE_MACHINE USERNAME ADDRESS_OF_THE_MACHINE PORT
16+
bash ./sshfsmount.sh --silent [PASSWORD_OF_THE_MACHINE] [USERNAME] [ADDRESS_OF_THE_MACHINE] [[PORT]]
17+
18+
To mount in semi-silent mode
19+
20+
bash ./sshfsmount.sh <username> <machine_ip_address> [<port>]
1721

1822
To unmount run:
1923

2024
bash ./sshfsunmount.sh
21-
25+
2226
To unmount in semi-silent mode run (useful when you want drives to be unmounted at shutdown):
2327

2428
bash ./sshfsunmount.sh --unmount FULL_DIRECTORY_PATH_TO_UNMOUNT
2529
or
2630
bash ./sshfsunmount.sh -1
2731
or
2832
bash ./sshfsunmount.sh -2
29-
33+
3034
-1 argument will unmount all the directories inside the default mount path.
3135
-2 argument will force unmount all the directories inside the default mount path.
32-
36+
3337
If you do not want to type "bash" or "sh" before the scripts use:
3438

35-
chmod +x ./sshfsmount.sh
36-
chmod +x ./sshfsunmount.sh
37-
39+
chmod a+x ./sshfsmount.sh
40+
chmod a+x ./sshfsunmount.sh
41+
3842
Installing the tools
3943
--------------------
4044

@@ -44,14 +48,26 @@ The installation requires superuser rights.
4448

4549
su -c "bash ./install.sh"
4650

51+
or
52+
53+
sudo bash ./install.sh
54+
4755

4856
Dependencies installation
4957
-------------------------
5058

51-
To install sshfs for Fedora and Cent OS (Red Hat platforms) use:
59+
To install sshfs for Fedora and Cent OS (Red Hat platforms you may need an EPEL repo) use:
5260

5361
su -
5462

63+
dnf install fuse-sshfs
64+
65+
or
66+
67+
dnf install sshfs
68+
69+
or
70+
5571
yum install fuse-sshfs
5672

5773
or

sshfsmount.sh

+105-45
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
#!/bin/bash
2-
# (C) 30.01.2015 zhgzhg
2+
# (C) 03.11.2016 zhgzhg
33
# 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 #####
626

727
IPADDRESS="192.168.36.98"
828
PORT="22"
929
USERNAME="root"
1030
MOUNTPATH="$HOME/sshfsmount"
1131
REMOTEMOUNTPATH="/"
1232

13-
###################################################
33+
#######################################################################
1434

1535
IP=""
1636
PRT=""
@@ -19,9 +39,17 @@ PASSWORD=""
1939

2040
INSILENTMODE=0
2141

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+
2250
if [ -n "$1" ]; then
2351
if [ "$1" == "--silent" ]; then
24-
52+
2553
if [ -n "$2" ]; then
2654
PASSWORD=$2
2755
fi
@@ -32,17 +60,27 @@ if [ -n "$1" ]; then
3260
IP=$4
3361
fi
3462
if [ -n "$5" ]; then
35-
PORT=$5
63+
PRT=$5
3664
fi
37-
38-
65+
66+
3967
if [[ "$USERNM" != "" && "$IP" != "" && "$PASSWORD" != "" ]]; then
4068
INSILENTMODE=1
4169
fi
4270
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
4684
fi
4785

4886
typeset RETCODE
@@ -57,15 +95,15 @@ if [ ! -d $MOUNTPATH ]; then
5795

5896
mkdir $MOUNTPATH >/dev/null 2>&1
5997
RETCODE=$?
60-
98+
6199
if [ $RETCODE -ne 0 ]; then
62-
100+
63101
echo Fail!
64-
if [[ -f $MOUNTPATH ]]; then
102+
if [[ -f $MOUNTPATH ]]; then
65103
echo -e "'${MOUNTPATH}' is a file!\nRename it or remove it from there!"
66104
exit 1
67105
fi
68-
106+
69107
if [ "$(id -u)" != "0" ]; then
70108
echo [You need to run this script as root!]
71109
exit 1
@@ -75,23 +113,23 @@ if [ ! -d $MOUNTPATH ]; then
75113
fi
76114
else
77115
echo Created!
78-
fi
116+
fi
79117
else
80-
echo Presented!
118+
echo Present!
81119
fi
82120

83121
echo Testing for write permissions...
84-
122+
85123
TEMPWDIRTEST="write_test_$RANDOM";
86124
mkdir $MOUNTPATH/$TEMPWDIRTEST >/dev/null 2>&1
87125
RETCODE=$?
88126

89127
if [ $RETCODE -ne 0 ]; then
90128
echo Fail! You do not have write permissions in $MOUNTPATH !
91-
129+
92130
if [ "$(id -u)" != "0" ]; then
93131
echo [Try to run this script as root!]
94-
fi
132+
fi
95133
exit 1
96134
else
97135
echo Success!
@@ -105,7 +143,7 @@ RETCODE=$?
105143

106144
if [ $RETCODE -eq 127 ]; then
107145
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\".";
109147
echo -e "For Ubuntu run \"sudo apt-get install sshfs\" and \"sudo apt-get install fuse-utils\".";
110148
echo -e "For Mandriva run \"urpmi fuse-utils sshfs\".";
111149
exit 1
@@ -123,22 +161,22 @@ if [ $ISNOTMACOS -eq 1 ]; then
123161

124162
thunar -h >/dev/null 2>&1
125163
RETCODE=$?
126-
164+
127165
if [ $RETCODE -eq 127 ]; then
128166
nautilus -h >/dev/null 2>&1
129167
RETCODE=$?
130-
168+
131169
if [ $RETCODE -eq 127 ]; then
132170
dolphin -h >/dev/null 2>&1
133171
RETCODE=$?
134-
135-
if [ $RETCODE -eq 127 ]; then
172+
173+
if [ $RETCODE -eq 127 ]; then
136174
nemo -h >/dev/null 2>&1
137175
RETCODE=$?
138-
139-
if [ $RETCODE -ne 127 ]; then
176+
177+
if [ $RETCODE -ne 127 ]; then
140178
FAVOURITEFILEMANAGER="nemo"
141-
fi
179+
fi
142180
else
143181
FAVOURITEFILEMANAGER="dolphin"
144182
fi
@@ -195,54 +233,76 @@ else
195233
echo -e "Set username => $USERNAME";
196234
fi
197235

236+
MNT="${MOUNTPATH}/VM_${IPADDRESS}_${PORT}_${USERNAME}"
237+
echo -e "Checking for ${MNT}...";
198238

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}"
204242
RETCODE=$?
205243
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
208256
fi
209257
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
211271
fi
212272

213273
echo Mounting...
214274

215275
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
217277
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"
219279
fi
220280
RETCODE=$?
221281

222282
if [[ $RETCODE -ge 0 && $RETCODE -le 1 ]]; then
223283
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
227287

228288
nohup --help >/dev/null 2>&1
229289
RETCODE=$?
230290

231291
if [[ "$FAVOURITEFILEMANAGER" != "your favourite file manager" && $INSILENTMODE -ne 1 ]]; then
232-
292+
233293
while [[ "$ANS" != "Y" && "$ANS" != "y" && "$ANS" != "N" && "$ANS" != "n" ]]; do
234294
echo -ne "Do you want to open it with $FAVOURITEFILEMANAGER[Y/N]? ";
235295
read -e -n1 ANS;
236296
done
237297

238298
if [[ "$ANS" = "Y" || "$ANS" = "y" ]]; then
239-
299+
240300
if [ $RETCODE -ne 127 ]; then
241-
FAVOURITEFILEMANAGERCMD="${FAVOURITEFILEMANAGER} $MOUNTPATH/VM_${IPADDRESS}_${PORT}_${USERNAME}";
301+
FAVOURITEFILEMANAGERCMD="${FAVOURITEFILEMANAGER} \"${MNT}\"";
242302
nohup bash -c "$FAVOURITEFILEMANAGERCMD &" >/dev/null 2>&1
243303
rm nohup.out >/dev/null 2>&1
244304
else
245-
$FAVOURITEFILEMANAGER $MOUNTPATH/VM_${IPADDRESS}_${PORT}_${USERNAME}
305+
$FAVOURITEFILEMANAGER "${MNT}"
246306
fi
247307
else
248308
echo Autoopen canceled!

0 commit comments

Comments
 (0)