From 0fbfb7fb829ac8312b219bbac14d7cba8a99328c Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Mon, 25 Jul 2022 13:57:48 -0400 Subject: [PATCH] Updated scripts --- .gitignore | 2 +- pdc-add-user-professor.sh | 53 ++++++++++++++++++++++++++++++++++ pdc-bulk-add-user-professor.sh | 36 +++++++++++++++++++++++ pdc-bulk-del-user.sh | 36 +++++++++++++++++++++++ 4 files changed, 126 insertions(+), 1 deletion(-) mode change 100644 => 100755 .gitignore create mode 100755 pdc-add-user-professor.sh create mode 100755 pdc-bulk-add-user-professor.sh create mode 100644 pdc-bulk-del-user.sh diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index a542cf0..dd5f1ef --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .vscode -users.csv +*.csv diff --git a/pdc-add-user-professor.sh b/pdc-add-user-professor.sh new file mode 100755 index 0000000..cdc81d6 --- /dev/null +++ b/pdc-add-user-professor.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +############################### +# Script to add a student # +# Must be run as root # +# For use with Zential Server # +############################### + +USERNAME=$1 +NAME=$2 +DEFAULT_QUOTA=256000 # 256MB + +# Check if arguments are not empty +if [ -z "$USERNAME" ] || [ -z "$NAME" ]; then + echo "Usage: $0 " + exit 1 +fi + +# Check if $USERNAME has spaces +if [[ $USERNAME =~ " " ]]; then + echo "Username cannot have spaces" + exit 1 +fi + +NAME=$(echo $NAME | tr "[A-Z]" "[a-z]" | sed -e "s/\b\(.\)/\u\1/g") + +# Check if $USERNAME is already in use +if samba-tool user show $USERNAME >/dev/null 2>&1; then + echo "User $USERNAME already exists" + exit 1 +fi + +# Capitalize every first letter of $NAME + +# Create user +echo "Creating user $USERNAME with name $NAME and password $USERNAME" +samba-tool user create $USERNAME $USERNAME --userou='OU=Professores,OU=IFMS-PP' --given-name="$NAME" --home-drive='H:' --home-directory="\\\\acadsrv.ACAD.PP.IFMS.EDU.BR\\$USERNAME" --use-username-as-cn --must-change-at-next-login + +# Add user to group alunos +echo "Adding user $USERNAME to group professores" +samba-tool group addmembers professores $USERNAME + +# Create home directory +echo "Creating home directory for user $USERNAME" +mkdir /home/$USERNAME +echo "Setting permissions for home directory" +chmod 700 /home/$USERNAME/ -R +chown $USERNAME:'domain users' /home/$USERNAME/ -R + +# Set user quota +./pdc-setquota.sh $USERNAME $DEFAULT_QUOTA + +echo "User $USERNAME ($NAME) created. Password must be changed at next login." diff --git a/pdc-bulk-add-user-professor.sh b/pdc-bulk-add-user-professor.sh new file mode 100755 index 0000000..f24b691 --- /dev/null +++ b/pdc-bulk-add-user-professor.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +file=$1 + +# Check if arguments are not empty +if [ -z "$file" ]; then + echo "Usage: $0 " + exit 1 +fi + +# Check if file exists +if [ ! -f "$file" ]; then + echo "File $file does not exist" + exit 1 +fi + +# Check if file is empty +if [ ! -s "$file" ]; then + echo "File $file is empty" + exit 1 +fi + +while IFS="" read -r p || [ -n "$p" ]; do + USERNAME=$(echo $p | cut -d ";" -f 1) + NAME=$(echo $p | cut -d ";" -f 2) + + # Check if $USERNAME is empty + if [ -z "$USERNAME" ]; then + echo "Username is empty" + continue + fi + + echo "---" + ./pdc-add-user-professor.sh $USERNAME "$NAME" + +done <$file diff --git a/pdc-bulk-del-user.sh b/pdc-bulk-del-user.sh new file mode 100644 index 0000000..d8f5c03 --- /dev/null +++ b/pdc-bulk-del-user.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +file=$1 + +# Check if arguments are not empty +if [ -z "$file" ]; then + echo "Usage: $0 " + exit 1 +fi + +# Check if file exists +if [ ! -f "$file" ]; then + echo "File $file does not exist" + exit 1 +fi + +# Check if file is empty +if [ ! -s "$file" ]; then + echo "File $file is empty" + exit 1 +fi + +while IFS="" read -r p || [ -n "$p" ]; do + USERNAME=$(echo $p | cut -d ";" -f 1) + NAME=$(echo $p | cut -d ";" -f 2) + + # Check if $USERNAME is empty + if [ -z "$USERNAME" ]; then + echo "Username is empty" + continue + fi + + echo "---" + ./pdc-del-user.sh $USERNAME + +done <$file