ManageGroups

Differences between revisions 2 and 3
Revision 2 as of 2008-11-11 13:57:45
Size: 2395
Editor: c-66-41-153-100
Comment:
Revision 3 as of 2008-11-13 05:07:51
Size: 2481
Editor: c-75-73-59-22
Comment:
Deletions are marked like this. Additions are marked like this.
Line 14: Line 14:
#!/bin/bash
# Script to add a user to Linux system
 #!/bin/bash
 # Script to add a user to Linux system
 
 FILENAME=Students.csv
 EXIST=Student.Exist
 >$EXIST
 debug=1
 
 # Test input for a file name and verify it´s existance
 if [ -z $1 ]; then
  echo Requires a filename to process
  exit
 else
   if [ ! -f $1 ]; then
    echo File ${FILENAME} does not exists
    exit
   fi
 # remove space characters from file from the file
  cat $1 | sed 's/ //g' > $FILENAME
 fi
 
 
 #if [ $(id -u) -eq 1006 ]; then
 
 if [ $(id -u) -eq 0 ]; then
  for d in `cat ${FILENAME}`
  do
    # Get the students name in normal form, first letter capitalized
   n_ln=`echo $d | cut -d, -f1`
 
   # Lowercase the name and cut out the first 2 characters
   ln2=`echo ${n_ln} | tr [A-Z] [a-z] | cut -c 1-2`
   
   # Get the first name
   n_fn=`echo ${d} | cut -d, -f2`
   # Lowercase the first name
   fn6=`echo ${n_fn} | tr [A-Z] [a-z] | cut -c 1-6`
 
   username=`echo ${fn6}${ln2}`
   period=`echo ${d} | cut -d, -f3`
   password=`echo ${d} | cut -d, -f4`
   
   if [ $debug -eq 0 ]; then
    echo First Name $n_fn
    echo Last Name $n_ln
    echo Login Name $username
    echo Period $period
    echo Password $password
    echo
   fi
 
 
   if [ $debug -eq 1 ]; then
    #read -p "Enter username : " username
    #read -p "Enter password : " password
    egrep "^period${period}" /etc/group >/dev/null
    if [ $? -ne 0 ]; then
     echo "period$period does not exists!"
     echo "Creating group period${period}"
     groupadd period${period}
    fi
    
    egrep "^$username" /etc/passwd >/dev/null
    # if the user exist dump user name out to a file
    if [ $? -eq 0 ]; then
      echo "Student $n_fn $n_ln already exists"
      echo "${n_ln},${n_fn},${period},${password}" >> $EXIST
    else
     pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
     useradd -m -c "${n_fn} ${n_ln}" -g period${period} -p $pass $username
     [ $? -eq 0 ] && echo "User ${n_fn} ${n_ln} has been added to system!" || echo "Failed to add a user!"
    fi
    fi
   done
 else
  echo "Only root may add a user to the system"
  exit 2
 fi
 rm $FILENAME
Line 17: Line 94:
FILENAME=Students.csv
EXIST=Student.Exist
>$EXIST
debug=1

# Test input for a file name and verify it´s existance
if [ -z $1 ]; then
 echo Requires a filename to process
 exit
else
  if [ ! -f $1 ]; then
   echo File ${FILENAME} does not exists
   exit
  fi
# remove space characters from file from the file
 cat $1 | sed 's/ //g' > $FILENAME
fi


#if [ $(id -u) -eq 1006 ]; then

