forked from sib-swiss/AWS-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_credentials
More file actions
executable file
·112 lines (88 loc) · 2.72 KB
/
generate_credentials
File metadata and controls
executable file
·112 lines (88 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
USAGE="Usage: generate_credentials -l <user list> [-o <outdir>] \n
\n
This command generates credentials for users\n
\n
-l CSV file with users, with 2 columns: first name and last name. Required. \n
-o output directory. Default: ./credentials \n
-p port start. Generates ports and starts counting from this port. E.g. for Rstudio server count from 9001. Default: 10001\n
-a address to use in the link (usually IP address). Required."
while getopts ":l:o:p:a:" opt
do
case $opt in
l)
LIST=$OPTARG
;;
o)
OUTDIR=$OPTARG
;;
p)
PORTSTART=$OPTARG
;;
a)
IP=$OPTARG
;;
\?)
echo -e "Invalid option: -$OPTARG \n" >&2
echo -e $USAGE >&2
exit 1
;;
:)
echo -e "Option -$OPTARG requires an argument. \n"
echo -e $USAGE >&2
exit 1
;;
esac
done
# return usage if no options are passed
if [ $OPTIND -eq 1 ]
then
echo -e "No options were passed. \n" >&2
echo -e $USAGE >&2
exit 1
fi
# required options
if [ "$LIST" == "" ]; then echo "option -l is missing, but required">&2 && exit 1; fi
if [ "$IP" == "" ]; then echo "option -a is missing, but required">&2 && exit 1; fi
# default values
if [ "$OUTDIR" == "" ]; then OUTDIR=./credentials; fi
if [ "$PORTSTART" == "" ]; then PORTSTART=10001; fi
# get script source directory to not break secondary script dependencies
SOURCEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
TMPULIST=`mktemp`
TMPTNAMES=`mktemp`
TMPUNAMES=`mktemp`
TMPPASSWD=`mktemp`
TMPCOMB=`mktemp`
TMPPORT=`mktemp`
mkdir -p $OUTDIR
# check if end of file new line exists
# and add it if not
cat $LIST | tail -c1 | read -r _ || echo >> $LIST
# removing carriage returns and spaces
cat $LIST | tr -d '\015\040' > $TMPULIST
LIST=$TMPULIST
FIRSTL=`cat "$LIST" | cut -f 1 -d ',' | tr -cd '\11\12\15\40-\176' | tr [:upper:] [:lower:] | cut -c-1`
LASTN=`cat "$LIST" | cut -f 2 -d ',' | tr -cd '\11\12\15\40-\176' | tr [:upper:] [:lower:]`
USERNAMES=$(paste -d '-' <(echo "$FIRSTL") <(echo "$LASTN") | tr -d '-')
cat "$LIST" | tr ',' '\t' > $TMPTNAMES
paste $TMPTNAMES <(echo $USERNAMES | tr ' ' '\n') >> $TMPUNAMES
rm $TMPTNAMES
# generate empty password file
if [ -f $TMPPASSWD ]
then
rm $TMPPASSWD
touch $TMPPASSWD
fi
for user in $USERNAMES
do
openssl rand -hex 4 >> $TMPPASSWD
done
paste $TMPUNAMES $TMPPASSWD > $TMPCOMB
cat $TMPCOMB \
| awk -v portstart=$PORTSTART -v address=$IP -v OFS='\t' \
'{print NR+portstart-1, "http://"address":"NR+portstart-1, $0}' \
> $TMPPORT
cat $TMPPORT | cut -f 1,5,6 > $OUTDIR/input_docker_start.txt
cat $TMPPORT | awk -v OFS='\t' 'BEGIN {print "first", "last", "password", "link"}{print $3, $4, $6 ,$2}' > $OUTDIR/user_info.txt
rm $TMPULIST $TMPPASSWD