-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk_modify_users.sh
executable file
·123 lines (115 loc) · 3.4 KB
/
bulk_modify_users.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
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
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
# bulk modify users, even on several servers.
# config file needed (config.csv)
# fields:
# server_name,API_key
# 2021-06 Sascha Rommelfangen, CIRCL, SMILE
command="$1"
user="$2"
function parse (){
encapsulated="$1"
shift
IFS=$' '
if [[ "$encapsulated" == "true" ]]
then
{ read id; read email; read last; read current; read alert; read disabled; } <<< `echo $@ | jq -r ".[].User.id, .[].User.email, .[].User.last_login, .[].User.current_login, .[].User.autoalert, .[].User.disabled"`
else
{ read id; read email; read last; read current; read alert; read disabled; } <<< `echo $@ | jq -r ".User.id, .User.email, .User.last_login, .User.current_login, .User.autoalert, .User.disabled"`
fi
echo -e " User ID:\t${id}"
echo -e " Email:\t\t${email}"
echo -e " Prev. Login:\t$(date -r ${last})"
echo -e " Current Login:\t$(date -r ${current})"
echo -e " Autoalert:\t${alert}"
echo -e " Disabled:\t${disabled}"
}
function search (){
for line in `cat config.csv | grep -v '^#'`
do
server=$(echo $line | cut -d "," -f 1)
echo -n "Working on server $server... "
apikey=$(echo $line | cut -d "," -f 2)
result=$(curl -s -H "Authorization: ${apikey}" -H "Accept: application/json" -H "Content-Type: application/json" -X GET https://${server}/users/admin_index/value:${user}.json | jq )
if [[ "$result" != '[]' ]]
then
echo "found $user"
parse true "$result"
else echo "$user not found"
fi
done
}
function modify_user (){
action="$1"
case "$action" in
disable_autoalert)
server_action='{ "autoalert": false }'
;;
enable_autoalert)
server_action='{ "autoalert": true }'
;;
disable_user)
server_action='{ "disabled": true }'
;;
enable_user)
server_action='{ "disabled": false }'
;;
esac
for line in `cat config.csv | grep -v '^#'`
do
server=$(echo $line | cut -d "," -f 1)
echo -n "Working on server $server... "
apikey=$(echo $line | cut -d "," -f 2)
result=$(curl -s -H "Authorization: ${apikey}" -H "Accept: application/json" -H "Content-Type: application/json" -X GET https://${server}/users/admin_index/value:${user}.json | jq)
user_id=$(echo $result | jq -r ".[].User.id")
if [[ ! "$user_id" ]]
then
echo "$user not found"
else
echo "Modifying user $user ($user_id): $server_action"
result=$(curl -s -H "Authorization: ${apikey}" -H "Accept: application/json" -H "Content-Type: application/json" -d "$server_action" -X POST https://${server}/admin/users/edit/${user_id})
if [[ "$result" != '[]' ]]
then
parse false "$result"
else echo "not found"
fi
fi
done
}
function show_help {
echo -e "Usage:\t$0 [-s|-d|-x] email_address"
echo -e "\t$0 -s email_address (search user by email address)"
echo -e "\t$0 -d email_address (disable email auto alerts)"
echo -e "\t$0 -e email_address (enable email auto alerts)"
echo -e "\t$0 -x email_address (disable user)"
echo -e "\t$0 -y email_address (enable user)"
}
case "$command" in
-s)
search $user
exit 0
;;
-e)
modify_user enable_autoalert $user
exit 0
;;
-d)
modify_user disable_autoalert $user
exit 0
;;
-x)
modify_user disable_user $user
exit 0
;;
-y)
modify_user enable_user $user
exit 0
;;
-h)
show_help
exit 0
;;
*)
show_help
exit 0
;;
esac