Friday 21 December 2012

Samba Share Script

#!/bin/bash
# Purpose : Enabling samba share in Linux PC,s
# Author  :
# START
# Checking the user is root or not

 function check() {
   echo " This script is used for creating a new shares only . If you have any old shares it will be over written. For continue press 1 and discontinue press 2 "
   read val
   }
  check
  if [ $val -ne 1 ]; then
   if [ $val -eq 2 ]; then
    echo " OK "
    exit 0
   else
    check
   fi
  else
   echo "Continue "
   echo "Your Operating system is : "
   cat /etc/redhat-release
  fi
   if [ `id -u ` -ne 0 ]; then
    printf "\n Only root user can use this script \n"
   fi
# checking the packages are installed or not
 rpm -qa | grep samba-* > /tmp/testrpm##
  if [  $? -eq 0 ]; then
   printf "\n packages are already installed \n"
   cat /tmp/testrpm##
  else
   printf "\n packages are not installed you have to install the following packages\n"
   echo "samba-client-version number"
   echo
   echo "samba-version number"
   echo
   echo "samba-common-version number"
   echo
   echo "system-config-samba-version number"
   echo
   exit 0
  fi
# removing the file which having the package details
 rm -rf /tmp/testrpm##
# Taking the backup of original configuration file
 Ori=/etc/samba/smb.conf
 bak="/etc/samba/smb.conf.copy.ori.`date +%d-%b-%y-%T`"
  if [ -e /etc/samba/smb.conf ]; then
   cp $Ori $bak
  fi
# Moving the original file
 mov="/etc/samba/smb.conf.move.ori.`date +%d-%b-%y-%T`"
  if [ -e /etc/samba/smb.conf ]; then
   mv $Ori $mov
  fi
# Creating the Samba confiuration file
 echo "#=================================Global Settings==============================" >> /etc/samba/smb.conf
 echo "" >> /etc/samba/smb.conf
 printf " Enter Your Workgroup\n "
 read workgroup
 echo "workgroup=$workgroup" >> /etc/samba/smb.conf
 printf " Enter the Server String\n "
 read string
 echo "server string=$string"  >> /etc/samba/smb.conf
 echo "printcap name = /etc/printcap" >> /etc/samba/smb.conf
 echo "load printers = yes"  >> /etc/samba/smb.conf
 echo "cups options = raw"  >> /etc/samba/smb.conf
 echo "log file = /var/log/samba/%m.log"  >> /etc/samba/smb.conf
 echo "max log = 50"  >> /etc/samba/smb.conf
  function secure() {
   echo " Which Security mode you want "
   echo "user [1] "
   echo "share [2]"
   read sec
    case $sec in
     "1" ) echo "security = user"  >> /etc/samba/smb.conf;;
     "2" ) echo "security = share"  >> /etc/samba/smb.conf;;
       * ) secure;;
    esac
    }
 secure
 echo "socket options = TCP_NODELAY SO_RCVBUF=8192 SO_SNDBUF=8192"  >> /etc/samba/smb.conf
 echo "#====================================Share Definitions=================="  >> /etc/samba/smb.conf
 echo "[homes]"  >> /etc/samba/smb.conf
 echo "comment = Home Directories"  >> /etc/samba/smb.conf
 echo "browseable = no "  >> /etc/samba/smb.conf
 echo "writable = yes "  >> /etc/samba/smb.conf
 echo ""  >> /etc/samba/smb.conf
 echo ""  >> /etc/samba/smb.conf
 echo "[printers]"  >> /etc/samba/smb.conf
 echo "path = /var/spool/samba"  >> /etc/samba/smb.conf
 echo "browseable = no "  >> /etc/samba/smb.conf
 echo "guest ok = no " >> /etc/samba/smb.conf
 echo "writable = no " >> /etc/samba/smb.conf
 echo "printable = yes " >> /etc/samba/smb.conf
 echo ""  >> /etc/samba/smb.conf
 echo ""  >> /etc/samba/smb.conf
#Adding the samba users
  function totaluser() {
   function smu() {
   printf "If you want to add samba users [ Yes [1] No[2] ] \n"
   read smuser
    }
   smu
   if [ $smuser -ne 1 ]; then
    if [ $smuser -eq 2 ]; then
     echo "ok"
    else
     smu
    fi
   else
   { printf "Enter the user name \n"
    read testuser
    grep $testuser  /etc/passwd  > /dev/null
    ch=$?
     if [ $ch -ne 0 ]; then
      function con() {
        printf "User is not exits . If you want to create this user now press 1 or if you want try again press 2 [ Yes [1] No [2] ]"
        read uconfirm
        }
     con
     if [ $uconfirm -ne 1 ]; then
      if [ $uconfirm -eq 2 ]; then
       totaluser
      else
       con
      fi
     else
      adduser $testuser
      printf "Type the password for the $testuser  user \n"
      passwd $testuser
      printf " Type a samba password "
      smbpasswd -a $testuser
     fi
     else
      printf "type the samba password for the user"
      smbpasswd -a $testuser
     fi
    }
     fi
       }
   totaluser
# creating share
 function definition()
  {
   printf " Type your share name\n "
   read name
   echo "[$name]" >> /etc/samba/smb.conf
   printf " Type the Share comment \n"
   read comment
   echo "comment = $comment" >> /etc/samba/smb.conf
   printf " Type your PATH ( absolute path eg : /var/spool/ ) "
   read path
   echo "path = $path" >> /etc/samba/smb.conf
 function testuser()
  {
   printf " If you want to apply for valid users [Yes [1] No [2] ]"
   read vuser
    if [ $vuser -eq 1 ]; then
     printf "Type a valid user name [ eg mary fred ] "
     read valid
     echo "valid users = $valid" >> /etc/samba/smb.conf
    fi
  }
  testuser
  }
 function share() {
   echo " If you want to add more share [Yes [1] , No [2] ] "
   read key
    if [ $key -eq 1 ]; then
     definition
     share
    fi
   }
 share
   echo "Starting the service "
   service smb start
   echo "Testing the shares "
   testparm
   echo "Restarting the service"
   service smb restart
 echo
 echo
 echo "========================SUCCESSFULLY CREATED YOUR SHARE================================" 
   

No comments:

Post a Comment