#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    The Little Red Dot
    Posts
    149
    Rep Power
    12

    Shell Script to create user in Linux using text file data


    Shell Script to create user in Linux using text file data.

    Is it possible?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    137
    Rep Power
    0
    Yep it is.

    Here's a quick and dirty example.

    You did not tell which info is in the text file and it's layout, so i'm goin to assume the following:

    - 1 line per newuser,
    - username password group are the space seperated fields
    Code:
    #!/usr/bin/ksh
    
    NEW_USERS="/path/to/text_data_file"
    HOME_BASE="/home/"
    
    cat ${NEW_USERS} | \
    while read USER PASSWORD GROUP
    do
      useradd -g ${GROUP} -p ${PASSWORD} -m -d ${HOME_BASE}${USER} ${USER}
    done
    From here you should be able to expand/add/subtract things you want/don't want.
    Last edited by druuna; September 5th, 2003 at 11:07 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    The Little Red Dot
    Posts
    149
    Rep Power
    12
    Thanks alot. It works!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    The Little Red Dot
    Posts
    149
    Rep Power
    12
    ermm by the way how do you compile/execute a ksh in linux terminal?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    137
    Rep Power
    0
    The same way you execute sh/bash:

    scriptname.ksh <enter> (if found in PATH)

    or

    ./scriptname.ksh <enter>

    It could be that ksh (pdksh) is not installed by default (Suse doesn't, don't know about RedHat).

    It should reside in /usr/bin or /bin. /bin/ksh should be linked to /usr/bin/ksh ( /bin/ksh -> ../usr/bin/ksh ).

    But you don't need to worry if ksh isn't installed, bash is (almost) as good. Just change #!/usr/bin/ksh to #!/bin/sh or #!/bin/bash (sh should be linked to bash: /bin/sh -> bash.
    Last edited by druuna; September 8th, 2003 at 06:55 AM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    1
    Rep Power
    0

    Iam not able to login after creating a user using script


    Hi, Everybody... Good Afternoon

    Why I am not able to login to create a user .... by using the following script..

    #!/usr/bin/ksh

    NEW_USERS="/path/to/text_data_file"
    HOME_BASE="/home/"

    cat ${NEW_USERS} | \
    while read USER PASSWORD GROUP
    do
    useradd -g ${GROUP} -p ${PASSWORD} -m -d ${HOME_BASE}${USER} ${USER}
    done


    using this script i create users but i am not able to login why it was...

    I am really appreciate if some body helps me resolving this issue....

    Zameer Ahmed Syed
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    2
    Rep Power
    0
    Originally Posted by druuna
    Yep it is.

    Here's a quick and dirty example.

    You did not tell which info is in the text file and it's layout, so i'm goin to assume the following:

    - 1 line per newuser,
    - username password group are the space seperated fields
    Code:
    #!/usr/bin/ksh
    
    NEW_USERS="/path/to/text_data_file"
    HOME_BASE="/home/"
    
    cat ${NEW_USERS} | \
    while read USER PASSWORD GROUP
    do
      useradd -g ${GROUP} -p ${PASSWORD} -m -d ${HOME_BASE}${USER} ${USER}
    done
    From here you should be able to expand/add/subtract things you want/don't want.
    hey,
    I am supposed to do a big project on unix shell script, but i dont really know wot to do, any1 can help me and give me some ideas about some projects involving shell script? e.g. internet analyzer using shell?
    pliz contact me on nima_h_S@yahoo.com
    regards
    Nima
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Posts
    1
    Rep Power
    0
    I am using the above code to add users to a red hat Linux box; it works, but I want to add a name to the account useradd -c 'Test Test' but I am unable cause the space as the delimiter in the file that the script reads thinks a first and last name are two variables. Also, the password reset part the script does not work. The script will run but the password I set in the file does not work allow the user to log in. Any help would be great.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    adding samba


    thanks druuna

    The script worked. Now what i need is to allow all these users to be able to access samba. To this i need to execute the following
    for every single user that i created by using ur shell script


    Code:
    sudo smbpasswd -a username
    New smb password : -------
    retype new smb password: -----
    Can you design a shell script for me to do this??

    Thanks
  18. #10
  19. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by quarkhirad
    thanks druuna

    The script worked. Now what i need is to allow all these users to be able to access samba. To this i need to execute the following
    for every single user that i created by using ur shell script


    Code:
    sudo smbpasswd -a username
    New smb password : -------
    retype new smb password: -----
    Can you design a shell script for me to do this??

    Thanks
    Have you ever used expect? That's why I use to generate samba accounts.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0
    bullet thanks

    But no i never have used expect. Can you please give me some details. Also note i am using ubuntu 7.04.
  22. #12
  23. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by quarkhirad
    bullet thanks

    But no i never have used expect. Can you please give me some details. Also note i am using ubuntu 7.04.
    This is a simple expect script for doing this.

    Code:
    #!/usr/bin/expect
    
    spawn /usr/local/samba/bin/smbpasswd -a [lindex $argv 0]
    
    expect "New*:" {send "[lindex $argv 1]\r"}
    expect "Retype*:" {send "[lindex $argv 1]\r"}
    expect "Password*" {break}
       
    exit 0
    Here, of course, I assume expect is in /usr/bin, and smbpasswd is in /usr/local/samba/bin

    Assuming the script is named smbpasswd.exp, it would be called like this.

    /path-to/smbpasswd.exp username password
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0
    Hi Druuna, I am implementing the adduser script above buit it doesnt work out for me, i have this data file format:
    leonard 123456
    Bernard 123456
    Elizabe 123456
    Wakareg 123456

    Thats' username and password
    then the script i wrote is this:

    #!/bin/bash

    NEW_USERS="user_accounts"
    HOME_BASE="/home/"

    cat ${NEW_USERS} | \
    while read USER PASSWORD
    do
    useradd -g ${PASSWORD} -m -d ${HOME_BASE}${USER} ${USER}
    done

    note: user_accounts is the name of the data file, and both the script and data file are in same directory. And the script name is user_script

    then am executing like this ./user_script
    when i press enter, this is the error i am getting:

    [root@hoasnet-fe32dd00-34 Lab4]# ./user_script
    useradd: unknown group 123456
    useradd: unknown group 123456
    useradd: unknown group 123456
    useradd: unknown group 123456
    useradd: invalid numeric argument '-m'


    Please advice

    Thanks
    Lennie

IMN logo majestic logo threadwatch logo seochat tools logo