LinuxQuestions.org
Visit Jeremy's Blog.
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 05-25-2017, 02:30 PM   #1
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237
Blog Entries: 2

Rep: Reputation: Disabled
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.

Last edited by justmy2cents; 05-25-2017 at 03:06 PM.
 
Old 05-25-2017, 02:40 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,274

Rep: Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994
Just "not working", or there is some error message?

next$1 and user$i are the same, or not?
 
Old 05-25-2017, 02:45 PM   #3
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237
Blog Entries: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
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.
 
Old 05-25-2017, 02:47 PM   #4
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,357

Rep: Reputation: 213Reputation: 213Reputation: 213
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
 
Old 05-25-2017, 03:03 PM   #5
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237
Blog Entries: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Xeratul View Post
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)

Last edited by justmy2cents; 05-26-2017 at 09:43 AM.
 
Old 05-25-2017, 03:10 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 16,128

Rep: Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866
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
 
Old 05-25-2017, 03:13 PM   #7
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237
Blog Entries: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
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.
 
Old 05-25-2017, 03:32 PM   #8
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237
Blog Entries: 2

Original Poster
Rep: Reputation: Disabled
@ 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..
 
Old 05-25-2017, 03:55 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 16,128

Rep: Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866
You can...

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

Last edited by michaelk; 05-25-2017 at 05:45 PM.
 
Old 05-26-2017, 04:27 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,274

Rep: Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994
Or
Code:
Pwd=$(dd if=/dev/random bs=1 count=6 2>/dev/null | base64)
 
Old 05-26-2017, 09:54 AM   #11
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,735

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
Quote:
Originally Posted by justmy2cents View Post
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.

Last edited by BW-userx; 05-26-2017 at 10:00 AM.
 
Old 05-26-2017, 02:13 PM   #12
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237
Blog Entries: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
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..
 
Old 05-26-2017, 02:29 PM   #13
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237
Blog Entries: 2

Original Poster
Rep: Reputation: Disabled
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

Last edited by justmy2cents; 05-26-2017 at 03:12 PM.
 
Old 05-26-2017, 04:29 PM   #14
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237
Blog Entries: 2

Original Poster
Rep: Reputation: Disabled
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..
 
Old 05-27-2017, 02:21 AM   #15
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 6,474
Blog Entries: 2

Rep: Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566
Quote:
Originally Posted by NevemTeve View Post
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.
 
  


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
shell script for adding multiple users on multiple servers. newtech Linux - Newbie 3 04-07-2015 03:58 AM
Script to create multiple user on multiple server. ashish.deokar Linux - Server 3 08-22-2014 09:05 AM
How do I create a shared folder for multiple users? vinodtpba Linux - Newbie 5 04-05-2014 11:54 AM
[SOLVED] create multiple samba users dushyantgohil Linux - Server 2 10-16-2012 05:19 AM
Script to create multiple directories under users homes shawnbishop Linux - Software 4 03-30-2006 08:11 AM

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

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

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