if [ $(id -u) -eq 0 ]; then
 for d in `cat ${FILENAME}`
 do
   # Get the students name in normal form, first letter capitalized
  n_ln=`echo $d | cut -d, -f1`

  # Lowercase the name and cut out the first 2 characters
  ln2=`echo ${n_ln} | tr [A-Z] [a-z] | cut -c 1-2`
  
  # Get the first name
  n_fn=`echo ${d} | cut -d, -f2`
  # Lowercase the first name
  fn6=`echo ${n_fn} | tr [A-Z] [a-z] | cut -c 1-6`

  username=`echo ${fn6}${ln2}`
  period=`echo ${d} | cut -d, -f3`
  password=`echo ${d} | cut -d, -f4`
  
  if [ $debug -eq 0 ]; then
   echo First Name $n_fn
   echo Last Name $n_ln
   echo Login Name $username
   echo Period $period
   echo Password $password
   echo
  fi


  if [ $debug -eq 1 ]; then
   #read -p "Enter username : " username
   #read -p "Enter password : " password
   egrep "^period${period}" /etc/group >/dev/null
   if [ $? -ne 0 ]; then
    echo "period$period does not exists!"
    echo "Creating group period${period}"
    groupadd period${period}
   fi
   
   egrep "^$username" /etc/passwd >/dev/null
   # if the user exist dump user name out to a file
   if [ $? -eq 0 ]; then
     echo "Student $n_fn $n_ln already exists"
     echo "${n_ln},${n_fn},${period},${password}" >> $EXIST
   else
    pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
    useradd -m -c "${n_fn} ${n_ln}" -g period${period} -p $pass $username
    [ $? -eq 0 ] && echo "User ${n_fn} ${n_ln} has been added to system!" || echo "Failed to add a user!"
   fi
  fi
 done
else
 echo "Only root may add a user to the system"
 exit 2
fi
rm $FILENAME

Figure out how to set detailed preferences for groups of users:

Description:

Procedure:

  • Do something
    • some detail
  • Do something else
    • some detail

Comments:

v1

Script to Add users from a CSV File

  • #!/bin/bash # Script to add a user to Linux system FILENAME=Students.csv EXIST=Student.Exist

    >$EXIST debug=1 # Test input for a file name and verify it´s existance if [ -z $1 ]; then

    • echo Requires a filename to process exit
    else
    • if [ ! -f $1 ]; then
      • echo File ${FILENAME} does not exists exit
      fi
    # remove space characters from file from the file
    • cat $1 | sed 's/ //g' > $FILENAME

    fi #if [ $(id -u) -eq 1006 ]; then if [ $(id -u) -eq 0 ]; then
    • for d in cat ${FILENAME} do

      • # Get the students name in normal form, first letter capitalized

        n_ln=echo $d | cut -d, -f1 # Lowercase the name and cut out the first 2 characters

        ln2=echo ${n_ln} | tr [A-Z] [a-z] | cut -c 1-2 # Get the first name

        n_fn=echo ${d} | cut -d, -f2 # Lowercase the first name fn6=echo ${n_fn} | tr [A-Z] [a-z] | cut -c 1-6

        username=echo ${fn6}${ln2} period=echo ${d} | cut -d, -f3 password=echo ${d} | cut -d, -f4 if [ $debug -eq 0 ]; then

        • echo First Name $n_fn echo Last Name $n_ln echo Login Name $username echo Period $period echo Password $password echo
        fi if [ $debug -eq 1 ]; then
        • #read -p "Enter username : " username #read -p "Enter password : " password

          egrep "^period${period}" /etc/group >/dev/null if [ $? -ne 0 ]; then

          • echo "period$period does not exists!" echo "Creating group period${period}" groupadd period${period}
          fi

          egrep "^$username" /etc/passwd >/dev/null # if the user exist dump user name out to a file if [ $? -eq 0 ]; then

          • echo "Student $n_fn $n_ln already exists"

            echo "${n_ln},${n_fn},${period},${password}" >> $EXIST

          else
          • pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) useradd -m -c "${n_fn} ${n_ln}" -g period${period} -p $pass $username

            [ $? -eq 0 ] && echo "User ${n_fn} ${n_ln} has been added to system!" || echo "Failed to add a user!"

          fi
        fi
      done
    else
    • echo "Only root may add a user to the system" exit 2
    fi rm $FILENAME

v2

v3

m1

m2

m3

ManageGroups (last edited 2009-10-14 20:51:25 by 75-150-67-119-NewEngland)