LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to create multiple users, not working.. (https://www.linuxquestions.org/questions/programming-9/script-to-create-multiple-users-not-working-4175606660/)

justmy2cents 05-25-2017 02:30 PM

Script to create multiple users, not working..
 
Hello ladies and gents, I have this script that im not quite sure why it's not working. Any help would be appreciated, Thank You.

Code:

#!/bin/bash
for (( i=0; i<=500; i++ ))
do
useradd next$1
< /dev/urandom tr -dc A-Za-z0-9_|head -c8 > /tmp/passwd.txt
cat /tmp/passwd.txt|passwd --stdin user$i
done
rm -rf /tmp/passwd.txt

Basically this script makes users with names next0, next1, next3 and so on up till 500.. The /dev/urandom part generates a random password and directs the output to /tmp/passwd.txt, but that goes on indefinitely, so head -c8 is used to specify the password length to be 8 characters (the -c8 does this)... The error that occurs is that it says --stdin is an unrecognized option.. My code succeeds in creating the users but their passwords fail to get created.

NevemTeve 05-25-2017 02:40 PM

Just "not working", or there is some error message?

next$1 and user$i are the same, or not?

justmy2cents 05-25-2017 02:45 PM

Quote:

Originally Posted by NevemTeve (Post 5715146)
Just "not working", or there is some error message?

next$1 and user$i are the same, or not?

Thank you for responding, I have just included additional information in my OP, please have a read.

Xeratul 05-25-2017 02:47 PM

Quote:

#!/bin/bash
for (( i=0; i<=500; i++ ))
do
useradd next$1
< /dev/urandom tr -dc A-Za-z0-9_|head -c8 > /tmp/passwd.txt
cat /tmp/passwd.txt|passwd --stdin user$i
done
rm -rf /tmp/passwd.txt
might it be? (unchecked)
Quote:

seq 1 1 500 | while read -r i ; do
useradd next${i} < /dev/urandom tr -dc A-Za-z0-9_|head -c8 > /tmp/passwd.txt
cat /tmp/passwd.txt|passwd --stdin user${i}
done

justmy2cents 05-25-2017 03:03 PM

Quote:

Originally Posted by Xeratul (Post 5715154)
might it be? (unchecked)

Can you please elaborate on what you mean by unchecked? I tried the code you posted and that produced the additional error "useradd: invalid home directory 'c'" Also nothng was written to the /tmp/passwd file, and no users were created..(my code creates users but their passwords fail to get created)

michaelk 05-25-2017 03:10 PM

Quote:

The error that occurs is that it says --stdin is an unrecognized option..
Your distribution/version does not have the passwd version that has --stdin option and will not accept a password from stdin. I do not recommend using the passwd command in a script for security reasons but you can use the chpasswd instead.

echo "password:name" | chpasswd

justmy2cents 05-25-2017 03:13 PM

Quote:

Originally Posted by michaelk (Post 5715169)
Your distribution/version does not have the passwd version that has --stdin option and will not accept a password from stdin. I do not recommend using the passwd command in a script for security reasons but you can use the chpasswd instead.

echo "password:name" | chpasswd

I'll give it a shot, I just recently discovered the chpasswd command earlier but it seemed like it was only for changing currently existing passwords.

justmy2cents 05-25-2017 03:32 PM

@ michaelk

Actually I was kinda hoping that I could retain the use of the /dev/urandom part of the script as to generate random passwords for each user..

michaelk 05-25-2017 03:55 PM

You can...

Code:

password=$(tr -cd a-zA-Z0-9_ < /dev/urandom | head -c 8)
echo "$password:user$i" | chpasswd


NevemTeve 05-26-2017 04:27 AM

Or
Code:

Pwd=$(dd if=/dev/random bs=1 count=6 2>/dev/null | base64)

BW-userx 05-26-2017 09:54 AM

Quote:

Originally Posted by justmy2cents (Post 5715139)
Hello ladies and gents, I have this script that im not quite sure why it's not working. Any help would be appreciated, Thank You.

Code:

#!/bin/bash
for (( i=0; i<=500; i++ ))
do
useradd next$1
< /dev/urandom tr -dc A-Za-z0-9_|head -c8 > /tmp/passwd.txt
cat /tmp/passwd.txt|passwd --stdin user$i
done
rm -rf /tmp/passwd.txt -- someone is going to be doing a lot stiffening
through the /passwd file to get the users passwords so they can
give them to the person to login.


this (( i=0 and next$1 is not a match you have i (eye) for your number increment "next0, next1, next3 and so on up till 500" and 1 (one) for the variable name. Where is the value coming from?

Don't know if anyone else caught that.

justmy2cents 05-26-2017 02:13 PM

Quote:

Originally Posted by BW-userx (Post 5715478)
this (( i=0 and next$1 is not a match you have i (eye) for your number increment "next0, next1, next3 and so on up till 500" and 1 (one) for the variable name. Where is the value coming from?

Don't know if anyone else caught that.

Good catch, the $1 in next$1 is supposed to be an $i..

justmy2cents 05-26-2017 02:29 PM

Quote:

Nothing you can do in bash can possibly work. passwd(1) does not read from standard input. This is intentional. It is for your protection. Passwords were never intended to be put into programs, or generated by programs. They were intended to be entered only by the fingers of an actual human being, with a functional brain, and never, ever written down anywhere. So before you continue, consider the possibility that the authors of passwd(1) were on to something, and you probably shouldn't be trying to script passwd(1) input.

Nonetheless, we get hordes of users asking how they can circumvent 35 years of Unix security. And we get people contributing their favorite security-removing "solutions" to this page. If you still think this is what you want, read on.
http://mywiki.wooledge.org/BashFAQ/078

After reading that^ I realized I shouldn't be using the passwd command.. So I decided to use the mkpasswd command. This is my new code but I don't have the mkpasswd on hand yet to try it out.. Is there's any errors in this script that's obvious?

Code:

#!/bin/bash
mkdir -p /home/admin/useraccounts
for (( i=0; i<=500; i++ ))
do
useradd -m next$i -s /bin/bash -p "$(mkpasswd "$password")"
#echo the usernames and passwords to the admin account
echo -e "Username:next$i" > /home/admin/useraccounts/next$i
echo -e "Password:" >> /home/admin/useraccounts/next$i
done


justmy2cents 05-26-2017 04:29 PM

After further reading the above article I realized that using mkpasswd is also not recommended.. I should instead use Kerberos.. So I think making a script such as this, to automatically generate passwords is insecure, and thus pointless.. This is because credentials should be separate from the script.. Please correct me if im wrong.. Nevertheless thanks you all for your suggestions..

ondoho 05-27-2017 02:21 AM

Quote:

Originally Posted by NevemTeve (Post 5715381)
Or
Code:

Pwd=$(dd if=/dev/random bs=1 count=6 2>/dev/null | base64)

very interesting.
it can cause significant delay.
i think it takes entropy from user activity? is it more random than /dev/urandom?

also, when i try to ramp up the password length (count > 6), the last characters are always the same, a '=' usually.
i wonder why.

oh, i see now, it works when i increase bs instead.


All times are GMT -5. The time now is 07:59 PM.