-
Notifications
You must be signed in to change notification settings - Fork 1
/
scriptExample1command.sh
211 lines (163 loc) · 5.04 KB
/
scriptExample1command.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
# Function to send a POST request and get the task ID
function send_post_request() {
local url="$1"
local oauthToken="$2"
local data="$3"
# Send POST request and capture the task ID
task_id=$(curl -s -k -X POST -H "Authorization: $oauthToken" -H "Content-Type: application/json" -d "$data" "$url" | jq -r '.id')
echo "$task_id"
}
# Function to check the status of a task using a GET request
function get_task() {
local url="$1"
local oauthToken="$2"
# Send GET request to check task status
task=$(curl -s -k -H "Authorization: $oauthToken" "$url")
echo "$task"
}
# Function to check the status of a task using a GET request
function get_workers() {
local url="$1"
local oauthToken="$2"
# Send GET request to check task status
workers=$(curl -s -k -H "Authorization: $oauthToken" "$url")
echo "$workers"
}
function wait_task(){
local url="$1"
local oauthToken="$2"
local task_id="$3"
# Wait for task done
while true; do
task=$(get_task "$url/task/$task_id" "$oauthToken" )
status=$(echo $task | jq -r '.status')
# Check if the status is not equal to "working"
if [ "$status" == "done" ]; then
#echo "Task completed successfully. Status: $status"
echo $task
break # Exit the loop
else
#echo "Task still in progress. Status: $status"
sleep 1 # Adjust the sleep duration as needed
fi
done
}
function wait_tasks(){
local url="$1"
local oauthToken="$2"
shift 2
local task_ids=("$@")
output_array=()
pids=()
echo -n '{"result":['
# Loop ids
array_length=${#task_ids[@]}
for ((i=0; i<array_length; i++)); do
task="${task_ids[i]}"
#for task in "${task_ids[@]}"; do
# execute in background
wait_task $url $oauthToken $task &
pids[$i]=$!
# Remove from array
task_ids=("${task_ids[@]/$task}")
done
# Wait for all background processes to finish
for pid in "${pids[@]}"; do
wait $pid
done
wait
echo ']}'
}
# Define vars
## oauth token, IP, port, range to scan
oauthToken="WLJ2xVQZ5TXVw4qEznZDnmEE2"
nTaskIP="127.0.0.1"
nTaskPort="8080"
url="https://$nTaskIP:$nTaskPort"
# Array to store task IDs
task_ids=()
WORKERS=$(get_workers "$url/worker" "$oauthToken")
echo "$WORKERS" | jq
# Get all workers and send the same task to each
for row in $(echo "${WORKERS}" | jq -r '.[] | @base64'); do
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
NAME=$(_jq '.name')
command="{\"module\": \"curl\", \"args\": \"-s ipinfo.io/ip\"}"
task_data="{\"command\": [$command],\"priority\": 0, \"workerName\": \"$NAME\"}"
task_id=$(send_post_request "$url/task" "$oauthToken" "$task_data")
echo task_id $task_id
# add task_id to array
task_ids+=("$task_id")
done
# Wait for task done
OUTPUTS=($(wait_tasks $url $oauthToken "${task_ids[@]}"))
# Fix jq errors, no comma in json
AUX=${OUTPUTS[@]}
REPLACED='} {'
REPLACE='}, {'
FINAL=${AUX//$REPLACED/$REPLACE}
echo "Results:"
#echo "${OUTPUTS[@]}" | jq
#echo "${FINAL}" | jq
for row in $(echo "${FINAL}" | jq -r '.result[] | @base64'); do
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
echo -e module: $(_jq '.command[0].module')
echo -e args: $(_jq '.command[0].args')
echo -e workerName: $(_jq '.workerName')
echo -e "output: $(_jq '.command[0].output')"
echo
done
exit 0
# Connect to Manager
## Send nmap ping only range
command="{\"module\": \"nmapIPs\", \"args\": \"$scanRange\"}"
task_data="{\"command\": [$command],\"priority\": false}"
task_id=$(send_post_request "$url/task" "$oauthToken" "$task_data")
# add task_id to array
task_ids+=("$task_id")
echo task_ids:
for task in "${task_ids[@]}"; do
echo "$task"
done
# Wait for task done
OUTPUTS=($(wait_tasks $url $oauthToken "${task_ids[@]}"))
# Fix jq errors, no comma in json
OUTPUTS=$(echo ${OUTPUTS[@]} | sed 's/} {/},{/g')
# output first
OUTPUT=$(echo "${OUTPUTS}" | jq .result[0].command[0].output | tr -d '"')
# For each IP execute nmap again but complete as a task
for IP in `echo -e $OUTPUT` ; do
echo $IP
command="{\"module\": \"nmap\", \"args\": \"$IP\"}"
task_data="{\"command\": [$command],\"priority\": false}"
task_id=$(send_post_request "$url/task" "$oauthToken" "$task_data")
echo task_ids: $task_id
# add task_id to array
task_ids+=("$task_id")
done
# Wait for task done
OUTPUTS=($(wait_tasks $url $oauthToken "${task_ids[@]}"))
# Fix jq errors, no comma in json
AUX=${OUTPUTS[@]}
REPLACED='} {'
REPLACE='}, {'
FINAL=${AUX//$REPLACED/$REPLACE}
echo "NMAP results:"
#echo "${OUTPUTS[@]}" | jq
#echo "${FINAL}" | jq
for row in $(echo "${FINAL}" | jq -r '.result[] | @base64'); do
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
echo -e module: $(_jq '.command[0].module')
echo -e args: $(_jq '.command[0].args')
echo -e workerName: $(_jq '.workerName')
echo -e "output: $(_jq '.command[0].output')"
echo
done
exit 0