Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, Im trying to create regular user accounts from a text file. I have a text file with a list of users that need to be created. Can I use ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2003
    Posts
    56

    User Account Script


    Hi,
    Im trying to create regular user accounts from a text file. I have a text file with a list of users that need to be created. Can I use a script to do this? Thanks

  2. #2
    Linux Engineer
    Join Date
    Apr 2003
    Location
    Sweden
    Posts
    796
    OK...lets say your list looks like this and are called users.txt and are located in /tmp do like this

    File format:
    Code:
    nisse1
    user1
    user2
    user3
    user4
    user5
    user6
    The program below creating the accounts...

    Code:
    #!/bin/sh
    #
    # Creator:andutt@linuxforums.org
    #
    # Creates users from a file
    #
    FILE=/tmp/users.txt
    
    if [ ! -f $FILE ]
       then
       echo "ERROR $FILE doesnt exist.."
       exit 1
    else
      echo "Creating user accounts from $FILE"
    fi
    
    USERS=`cat $FILE`
    
    for i in $USERS
         do
         echo "Creating user:$i"
         useradd -g users -m -c "Autocreated account $i" $i
    done
    
    echo
    echo "Usercreation is completed"
    echo
    Regards

    Andutt

  3. #3
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217
    You still end up manually setting the passwords. That's the worst part.
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  4. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    That can be rather easily remedied, though.
    Use an input file of this format instead:
    Code:
    user1
    pass1
    user2
    pass2
    ...
    Then use the following script.
    Code:
    #!/bin/sh
    if [ $# -lt 1 ]; then
        echo "Usage: $0 [ -g grpname ] [ -r ] filename ..." >&2
        exit 1
    fi
    correct=
    group=users
    getpwd=y
    while [ $# -ge 0 ]; do
        o="$1"
        shift
        case "$o" in
            -g)
                group="$1"
                shift
                ;;
            -r)
                if [ -z "$getpwd" ]; then
                    getpwd=y
                else
                    getpwd=
                fi
                ;;
            *)
                correct=y
                if [ "$o" != - ]; then
                    exec 3<"$o"
                else
                    exec 3<&0
                fi
                while &#58;; do
                    read username <&3
                    if &#91; -n "$getpwd" &#93;; then
                        read password <&3
                    else
                        password="$&#40;dd bs=6 count=1 </dev/urandom 2>/dev/null | mimencode&#41;"
                        echo "$username $password" >>passwords
                    fi
                    if &#91; -z "$username" &#93;; then break; fi
                    useradd -g "$group" -m "$username"
                    passwd --stdin "$username" <<<"$password"
                done
                exec 3<&-
                ;;
        esac
    done
    if &#91; -z "$correct" &#93;; then
        echo "No users were created" >&2
    fi
    As you can see, this script also supports generation of 8-character random passwords by using the -r switch. In that case, the input file format is similar to what andutt suggested and it outputs the passwords to a file called "passwords". It also supports multiple input files and usage of stdin. For example, the following usage might be convenient:

    Code:
    awk '&#123;if&#40;$1 == "thisschool"&#41; print $2;&#125;' | ./genuser -g teachers teachers.in -g students -r -
    Note, however, that I haven't actually tried this script; I just wrote it down now. Therefore, check it through for typos before using it in a real environment.

  5. #5
    Just Joined!
    Join Date
    Feb 2003
    Posts
    56

    Script

    Thanks a lot for your help, the scripts worked great.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •