Skip to content

Instantly share code, notes, and snippets.

@naotaco
Last active October 5, 2018 16:44
Show Gist options
  • Save naotaco/21e9335538cecf03ad42e25c6a0b497e to your computer and use it in GitHub Desktop.
Save naotaco/21e9335538cecf03ad42e25c6a0b497e to your computer and use it in GitHub Desktop.
適当にnginx用のid/password生成する君
#!/bin/bash
# Usage: ./gen_users.sh [num] [dest_dir(option)]
NUM=$1 # number of accounts
DIR_NAME=$2
PW_FILE=./.htpasswd # for nginx setting
CSV_FILE=./id_list.csv # for print
if [ -z $DIR_NAME ]; then
DIR_NAME=$(date +%F_%H_%M_%S)
else
PW_FILE=./.htpasswd_$DIR_NAME
CSV_FILE=./id_list_${DIR_NAME}.csv
fi
if [ -e $DIR_NAME ]; then
echo "Already exists. Remove it first."
exit 1
fi
mkdir $DIR_NAME
cd $DIR_NAME
touch $PW_FILE
echo "id,password" > $CSV_FILE
echo "Generating accounts in $DIR_NAME ..."
for ((i=0; i<NUM; i++));
do
line=$(pwgen -C -A 8 2) # generate two random strings as ID/PW
echo $line | sed -e 's/\ /,/g' >> $CSV_FILE
htpasswd -b -m $PW_FILE $line > /dev/null 2>&1
printf .
done
echo "$NUM accounts created."
@naotaco
Copy link
Author

naotaco commented Oct 5, 2018

usage:

$ ./gen_useres.sh 100 content_name
Generating accounts in content_name ...
....................................................................................................100 accounts created.

$ tree content_name/ -a
content_name/
├── .htpasswd_content_name
└── id_list_content_name.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment