LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: create user and password on multiple machines (https://www.linuxquestions.org/questions/programming-9/bash-create-user-and-password-on-multiple-machines-825137/)

hobbes80 08-09-2010 02:34 PM

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

agnar 08-09-2010 03:22 PM

Try to add a blank after '[' and before ']' in the if statements.
E.g.
if ["${TEST}" = "FAIL"]
should be
if [ "${TEST}" = "FAIL" ]

hobbes80 08-09-2010 03:27 PM

Thanks, that fixed the "if" statements but the password is not getting inserted properly into the user account. Any ideas on that front?

konsolebox 08-10-2010 12:30 AM

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.

hobbes80 08-10-2010 09:58 AM

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?

hobbes80 08-10-2010 10:05 AM

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


konsolebox 08-10-2010 05:55 PM

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


hobbes80 08-11-2010 10:15 AM

Quote:

Originally Posted by konsolebox (Post 4062413)
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.


All times are GMT -5. The time now is 10:30 PM.