Client Area
Mount A Samba Share If Server Is Available
Mount a share if ping returns successful.
Description: The past few days I have been playing "High Availability" systems. So I had apache up and running and it worked, good. However on both machines each had its own copy of the web pages I was having it serve. So I was thinking why not have a 3rd machine be a fileserver and have the web pages be read over that. So here is a script I wrote to automatically do it for me. Now you can just use smbmount and it will "remount" the files if they are already there or throw an error if it can't find the server, but I through in the checks just for fun.
OS: Linux/Unix
Requirements: Bash, Samba (server and clients)

Source Code:
  1.  
  2. # Check if the share is already mounted
  3. if [ $(mount | grep -c //server/sharename) -eq 1 ]
  4. then
  5. echo "Drive Already Mounted";
  6. else
  7. # If its not already mounted "ping" the ip address (or name) of the server
  8. if ping -c 1 ip_address
  9. then
  10. # If you get a response from the server mount the share
  11. smbmount //server/sharename /localfolder -o username=SAMBA_USERNAME,password=SAMBA_PASSWORD,uid=500,gid=500
  12. echo "Drive Mounted";
  13. else
  14. # It cant find the server
  15. echo "Cannot Find Server!";
  16. fi
  17. fi

Example:
 
if [ $(mount | grep -c //filserver/backups) -eq 1 ] 
then
	echo "Drive Already Mounted";
else
	# If its not already mounted "ping" the ip address (or name) of the server
	if ping -c 1 filserver
	then
		# If you get a response from the server mount the share
		smbmount //fileserver/backups /backups -o username=jeff,password=JeffRocksMySocks,uid=500,gid=500
		echo "Drive Mounted";
	else
		# It cant find the server
		echo "Cannot Find Server!";
	fi
fi
Twitter: