Shell Script to filter users and create them again from a back-up server

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to filter users and create them again from a back-up server
# 1  
Old 09-26-2013
Shell Script to filter users and create them again from a back-up server

This is a script to filter the users out of etc/passwd and etc/group. So if you want to migrate of restore a server you can use this script from a backup to restore and make the same users on you had..

Please feedback and comments.


prompt_list () {
# haal uit de argumenten de prompt en de keuzes
#echo len: $LEN
#echo prompt: $PROMPT
#echo keuzes: $CHOICES
   echo -e "\e[01;31m${PROMPT}\e[00m"
   PS3="Maak je keuze ('s' voor stop): "
   select ANSWER in ${CHOICES}; do
      case ${ANSWER} in
         *    ) CHOICE=${ANSWER};  break;;
   check_stop ${CHOICE}

check_stop () {
   if [ -z ${1} ]
      echo -e "\n\e[01;31mJe hebt er voor gekozen om te stoppen\e[00m\n"

contains() {
    if test "${string#*$substring}" != "$string"

echo "--------------------------------------"
if [ $# = 0 ]
	echo "Usage: $0 <username>"


echo "Users zijn $USERS"
prompt_list "Which tar.gz you want to use for userid?" `locate *etc*passwd.tar.gz`
echo "--------------------------------------"
prompt_list "Which tar.gz you want to use for groupid?" `locate *etc*group.tar.gz`

tar -zxvf ${GROUP_OBJECT}
tar -zxvf ${PASSWD_OBJECT}

for USER in $USERS		
	read -e -p "Which user you want to know the userid and groupid? " -i "$USER" USER
	USERID=`cat passwd | grep "^${USER}:" | cut -d":" -f3`
	if [ "${USERID}x" = "x" ]
		echo "User $USER not in passwd"
		echo -e "userid is: \t$USERID"

		GROUPID=`cat passwd | grep "^${USER}:" | cut -d":" -f4`
		echo -e "groupid is: \t$GROUPID"
		HOMEID=`cat passwd | grep "^${USER}:" | cut -d":" -f6`
		echo -e "homedir is: \t$HOMEID"

		SHELLID=`cat passwd | grep "^${USER}:" | cut -d":" -f7`
		echo -e "shellid is: \t$SHELLID"

		GROUPID2=`cat group | grep "^${USER}:" | cut -d":" -f3`
                echo -e "groupid in group is: \t$GROUPID2"
		if [ ${GROUPID} -eq ${GROUPID2} ]
			SUDOERS=`cat group | grep "^sudo:" | cut -d":" -f4`
                	echo -e "sudoers: \t$SUDOERS"
			contains $SUDOERS $USER
			echo $CONTAINS
			if [ "$CONTAINS" == "ja" ]
				SUDOPARAM="-G sudo"
			echo "This is ${SUDOPARAM}"

			groupadd -g ${GROUPID2} ${USER}
			echo -e "\e[01;31mResult Add to groep is ${RESULT}\e[00m"
			useradd -u ${USERID} -g ${GROUPID} ${SUDOPARAM} -d ${HOMEID} -m -s ${SHELLID} ${USER}
			echo -e "\e[01;31mAdd result user is ${RESULT}\e[00m"
			echo "groupid of passwd is not equal from groupid of group"

Last edited by dannyvdberg; 09-26-2013 at 01:06 PM..
# 2  
Old 09-26-2013
What are you asking here? In general this script is too dependent on a specific environment. What if I don't have the bash shell or use sudo. Also, system administrators generally don't like to be prompted and "joe user" should not be running a script to add users so I would never hand this task off to someone not responsible for admin. There also seems to a language dependency here. Why is PS3 in a different language than the echo statements?
Not sure of the value of the functionality here. I don't mean to be negative but you asked for feedback.
# 3  
Old 09-26-2013
Nice script.
We don't normal do this where I work. If the system is backed up, so are the user accounts and if you build the server again the accounts will be there from your last backup.
# 4  
Old 09-26-2013
Run the following UUOC killer on your script:
sed 's/\<cat \([^ ][^ ]*  *\)| *\([^{(]\)/ < \1\2/' yourscript > newscript

and see the result with
diff yourscript newscript

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 09-26-2013
... and even more effective were the single read
IFS=":" read X X USERID GROUPID X HOMEID SHELLID < <(grep $USER /etc/passwd)

These 2 Users Gave Thanks to RudiC For This Post:
# 6  
Old 09-27-2013
Good point, this bashism (and zshism) simplifies a lot.
Of course keep the original grep "^${USER}:"!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to read through a file and create new users/assign them to groups in Ubuntu

Hi all. I need a shell script that can, in short, read through a text file line by line and create a new user in Ubuntu, as well as assign that user to a group. The format of the text file is not important but preferably: 'username:group'. I don't have much programming knowledge no matter shell... (3 Replies)
Discussion started by: LewisWeekly
3 Replies

2. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

3. Shell Programming and Scripting

Create users from a text file by script

Hi All, I am new to scripting, i want to create one script in which i can add users from a text file, assign them a default password like 123456. It should be like: Username= $i (it should take users from text file one by one) Password : 123456(default for every user) (1 Reply)
Discussion started by: prad_rocxx
1 Replies

4. Shell Programming and Scripting

Help with script to create users from database query

#!/bin/bash user=`mysql userList -uuserlist -puserlistpassword -s -N -e "SELECT userName FROM users WHERE activated='n'"` for i in $user; do useradd "$i" -m doneThis is what I have done so far. But obviously it still does not work. I'm trying to create users based on information stored in a... (5 Replies)
Discussion started by: bucketuk
5 Replies

5. Shell Programming and Scripting

How to create a shell script to remove the files on solaris server at 00000hrs?

Hi folks, As per mentioned in the title, how to create a shell script to delete those files from the server at 00000hrs every day? Thanks in advance :) (2 Replies)
Discussion started by: kimurayuki
2 Replies

6. Shell Programming and Scripting

Create new users in DMZ box using script

I remote to many DMZ boxes every day to run batch file that allows me to create users. I create users in 17 DMZ boxes every day which takes a lot of my time. Is there any script that would do this job from my local computer? Thank you for your help! (3 Replies)
Discussion started by: idiazza
3 Replies

7. Shell Programming and Scripting

help to create script for added date to list users

hi my friends im asking for the possibility to creat a script in ubuntu for added date to list users for doing this : - search in debug connected user of all connected users - if a new user is connect for the first time to my server the script record the date of the connection and added it... (1 Reply)
Discussion started by: amzioujda
1 Replies

8. OS X (Apple)

interactive shell script to create users 10.4

Hello everyone, Not sure if this is the right place, but OS X isn't your standard Unix, so I figured here would be best. I am looking at creating a script that will be interactive that admins can run to create users. Now, 10.4 uses netinfo database and netinfo manager to handle it's users. ... (3 Replies)
Discussion started by: tlarkin
3 Replies

9. Shell Programming and Scripting

script to create users on many servers

Hi all, working on script to create a user acct on all our servers. for i in `cat $host_file`; do ssh $i /usr/bin/sudo /usr/bin/mkuser id='bpadm' gecos='NetBackup Admin' 2>&1 >> $log done error i get is: 3004-692 Error changing "id" to "bpadm" : Value is invalid. I have tried this in... (1 Reply)
Discussion started by: dnidiffer
1 Replies

10. UNIX for Dummies Questions & Answers

shell scripts to create 100 users

Hello i need a shell script to create 100 users i am running hp-ux......... startegy is something like this craete a shell script !/bin/ksh counter=1 while do { useradd usr$counter passwd usr$counter # here begins my problem when i say passwd usr$counter #it again prompts... (9 Replies)
Discussion started by: xiamin
9 Replies
Login or Register to Ask a Question