-
Notifications
You must be signed in to change notification settings - Fork 17
/
nextcloud-bot-pass.sh
82 lines (68 loc) · 1.8 KB
/
nextcloud-bot-pass.sh
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
#!/bin/bash
# By Georgiy Sitnikov.
#
# AS-IS without any warranty
#
# To added to Nextcloud please execute:
# sudo -u www-data php /var/www/nextcloud/occ talk:command:add pass pass "/usr/local/bin/nextcloud-bot-pass.sh {ARGUMENTS}" 1 3
#
# More info under https://nextcloud-talk.readthedocs.io/en/latest/commands/
# Default password length
length=20
###
while test $# -gt 0; do
case "$1" in
--help)
echo "/pass - A Nextcloud Talk chat wrapper random pass generation"
echo " "
echo "Simple execution: /pass"
echo "Complex execution to get 8 characters pass: /pass 8"
echo "Default password length is $length"
exit 0
;;
*)
break
;;
esac
done
if [ ! -z "$1" ]; then
length="$1"
# check if it is a number
# https://stackoverflow.com/questions/806906/how-do-i-test-if-a-variable-is-a-number-in-bash
re='^[0-9]+$'
if ! [[ $length =~ $re ]] ; then
echo "Not a number :(" >&2
exit 1
fi
fi
# Use urandom if presented
if [ -r "/dev/urandom" ]; then
echo "Generated with Urandom"
< /dev/urandom tr -dc A-Za-z0-9 | head -c"$length"
echo
exit 0
fi
# Use openssl if presented
if [ -x "/usr/bin/openssl" ]; then
echo "Generated with OpenSSL"
/usr/bin/openssl rand -base64 "$length" | head -c"$length"
echo
exit 0
fi
# User bash Random if nothing from above presented
# https://stackoverflow.com/questions/26665389/random-password-generator-bash
echo "Generated with Bash random"
choose() { echo ${1:RANDOM%${#1}:1} $RANDOM; }
{
choose '!@#$%^\&'
choose '0123456789'
choose 'abcdefghijklmnopqrstuvwxyz'
choose 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for i in $( seq 1 "$length" )
# for i in $( seq 1 $(( 4 + RANDOM % 8 )) )
do
choose '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
done
} | sort -R | awk '{printf "%s",$1}' | head -c"$length"
echo
exit 0