Scripts
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb Site ManagementScripts

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old September 7th, 2003, 11:12 PM
stunned stunned is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: The Little Red Dot
Posts: 149 stunned User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 23 m 6 sec
Reputation Power: 5
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

Reply With Quote
  #2  
Old September 8th, 2003, 06:35 AM
druuna druuna is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 137 druuna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 37 sec
Reputation 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.

Reply With Quote
  #3  
Old September 9th, 2003, 07:34 PM
stunned stunned is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: The Little Red Dot
Posts: 149 stunned User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 23 m 6 sec
Reputation Power: 5
an input file will be provided.

There will be a full name included in the input file

Reply With Quote
  #4  
Old September 10th, 2003, 05:20 PM
druuna druuna is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 137 druuna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 37 sec
Reputation 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 05:28 PM.

Reply With Quote
  #5  
Old September 10th, 2003, 08:08 PM
stunned stunned is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: The Little Red Dot
Posts: 149 stunned User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 23 m 6 sec
Reputation Power: 5
Thanks druuna. For the effort and help. Theres some minor glitches but its working fine now.

Reply With Quote
  #6  
Old September 11th, 2003, 03:24 AM
druuna druuna is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 137 druuna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 37 sec
Reputation Power: 0
You're welcome.

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb Site ManagementScripts > Help modify this script


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway