Skip to content

Instantly share code, notes, and snippets.

@hiramhuang
Created November 13, 2015 01:37
Show Gist options
  • Save hiramhuang/2483951f83fc4329a072 to your computer and use it in GitHub Desktop.
Save hiramhuang/2483951f83fc4329a072 to your computer and use it in GitHub Desktop.
Automatic create continuous user.
#!/bin/bash
if [ $(id -u) -eq 0 ]; then
# as root user
read -p "Username prefix: " username
read -p "Password: " password
read -p "Group: " group
read -p "Quantity:" quantity
read -p "Shell: " login
grep -E "^$group" /etc/group > /dev/null
if [ $? -ne 0 ]; then
groupadd $group
echo "Group $group created."
fi
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
current=0
while [ $current -le "$quantity" ]
do
grep -E "^$username$current" /etc/passwd > /dev/null
if [ $? -eq 0 ]; then
echo "User $username$current is exists (Passed)"
current=$(($current+1))
continue
fi
useradd -m -s $login -p $pass -g $group $username$current
echo "User $username$current has been created."
current=$(($current+1))
done
[ $? -eq 0 ] && echo "All usr has been created!" || echo "Failed"
else
echo "Only allow root user to add user!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment