-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.sh
83 lines (78 loc) · 2.51 KB
/
client.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
while true; do
# Take input from user, for add or remove servers
echo -n "Enter home/rep/add/rm/exit: "
read action
# Check if action is add or rm (case insensitive)
if [ "${action,,}" == "add" ]; then
echo -n "Enter number of servers to add: "
read n
echo -n "Enter hostnames of servers to add (seperated by spaces): "
read -a hostnames
# Check if hostnames are valid
for hostname in "${hostnames[@]}"
do
if [[ ! $hostname =~ ^[a-zA-Z0-9_@-]+$ ]]; then
echo "Invalid hostname"
fi
done
# Check if number of servers is valid
if [ $n -lt 0 ]; then
echo "Invalid number of servers"
fi
# hostname_string="[\"$(IFS='","'; echo "${hostnames[*]}")\"]"
hostname_string=""
for element in "${hostnames[@]}"; do
hostname_string+="\"$element\","
done
hostname_string="${hostname_string%,}" # Remove trailing comma
# hostname_string+="]"
# echo $hostname_string
# echo $n
# Send request to server with hostnames and number of servers
curl -X POST \
-H "Content-type: application/json" \
-d "{\"n\":$n, \"hostnames\": [$hostname_string]}" \
"http://0.0.0.0:5000/add"
echo ""
elif [ "${action,,}" == "rm" ]; then
echo -n "Enter number of servers to remove: "
read n
echo -n "Enter hostnames of servers to remove (seperated by spaces): "
read -a hostnames
# Check if hostnames are valid
for hostname in "${hostnames[@]}"
do
if [[ ! $hostname =~ ^[a-zA-Z0-9_@-]+$ ]]; then
echo "Invalid hostname"
fi
done
# Check if number of servers is valid
if [ $n -lt 0 ]; then
echo "Invalid number of servers"
fi
hostname_string=""
for element in "${hostnames[@]}"; do
hostname_string+="\"$element\","
done
hostname_string="${hostname_string%,}" # Remove trailing comma
# hostname_string+="]"
# echo $hostname_string
# Send request to server
curl -X DELETE \
-H "Content-type: application/json" \
-d "{\"n\":$n, \"hostnames\": [$hostname_string]}" \
"http://0.0.0.0:5000/rm"
echo ""
elif [ "${action,,}" == "home" ]; then
curl -X GET "http://0.0.0.0:5000/home"
echo ""
elif [ "${action,,}" == "rep" ]; then
curl -X GET "http://0.0.0.0:5000/rep"
echo ""
elif [ "${action,,}" == "exit" ]; then
break
else
echo "Invalid action"
fi
done
exit 0