[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1139
  • Last Modified:

bash samba connection check/mount

Hi I need help creating a script which main purpose would be to check if the smbmount connection has been established. If is has not, then ask the user to enter the ip address of the node that will be tested. And the last portion would be to issue to smbmount command:

smbmount //10.5.0.3/icsglobal /mnt/iotest/ --verbose -o username=admin pass=admin1
#Prompt for remote or local test, if is remote invoke samba, for local do nothing
#Samba check and mount
#Checking if mount /mnt/iotest directory exists
 
samba=/mnt/mountpoint
 
read -p "Please enter the ip address of the node to test: " -e ip
 
 
df -h |grep $samba
if [ -d $samba];then
   echo "Directory $samba appears to be mounted"
else
   if [ ! -d $samba];then
   echo "Creating $samba directory"
   echo "Mounting $samba directory for samba connection"
   smbmount //10.5.0.3/mnt/iotest/ --verbose -o username=admin pass=admin1
fi

Open in new window

0
dpoper1
Asked:
dpoper1
  • 2
3 Solutions
 
Maciej SsysadminCommented:
Little modification of your script.
Is this what you wanted to have?
#!/bin/sh
 
MOUNTPOINT=/mnt/mountpoint
 
printf -- "Please enter the IP address of the node to test: "
read IPADDRESS
 
RESULT=`df -h | grep "${MOUNTPOINT}"`
if [ -n "${RESULT}" ];then
   echo "Directory ${MOUNTPOINT} appears to be mounted"
elif [ ! -d "${MOUNTPOINT}" ];then
   echo "Creating ${MOUNTPOINT} directory"
   mkdir -p ${MOUNTPOINT}
   echo "Mounting ${MOUNTPOINT} directory for samba connection"
   smbmount //${IPADDRESS}/icsglobal ${MOUNTPOINT} --verbose -o username=admin pass=admin1
fi 

Open in new window

0
 
Duncan RoeSoftware DeveloperCommented:
Do you realise that the IP address of the node to test is not used at all in the process of testing? I haven't yet used SAMBA myself, but if you do "mount -v", do you see the IP address in the output? And do you have multiple SMB mounts? (or CIFS). If you only have 1 mount point and 1 IP, there's no need to ask for the IP if the mount is already done. If you have 1 mount point and several possible IPs, you could do something like below (assuming mount -v shows the IP).
This script does not cater for there being a different IP already mounted - the mount command in that case may have unexpected results. Please post your requirements in more detail if you're still in doubt what to do
#!/bin/sh
 
MOUNTPOINT=/mnt/mountpoint
 
printf -- "Please enter the IP address of the node to test: "
read IPADDRESS
 
if mount -v | grep $IPADDRESS >/dev/null;then
   echo "Directory ${MOUNTPOINT} appears to be mounted"
elif [ ! -d "${MOUNTPOINT}" ];then
   echo "Creating ${MOUNTPOINT} directory"
   mkdir -p ${MOUNTPOINT}
   echo "Mounting ${MOUNTPOINT} directory for samba connection"
   smbmount //${IPADDRESS}/icsglobal ${MOUNTPOINT} --verbose -o username=admin pass=admin1
fi 

Open in new window

0
 
Duncan RoeSoftware DeveloperCommented:
Should have changed the echo. Also this time, try to cater for another IP being mounted (no guarantees though).
*** Just noticed your main problem - missing fi (compounded by previous poster's converting else to elif).
Your original post didn't have a mkdir - oklit suggested mkdir -p which actually makes testing for the existence of the mount directory unnecessary. mkdir -p will not fail if the mount directory already exists, but will fail if it doesn't and mkdir can't create it. But you want to echo if creating, so I've dropped the -p.
|| after a command specifies what to do if the command fails. mount can fail if the mount point is in use, for example
#!/bin/sh
 
MOUNTPOINT=/mnt/mountpoint
 
printf -- "Please enter the IP address of the node to test: "
read IPADDRESS
 
if mount -v | grep $IPADDRESS >/dev/null;then
   echo "File system at $IPADDRESS appears to be mounted"
else
   if [ ! -d "${MOUNTPOINT}" ];then
      echo "Creating ${MOUNTPOINT} directory"
      mkdir ${MOUNTPOINT} || exit 1
   # Unmount old IP if required
   elif mount -v | grep $MOUNTPOINT >/dev/null;then
      echo "Removing previous mount to $MOUNTPOINT"
      umount $MOUNTPOINT || exit 1
   fi
   echo "Mounting ${MOUNTPOINT} directory for samba connection"
   smbmount //${IPADDRESS}/icsglobal ${MOUNTPOINT} --verbose -o username=admin pass=admin1
fi 

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now