0% found this document useful (0 votes)
2 views

addusersdc

This shell script automates the creation of user directories and Samba users from a CSV file. It checks for the existence of 'users' and 'shares' directories in /home, creates them if they don't exist, and prompts the user for necessary inputs such as IP address and volume size. The script processes the CSV file to create users, ensuring that duplicates and restricted usernames are handled appropriately while logging the results.

Uploaded by

mike
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

addusersdc

This shell script automates the creation of user directories and Samba users from a CSV file. It checks for the existence of 'users' and 'shares' directories in /home, creates them if they don't exist, and prompts the user for necessary inputs such as IP address and volume size. The script processes the CSV file to create users, ensuring that duplicates and restricted usernames are handled appropriately while logging the results.

Uploaded by

mike
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#!

/bin/sh -x

dirusers=$(ls /home | grep users)


varusers="users"
dircero=""
sleep 3

####################USERS##########################################################
###

if [ $dirusers = $dircero ]

then
(dialog --infobox "The directori users will be created in /home." 0 0)
sleep 3

mkdir /home/users

elif [ $dirusers = $varusers ]

then
(dialog --infobox "The directories users already exists in /home." 10 20)
sleep 3

else
(dialog --infobox "Coming Out" 10 0)
sleep 2

fi

######################SHARES#######################################################
#######

dirshares=$(ls /home | grep shares)


dircero=""
varshares="shares"
sleep 3

if [ $dirshares = $dircero ]

then
(dialog --infobox "The directori shares will be created in /home." 0 0)
sleep 3

mkdir /home/shares

elif [ $dirshares = $varshares ]

then
(dialog --infobox "The directories shares already exists in /home." 10 20)
sleep 2

else
(dialog --infobox "Coming Out" 10 0)
sleep 3
fi
###################################################################################
########

(dialog --title "Read carefully, important" \


--stdout \
--msgbox "\nThis application will create users from a .csv file.\nIt ask you
to write the IP,
netbios (hostname) to create the network directories of the users.\nIt ask you
selet the file that contains the name of the users that will be included in the
domain.\nThe .csv file must be in the same folder from where the application is
launched and must be made up of four groups of characters separated by a comma per
line,\nfor example:\n\n
madrid,madrid,Madrid,Europe Spain\n
leganes,leganes,Madrid,Europe Spain\n
pepitopp,pepitopp,Pepito,Perez Perez\n\n
The first group will be the user, the second will be the password,\nthe third will
be the main name and the fourth group will be the surname or secondary name\n\n
END" 0 0 )

hostname=$(hostname)

domain=$(dialog --title "Write the IP or enter to continue" \


--stdout \
--inputbox "\nWrite IP, Netbios or Domain name\ndefault hostname" 0 0
$hostname )

volume=$(volume)

volume=$(dialog --title "Write the volume to netdir´s user" \


--stdout \
--inputbox "\nWrite only numbers, will be multiped for 4\nVolume
default 512 Megas" 0 0 512)

#################################################################
################Selet file .csv New Users DC#########################

dir=$(pwd)
file=$(dialog --title "Selet file .csv" \
--stdout \
--fselect $dir/. 14 70 )

cp ${file} ${file}.bak

newusers=$(cat -n ${file})

(dialog --msgbox "The setec file is ${file}" 0 0)


(dialog --title "list of new users to add DC" \
--msgbox "${newusers}" 0 0)
(dialog --infobox "The list will be reviewed and repeat users will be moved
on the control list." 0 0)
sleep 7
#################################################################

OLDIFS=$IFS
IFS=","

while read user passwword firstname lastname


do

####################Control Variables############################################
nopermit=admin
usersdc=$(samba-tool user list | grep $user )

if [ $user = $nopermit ]

then

for i in $(seq 0 10 100) ;


do
echo $i | (dialog --gauge "The name ${nopermit} no permit \nWill not be
added" 0 0 0); done
sleep 3
sed -i "/${user}/d" ${file}

elif [ $user = $usersdc ]

then

for i in $(seq 0 10 100) ;


do
echo $i | (dialog --gauge "The ${user} user exist in domain \nWill not be
added again" 0 0 0); done
sleep 3
fecha=$(date)
echo "Usuario: ${user} -Already exist -Date ${fecha} " >>
controlNewUsersDC.txt
sed -i "/${user}/d" ${file}

listado=$(cat $file)
for i in $(seq 0 10 100) ;
do
echo $i | (dialog --title "Reviewing list new users" --gauge "${listado}" 0 0
0); done
sleep 3

fi
done < $file

################################################################

OLDIFS=$IFS
IFS=","
mkdir /media/usersdc/
while read user passwword firstname lastname
do

samba-tool user create "${user}" "${passwword}" --given-name="${firstname}" --


surname="${lastname}"
fecha=$(date)

mkdir /home/users/${user}
chmod 700 /home/users/${user}

dd if=/dev/zero of=/media/usersdc/$user.img bs=${volume}M count=4


mkfs.ext4 /media/usersdc/$user.img
mount -o loop /media/usersdc/$user.img /home/users/$user
echo >> /etc/fstab
echo >> /etc/fstab "#1"$user
echo >> /etc/fstab /media/usersdc/$user.img /home/users/$user ext4 loop 0 2
echo >> /etc/fstab "#2"$user

echo >> /etc/samba/smb.conf


echo >> /etc/samba/smb.conf "#1"${user}
echo >> /etc/samba/smb.conf [${user}]
echo >> /etc/samba/smb.conf "browseable = no"
echo >> /etc/samba/smb.conf "path = /home/users/"${user}
echo >> /etc/samba/smb.conf "read only = no"
echo >> /etc/samba/smb.conf "admin users = "${user}
echo >> /etc/samba/smb.conf "#2"${user}
pdbedit ${user} -D Y:
pdbedit ${user} -h \\\\${domain}\\${user}
smbcontrol all reload-config
echo "Usuario: ${user} - Password: ${passwword} - Creado el: ${fecha}" >>
usuariosDCcreados.txt

done < $file

You might also like