LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-16-2011, 12:56 AM   #1
nursegirl1
LQ Newbie
 
Registered: Mar 2011
Posts: 3

Rep: Reputation: 0
Help


I need help writing a script that parses the name and password given in a file
(userid- Prentis Parmenion (password- ySHSPV5v)
and creates an Ubutnu user using the first two letter of his/her name followed by the full last name. The pwd should be in the third field.

---------- Post added 03-16-11 at 12:57 AM ----------

I need help writing a script that parses the name and password given in a file
(userid- Prentis Parmenion (password- ySHSPV5v)
and creates an Ubutnu user using the first two letter of his/her name followed by the full last name. The pwd should be in the third field.
windows_7 nursegirl1 is online now Click to check your reputation for this post Edit/Delete Message
 
Old 03-16-2011, 01:45 AM   #2
kpearsonxyz
LQ Newbie
 
Registered: Jan 2011
Location: Lehi, Utah
Distribution: Linux Mint
Posts: 14

Rep: Reputation: 1
If you are asking for a script that will use a file as input, that's fairly easy, provided that the file is delineated with a specific character, or each part of the file is an exact number of spaces, ie:

file 1:
Code:
brown, john, bo2x6&@!
Smith, Marge, alks84r32
file 2:
Code:
brown       john        bosx6&@!
Smith       marge       alks84r32
The script would look something like:

Code:
#!/bin/bash

FL=/path/to/users.file

TTL=`cat $FL|wc -l`
cnt=1

until [ "$TTL" = "$cnt" ] ; do
  LN=`cat $FL|head -n$cnt|tail -n1`
# file 1 example:
    LSTNM=`echo $LN|cut -f1 -d',' | tr 'A-Z' 'a-z'` ; # strip any upper-case letters to lower-case
    FRSTNM=`echo $LN|cut -f2 -d',' | tr 'A-Z' 'a-z'`
    PASSWD=`echo $LN|cut -f2 -d','`
# file 2 example:
    LSTNM=`echo $LN|cut -c1-12 | tr 'A-Z' 'a-z'`
    FRSTNM=`echo $LN|cut -c13-24 | tr 'A-Z' 'a-z'`
    PASSWD=`echo $LN|cut -c25-999` ;# to get everything to the end of the line
# create login name
    LGN=`echo $FRSTNM|cut -c1-2
    LGN=$LGN$LSTNM
# create account
    echo $PASSWD
    adduser $LGN
done
# End of script
adduser must be run as root. You can either sudo su - first (depending on your distribution, you might just do su -) or put sudo adduser $LGN on the appropriate line. The first cycle through the file, you will be prompted for the correct sudo password. After that, you won't be prompted again.

Because of security requirements, you can't automate the password creation, so this script will prompt you for the password, which you can just copy and paste, twice, from the echo command above the prompt.

This is a quick and dirty example. YMMV (your mileage may vary).

Last edited by kpearsonxyz; 03-16-2011 at 01:48 AM. Reason: add su instruction
 
Old 03-24-2011, 11:14 AM   #3
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by kpearsonxyz View Post
... Because of security requirements, you can't automate the password creation, ...
Actually, there is a command, newusers, for the convenience of admins. who may have to add large #s of users in batches.


From the newusers man page, as found on a SimplyMEPIS 8.0 box:
Quote:
NAME
newusers - update and create new users in batch
Quote:
The newusers command reads a file of user name and clear-text password pairs and uses this information to update a group of existing users or to create new users. Each line is in the same format as the standard password file (see passwd(5)) with the following exceptions: ...
Quote:
This command is intended to be used in a large system environment where many accounts are updated at a single time.
Quote:
CAVEATS
The input file must be protected since it contains unencrypted passwords.

Since Ubuntu & SimplyMEPIS are both Debian derivatives, I assume newusers is available in Ubuntu.
 
1 members found this post helpful.
Old 03-25-2011, 10:28 AM   #4
kpearsonxyz
LQ Newbie
 
Registered: Jan 2011
Location: Lehi, Utah
Distribution: Linux Mint
Posts: 14

Rep: Reputation: 1
Hey, I learned something new.

Thanks.
 
Old 03-25-2011, 06:59 PM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Feel free to give rep, or click helpful.
 
  


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




All times are GMT -5. The time now is 02:37 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