You are not logged in.

#1 2010-09-22 07:02:19

Alexbit
Member
From: Italy
Registered: 2010-06-02
Posts: 60

[SOLVED] advanced shell script for mount and unmount samba share

Good morning guys,
Today I've a very long question....
Hope in your help....:

I usually move from different network.
For one of those I need to mount some share that are under an ActiveDirectory server.
To do that I use samba.
Especially I've made and use the below  shell script.
I know that it's so stupid but I'm a very newbie:

#!/bin/sh
echo "This is a custom script for mount my citrix share"
echo "Please insert user password (must a sudoer user)"
echo " "
sudo mount -t cifs -o username='myusername',password='mypassword' //host_ip/TsHome$/myusername /home/myuser/samba_share/TsHOME
sudo mount -t cifs -o username='myusername',password='mypassword' "//host_ip/direction" /home/myuser/samba_share/direction
echo " "
read -p "Press ENTER key to close this terminal"
exit

As I say the script work but it's so crude...!
I want/need to implement it whit the follow addictions:

1. print a feedback on mount: somenthing like "TsHOME is now mounted" or "unable to mount TsHOME"
2. check if the share are already mounted: to prevent accidentally multiple mount. If a share is already mounted I want to print something like "TsHOME is already mounted on /home/myuser/samba_share/TsHOME"
3. ask for share password  (now it's in clear on script) - This is optinal
4. un-mount the share before reboot or shutdown command. That because I've notice that If i reboot or shutdown without manually un-mount the share the step of unmounting network file system became very slow

If it's no extremely difficult, can someone help me to write this script?
I know that man exist but I'm not a programmer and it's so difficult for me approch it.
Thank you in advance.
Ale

Last edited by Alexbit (2010-09-23 21:29:13)

Offline

#2 2010-09-22 10:55:12

barto
Member
From: Budapest, Hungary
Registered: 2009-10-22
Posts: 88

Re: [SOLVED] advanced shell script for mount and unmount samba share

Hello,

1. Examine the return values of the mount commands

sudo mount -t cifs -o username='myusername',password='mypassword' //host_ip/TsHome$/myusername /home/myuser/samba_share/TsHOME && echo "TsHOME is now mounted" || echo "unable to mount TsHOME"

2. Make the script look at /etc/mtab

if grep "TsHome" /etc/mtab &>/dev/null; then
    grep "TsHome" /etc/mtab | awk '{print "TsHome is already mounted on " $2}'
else
    [sudo mount ... whatever]
fi

3. read with -s option

read -s mypassword
sudo mount [...] password="$mypassword" [...]

4. Put the unmounting command to /etc/rc.local.shutdown with the conditional structure similar to that of point 2.

Good luck. smile


“First principle, Clarice. Simplicity” – Dr. Hannibal Lecter

Offline

#3 2010-09-23 17:34:12

Alexbit
Member
From: Italy
Registered: 2010-06-02
Posts: 60

Re: [SOLVED] advanced shell script for mount and unmount samba share

Thank you very (VERY) much!
I've follow your information and.. it work!

If can be usefull to other this is the complete script:

#!/bin/sh
echo "This is a custom script for mount citrix share"
echo "****************************************"
echo " "
echo "Please insert domain password:" 
read -s mypassword
echo " "
echo "|checking mount state...in share are not mounted mount it!|"
echo "+-------------------------------------------------------------------------+"

if grep "TsHome" /etc/mtab &>/dev/null; then
    grep "TsHome" /etc/mtab | awk '{print "TsHome is already mounted on " $2}'
else
    sudo mount -t cifs -o username='yourusername',password="$mypassword" //ipaddress/TsHome$ /home/Samba_share/TsHOME && echo "TsHOME is now mounted" || echo "unable to mount TsHOME"
fi
echo " "
if grep "direction" /etc/mtab &>/dev/null; then
    grep "direction" /etc/mtab | awk '{print "direction is already mounted on " $2}'
else
    sudo mount -t cifs -o username='yourusername',password="$mypassword" '//ipaddress/direction' /home/Samba_share/direction' && echo "direction pat is now mounted" || echo "unable to mount direction"
fi
echo " "
if grep "Area" /etc/mtab &>/dev/null; then
    grep "Area" /etc/mtab | awk '{print "Area is already mounted on " $2}'
else
    sudo mount -t cifs -o username='yourusername',password="$mypassword" '//ipaddress/Area' /home/Samba_share/Area && echo "Area is now mounted" || echo "unable to mount Area"
fi
echo " "
echo "All DONE!"
echo " "
read -p "Press ENTER key to close this terminal"
exit

I don't really well understand what I've to put in rc.local.shutdown....
I think somethings like:

#!/bin/bash
#
# /etc/rc.local.shutdown: Local shutdown script.
#

echo "This is a custom script for UN-mount citrix share"
echo "********************************************"
echo " "
echo "|check mount state: if mount then un-mount!|"
echo "+-------------------------------------------------------+"
echo " "
if grep "TsHome" /etc/mtab &>/dev/null; then
    grep "TsHome" /etc/mtab | awk '{print "TsHome is NOT mounted -> going on..." $2}'
else
    sudo umount /home/Samba_share/TsHOME && echo "TsHOME is now UN mounted" || echo "unable to UN-mount TsHOME"
fi
echo " "
if grep "direction" /etc/mtab &>/dev/null; then
    grep "direction" /etc/mtab | awk '{print "direction is NOT mounted -> going on.." $2}'
else
    sudo umount /home/Samba_share/direction && echo "direction is now UN mounted" || echo "unable to UN-mount direction pat"
fi
echo " "
if grep "Area" /etc/mtab &>/dev/null; then
    grep "Area" /etc/mtab | awk '{print "Area is NOT mounted - Finished" $2}'
else
    sudo umount /home/Samba_share/Area && echo "Area is now UN mounted" || echo "unable to UN-mount Area"
fi
echo " "
echo "going shutdown"
echo " "
sleep 5

I've try but it seems to ignore the IF statement... this is the output when ALL share are UNmounted:

$ sh /etc/rc.local.shutdown
This is a custom script for UN-mount citrix share
********************************************
 
|check mount state: if mount then un-mount!|
+-------------------------------------------------------+
 
umount: /home/Samba_share/TsHOME: not mounted
unable to UN-mount TsHOME
 
umount: /home/Samba_share/direction: not mounted
unable to UN-mount direction
 
umount: /home/Samba_share/Area: not mounted
unable to UN-mount Area
 
going shutdown

Tomorrow I will test again when I'm at office.
Meanwhile can you check if I made a macroscopic mistakes?
Thank you again!

Last edited by Alexbit (2010-09-23 17:34:49)

Offline

#4 2010-09-23 19:32:02

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [SOLVED] advanced shell script for mount and unmount samba share

Your if statements are the wrong way round in rc.shutdown.
Also, you can ditch the grep & awk in those ones and use echo.

if grep "TsHome" /etc/mtab &>/dev/null; then
    sudo umount etc.....
else
    echo "TsHome not mounted..."
fi

Last edited by skanky (2010-09-23 19:32:20)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#5 2010-09-23 20:21:31

Alexbit
Member
From: Italy
Registered: 2010-06-02
Posts: 60

Re: [SOLVED] advanced shell script for mount and unmount samba share

YES!Just modded the shudown script!
This is the output:

$ sh /etc/rc.local.shutdown
 
This is a custom script for UN-mount citrix share
********************************************
 
|..:: Check mount state: if mount then un-mount! ::..|
 
 
TsHome is NOT mounted -> going on...
 
direction is NOT mounted -> going on..
 
Area is NOT mounted - Finished
 
...continue shutdown in 5 seconds

Now, in my simulation scenario seems to work!
As I say, tomorrow I'll check in a real environment!

Last time question:
rc.local.shutdown is called over a reboot or only on shutdown?

For now....
Thank you ALL!

Offline

#6 2010-09-23 20:32:42

barto
Member
From: Budapest, Hungary
Registered: 2009-10-22
Posts: 88

Re: [SOLVED] advanced shell script for mount and unmount samba share

Yes, as stated above.
The grep + awk pipeline was for getting the mount point, which has no sense in this case.
rc.local.shutdown is called at reboot too.


“First principle, Clarice. Simplicity” – Dr. Hannibal Lecter

Offline

Board footer

Powered by FluxBB