LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-09-2010, 02:34 PM   #1
hobbes80
LQ Newbie
 
Registered: Aug 2010
Posts: 9

Rep: Reputation: 0
BASH: create user and password on multiple machines


I'm trying to write a script that will prompt the user for a username/password, then create that user/password in the right groups on all my machines. I know this is kind of a long way around to avoid a NIS server, but I like making my life more difficult.

this is what I have so far:
Code:
MATCH=1

# Get username and password
save_state=$(stty -g)

echo -n "Username: "
read username
while [ $MATCH -gt 0 ]
do
echo -n "Password: "
stty -echo
read password
stty "$save_state"
echo -n "Re-Enter Password: "
stty -echo
read password2
stty "$save_state"
echo ""
if ["${password}" = "${password2}"]
then
MATCH=0
else
echo Passwords do not match
MATCH=$(( $MATCH + 1 ))
fi
if [ $MATCH -gt 4 ]
then
MATCH=0
TEST="FAIL"
fi
done

if ["${TEST}" = "FAIL"]
then
echo Password fail, dumping out
else
echo test account
ssh root@10.241.209.61 "useradd ${username} -G dsadmins"
ssh root@10.241.209.61 "passwd ${username}" <<EOF
"${password}"
"${password2}"
EOF
fi
the script has 2 problems:
the "if" functions return an error and do not compare the strings successfully.
whatever password is entered does get applied properly and the user is unable to login
 
Old 08-09-2010, 03:22 PM   #2
agnar
LQ Newbie
 
Registered: Jul 2009
Distribution: Arch Linux, Ubuntu
Posts: 6

Rep: Reputation: 1
Try to add a blank after '[' and before ']' in the if statements.
E.g.
if ["${TEST}" = "FAIL"]
should be
if [ "${TEST}" = "FAIL" ]
 
Old 08-09-2010, 03:27 PM   #3
hobbes80
LQ Newbie
 
Registered: Aug 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks, that fixed the "if" statements but the password is not getting inserted properly into the user account. Any ideas on that front?
 
Old 08-10-2010, 12:30 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
This is my version.
Code:
#!/bin/bash

shopt -s extglob

function f {
	local USERNAME PASSWORD PASSWORD2
	local GROUP='dsadmins'     ## you can also have these values by asking user in a prompt or by parsing arguments ($@)
	local HOST=10.241.209.61

	for (( ;; )); do
		echo -n 'Enter username: '

		USERNAME=''
		read USERNAME

		if [[ -z $USERNAME || $USERNAME != [[:alpha:]_]*([[:alnum]]) ]]; then    ## this can have additional checks
			echo 'Please enter a valid unsername.'
			echo
			continue
		fi

		break
	done

	for (( ;; )); do
		echo -n 'Enter password: '

		PASSWORD=''
		read PASSWORD

		if [[ -z $PASSWORD || $PASSWORD = *$'\t'* ]]; then    ## this can have additional checks
			echo 'Please enter a valid password.'
			echo
			continue
		fi

		echo -n 'Enter again password: '

		PASSWORD2=''
		read PASSWORD2

		if [[ ! $PASSWORD2 = "$PASSWORD" ]]; then
			echo 'Passwords do not match.'
			echo
			continue
		fi

		break
	done

	ssh "root@$HOST" "useradd $USERNAME -G $GROUP" || {
		echo "Failed to add username '$USERNAME' to group '$GROUP'."
		return 1
	}

	ssh "root@$HOST" "passwd $USERNAME" <<EOF
$PASSWORD
$PASSWORD
EOF

	if [[ $? -ne 0 ]]; then
		echo "Failed to change password of '$USERNAME'."
		return 1
	fi

	return 0
}

f
Please customize it in any way you like.

Last edited by konsolebox; 08-24-2010 at 03:06 AM. Reason: "PASSWORD" -> $PASSWORD
 
Old 08-10-2010, 09:58 AM   #5
hobbes80
LQ Newbie
 
Registered: Aug 2010
Posts: 9

Original Poster
Rep: Reputation: 0
This works, but makes the password for the account "PASSWORD" with the quotes... which is the same problem with my script.

I tried modifying it with adding ${ } around it, no dice.

Thoughts on how to fix?
 
Old 08-10-2010, 10:05 AM   #6
hobbes80
LQ Newbie
 
Registered: Aug 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Figured it out. Here is the functioning script that sets the password on all the machines:

Code:
# Create_Users and passwords

#Identify Servers
SERVERS="10.241.209.61 10.241.209.62 10.241.209.63 10.241.209.64 10.241.209.65 10.241.209.66 10.241.209.67 10.241.209.68 10.241.209.69"

MATCH=1

# Get username and password
save_state=$(stty -g)

echo -n "Usernamd: "
read username
while [ $MATCH -gt 0 ]
do
echo -n "Password: "
stty -echo
read password
stty "$save_state"
echo -n "Re-Enter Password: "
stty -echo
read password2
stty "$save_state"
echo ""
if [ "${password}" = "${password2}" ]
then
MATCH=0
else
echo Passwords do not match
MATCH=$(( $MATCH + 1 ))
fi
if [ $MATCH -gt 4 ]
then
MATCH=0
TEST="FAIL"
fi
done

if [ "${TEST}" = "FAIL" ]
then
echo Password fail, dumping out
else
# create user accounts
for host in $SERVERS
do
echo Configuring $host User and password
ssh root@$host "useradd ${username} -G dsadmins"
ssh root@$host "passwd ${username}" <<EOF
${password}
${password}
EOF
echo User and password set for $host
echo
done
fi
 
Old 08-10-2010, 05:55 PM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Code:
	ssh "root@$HOST" "passwd $USERNAME" <<EOF
"PASSWORD"
"PASSWORD"
EOF
Odd. I'm pretty sure what I intended to write was:
Code:
	ssh "root@$HOST" "passwd $USERNAME" <<EOF
"$PASSWORD"
"$PASSWORD"
EOF
 
Old 08-11-2010, 10:15 AM   #8
hobbes80
LQ Newbie
 
Registered: Aug 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by konsolebox View Post
Odd. I'm pretty sure what I intended to write was:
Code:
	ssh "root@$HOST" "passwd $USERNAME" <<EOF
"$PASSWORD"
"$PASSWORD"
EOF
either way, "$PASSWORD" did not work. the password became exactly that, no variable entered.

What did work was ${PASSWORD} with no quotes.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Create user add file with default password and force user to change it? Morgandy Linux - Newbie 3 02-02-2010 05:06 PM
How do I create multiple variables from a list in Bash? Passions Programming 12 05-23-2009 05:23 PM
create multiple directories in bash? Count Zero Programming 19 07-06-2008 08:34 AM
how i create user without password lion_heart_300 Fedora 4 03-29-2007 07:24 AM
How do I Create User w/o password?? Ruth Linux - General 6 03-02-2001 09:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration