Sponsored Content
Top Forums Shell Programming and Scripting Shell Script to filter users and create them again from a back-up server Post 302857385 by dannyvdberg on Thursday 26th of September 2013 08:14:15 AM
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.

Code:
#!/bin/bash

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

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

contains() {
    string="$1"
    substring="$2"
    if test "${string#*$substring}" != "$string"
    then
        CONTAINS=ja
    else
        CONTAINS=nee
    fi
}

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

	

USERS=$@ 
echo "Users zijn $USERS"
updatedb
prompt_list "Which tar.gz you want to use for userid?" `locate *etc*passwd.tar.gz`
PASSWD_OBJECT=${CHOICE}
echo "--------------------------------------"
prompt_list "Which tar.gz you want to use for groupid?" `locate *etc*group.tar.gz`
GROUP_OBJECT=${CHOICE}

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

for USER in $USERS		
do		
	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" ]
	then
		echo "User $USER not in passwd"
	else
 	
		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} ]
		then
			SUDOERS=`cat group | grep "^sudo:" | cut -d":" -f4`
                	echo -e "sudoers: \t$SUDOERS"
			
			contains $SUDOERS $USER
			echo $CONTAINS
			if [ "$CONTAINS" == "ja" ]
			then
				SUDOPARAM="-G sudo"
			else
    			        SUDOPARAM=""
			fi
		
			echo "This is ${SUDOPARAM}"

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


Last edited by dannyvdberg; 09-26-2013 at 12:06 PM..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ROUND(3)								 1								  ROUND(3)

round - Rounds a float

SYNOPSIS
float round (float $val, [int $precision], [int $mode = PHP_ROUND_HALF_UP]) DESCRIPTION
Returns the rounded value of $val to specified $precision (number of digits after the decimal point). $precision can also be negative or zero (default). Note PHP doesn't handle strings like "12,300.2" correctly by default. See converting from strings. PARAMETERS
o $val - The value to round o $precision - The optional number of decimal digits to round to. o $mode - Use one of the following constants to specify the mode in which rounding occurs. +--------------------+---------------------------------------------------+ | Constant | | | | | | | Description | | | | +--------------------+---------------------------------------------------+ | | | | PHP_ROUND_HALF_UP | | | | | | | Round $val up to $precision decimal places away | | | from zero, when it is half way there. Making 1.5 | | | into 2 and -1.5 into -2. | | | | | | | |PHP_ROUND_HALF_DOWN | | | | | | | Round $val down to $precision decimal places | | | towards zero, when it is half way there. Making | | | 1.5 into 1 and -1.5 into -1. | | | | | | | |PHP_ROUND_HALF_EVEN | | | | | | | Round $val to $precision decimal places towards | | | the next even value. | | | | | | | |PHP_ROUND_HALF_ODD | | | | | | | Round $val to $precision decimal places towards | | | the next odd value. | | | | +--------------------+---------------------------------------------------+ RETURN VALUES
The rounded value EXAMPLES
Example #1 round(3) examples <?php echo round(3.4); // 3 echo round(3.5); // 4 echo round(3.6); // 4 echo round(3.6, 0); // 4 echo round(1.95583, 2); // 1.96 echo round(1241757, -3); // 1242000 echo round(5.045, 2); // 5.05 echo round(5.055, 2); // 5.06 ?> Example #2 $mode examples <?php echo round(9.5, 0, PHP_ROUND_HALF_UP); // 10 echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9 echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10 echo round(9.5, 0, PHP_ROUND_HALF_ODD); // 9 echo round(8.5, 0, PHP_ROUND_HALF_UP); // 9 echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8 echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8 echo round(8.5, 0, PHP_ROUND_HALF_ODD); // 9 ?> Example #3 $mode with precision examples <?php /* Using PHP_ROUND_HALF_UP with 1 decimal digit precision */ echo round( 1.55, 1, PHP_ROUND_HALF_UP); // 1.6 echo round( 1.54, 1, PHP_ROUND_HALF_UP); // 1.5 echo round(-1.55, 1, PHP_ROUND_HALF_UP); // -1.6 echo round(-1.54, 1, PHP_ROUND_HALF_UP); // -1.5 /* Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision */ echo round( 1.55, 1, PHP_ROUND_HALF_DOWN); // 1.5 echo round( 1.54, 1, PHP_ROUND_HALF_DOWN); // 1.5 echo round(-1.55, 1, PHP_ROUND_HALF_DOWN); // -1.5 echo round(-1.54, 1, PHP_ROUND_HALF_DOWN); // -1.5 /* Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision */ echo round( 1.55, 1, PHP_ROUND_HALF_EVEN); // 1.6 echo round( 1.54, 1, PHP_ROUND_HALF_EVEN); // 1.5 echo round(-1.55, 1, PHP_ROUND_HALF_EVEN); // -1.6 echo round(-1.54, 1, PHP_ROUND_HALF_EVEN); // -1.5 /* Using PHP_ROUND_HALF_ODD with 1 decimal digit precision */ echo round( 1.55, 1, PHP_ROUND_HALF_ODD); // 1.5 echo round( 1.54, 1, PHP_ROUND_HALF_ODD); // 1.5 echo round(-1.55, 1, PHP_ROUND_HALF_ODD); // -1.5 echo round(-1.54, 1, PHP_ROUND_HALF_ODD); // -1.5 ?> CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.3.0 | | | | | | | The $mode parameter was introduced. | | | | | 5.2.7 | | | | | | | The inner workings of round(3) was changed to | | | conform to the C99 standard. | | | | +--------+---------------------------------------------------+ SEE ALSO
ceil(3), floor(3), number_format(3). PHP Documentation Group ROUND(3)
All times are GMT -4. The time now is 04:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy