#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

    Help modify this script


    Can anyone help me modify this shell script so its takes in the login name and password from a text file?

    I want to automate the process by making the script retrieve data from a text file and automatically creating users in the RH8 i'm using. Thanks in advance for any help.

    --------------------------------------------------------------------------------
    #!/bin/sh
    if [ "$UID" != "0" ]; then
    echo "You must be root"
    exit 1
    fi

    SHELLTOUSE="/bin/bash" #Change default shell here
    printf "%s" "Enter Full Name: "
    read FULLNAME
    printf "%s" "Enter SHELL to use (default:$SHELLTOUSE) :"
    read TEMP
    if [ -n "$TEMP" ]; then
    SHELLTOUSE="$TEMP"
    fi
    printf "%s" "Enter group: "
    read USERGROUP
    printf "%s" "Enter login name: "
    read LOGINNAME
    printf "\n"
    echo "Name:$FULLNAME Shell:$SHELLTOUSE Group:$USERGROUP login:$LOGINNAME "
    printf "%s\n" "Is all this correct?"
    read ANSWER
    case "$ANSWER" in
    y | Y | Yes | YES )
    $(adduser "$LOGINNAME" -c "$FULLNAME" -s "$SHELLTOUSE" -g
    "$USERGROUP" )
    /usr/bin/passwd "$LOGINNAME"
    echo "User added"
    ;;
    n | N | No | NO )
    echo "Aborting adduser"
    exit 1
    ;;
    esac
    exit 0
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    137
    Rep Power
    0
    I'm not entirely sure what it is you want.

    - Do you want it to be able to run manual (no input file given) and automated (input file provided)?

    - If an input file is given, do you still want (all) the questions?

    - If the input file only contains login and password, do you want the fullname (-c option) to be the same as the loginname?

    Some other points you might want to consider:

    - check to see if shell exists (found in /etc/shell)
    - check to see if group exists (found in /etc/group)
    - check if login name already exist (found in /etc/passwd)

    The above 3 points will be checked by useradd, but it will exit with an error message if something isn't correct. Checking this during input might be better.
  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
    an input file will be provided.

    There will be a full name included in the input file
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    137
    Rep Power
    0
    Ok, here's my 2 cents

    Your input file must have the following layout:

    fullname:loginname:usergroup:shelltouse

    I used ':' as seperator instead of ' '. The fullname field can contain spaces (among other charachters), so that's why I choose ':'. So the following line is valid:

    John Doe, Nowhereville (NL):stunned:users:/usr/bin/ksh

    If no shelltouse is given _or_ the given shell is not valid, the default shell will be used and an error will be written to an errorfile.

    If fullname and/or loginname and/or usergroup is missing the line will be ignored, an error will be written to an errorfile.

    If loginname already exists the line will be ignored, an error will be written to an errorfile.

    If given usergroup is not valid the line will be ignored, an error will be written to an errorfile.

    If an error occurs the script will keep running, it will only ignore lines which have errors in them. There will also be an errorfile present if something went wrong.

    You will be prompted before a user is added.

    I did try to use the code you provided, but it got kinda fragmented. Key parts are used.

    Code:
    #!/bin/sh 
    #---------------------------------------------------------------------begin--#
    #  create_users <inputfile>
    #  Purpose: add users using an input file
    #  Layout inputfile: fullname:loginname:usergroup:shelltouse
    #-----------------------------------------------------------
    
    # Variables
    DEFAULTSHELL="/bin/bash"                           # Change default shell here 
    LEGALSHELLS="/etc/shells"                          # File with valid shells
    LEGALGROUPS="/etc/group"                           # File with valid groups 
    
    PWDIR="`pwd`"
    EFILE="${PWDIR}/unable2create.`date '+%y%m%d'`"
    
    # Functions
    Check_All()
    {
      # is FULLNAME provided
      [[ -z ${FULLNAME} ]] && ALL_OK="Full name"
      # is LOGINNAME provided
      [[ -z ${LOGINNAME} ]] && ALL_OK="Login name"
      # is LOGINNAME valid
      grep "^${LOGINNAME}:" /etc/passwd > /dev/null 2>&1
      [ "$?" -eq 0 ] && ALL_OK="Loginname"
      # is USERGROUP provided
      [[ -z ${USERGROUP} ]] && ALL_OK="Usergroup"
      # is USERGROUP valid
      grep "^${USERGROUP}:" ${LEGALGROUPS} > /dev/null 2>&1
      [ "$?" -eq 1 ] && ALL_OK="Usergroup"
    }
    
    # Main
    clear
    
    echo ""
    echo "Running........"
    echo ""
    
    rm ${EFILE} > /dev/null 2>&1
    
    # Is inputfile provided:
    [[ -z $1 ]] && echo "Error: No input file given. Quiting." && exit 1
    
    INFILE=$1
    
    # Script run with root privilage:
    if [ "$UID" != "0" ]
    then 
      echo "Error: You must be root." 
      exit 1 
    fi 
    
    # Need the following to answer questions in a pipe
    exec 3>&0
    
    # Process inputfile:
    
    cat ${INFILE} | \
    while read ONELINE
    do
      FULLNAME="`echo ${ONELINE} | awk -F: '{ print $1 }'`"
      LOGINNAME="`echo ${ONELINE} | awk -F: '{ print $2 }'`"
      USERGROUP="`echo ${ONELINE} | awk -F: '{ print $3 }'`"
      SHELLTOUSE="`echo ${ONELINE} | awk -F: '{ print $4 }'`"
    
      # Check input fields
      ALL_OK="0"
      Check_All
      if [[ ${ALL_OK} = "0" ]]
      then
        # All is well: Let's continue......
    
        # is SHELLTOUSE provided
        if [[ -z ${SHELLTOUSE} ]]
        then
          # Use default shell
          SHELLTOUSE="${DEFAULTSHELL}"
        else
          # Is choosen shell valid
          grep "^${SHELLTOUSE}$" ${LEGALSHELLS} > /dev/null 2>&1
           if [ "$?" -eq 1 ]
           then
             # shell not valid, use default instead.
             SHELLTOUSE="${DEFAULTSHELL}"
             echo "Shell-> ${ONELINE}" >> ${EFILE}
           fi
        fi
    
        # Show and ask
        echo "Name  : $FULLNAME"
        echo "Login : $LOGINNAME" 
        echo "Group : $USERGROUP"
        echo "Shell : $SHELLTOUSE"
        echo ""
        printf "%s\n" "Is all this correct?"
        <&3 read ANSWER
    
        case "$ANSWER" in
          y | Y | Yes | YES )
            $(adduser "$LOGINNAME" -c "$FULLNAME" -s "$SHELLTOUSE" -g "$USERGROUP")
            <&3 /usr/bin/passwd "$LOGINNAME"
            echo ""
          ;;
          n | N | No | NO )
            echo ""
            echo "Ignoring this line of input, will continue with next line."
          ;;
        esac
      else
        # Something went wrong: Write to errorfile, set token and continue
        echo "${ALL_OK}-> ${ONELINE}" >> ${EFILE}
      fi
    
    done
    
    # Did something go wrong
    if [[ -a ${EFILE} ]]
    then
      # Yep
      echo ""
      echo "-----------------------------------------------------------------------"
      echo "Something went wrong during processing, please check:"
      echo ""
      echo "${EFILE}"
    fi
    
    # Done
    echo ""
      echo "----------------------------------------------------------------------------"
    echo "Done."
    echo ""
    
    exit 0
    #-----------------------------------------------------------------------end--#
    Last edited by druuna; September 10th, 2003 at 06:28 PM.
  8. #5
  9. 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 druuna. For the effort and help. Theres some minor glitches but its working fine now.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    137
    Rep Power
    0
    You're welcome.

    Tell me what the glitches are and I might be able to help.
    Thought it was fail-prove ;-)

IMN logo majestic logo threadwatch logo seochat tools logo