-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiot-endpoint-debug
executable file
·301 lines (238 loc) · 7.06 KB
/
iot-endpoint-debug
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/bin/bash -e
CPWD="$( cd "$(dirname "$0")" ; pwd -P )"
#----------------------------------------------------#
# COMMON ATTRIBUTES #
#----------------------------------------------------#
source $CPWD/scripts/iot-utils
#----------------------------------------------------#
# DEFINES / MACROS #
#----------------------------------------------------#
LOG=$LOGS_DIR/iot-firmware-debug.log
#----------------------------------------------------#
# CMDLINE OPTIONS #
#----------------------------------------------------#
ARGC=$#
ARGV=("$@")
VERBOSE=false
HELP=false
IDEBUG=false
ISHELL=false
IP=
# Clear args
set --
COUNT=0
while [ $COUNT -lt $ARGC ]; do
STRIP=${ARGV[COUNT]}
ID=0
LONGFORMAT=`echo $STRIP | cut -b 1-2`
if [ "x$LONGFORMAT" == "x--" ]; then
STRIP=`echo ${ARGV[COUNT]} | cut -b 2-3`
else
ID=`echo $STRIP | cut -b 3-`
STRIP=`echo $STRIP | cut -b 1-2`
fi
if [ "x$STRIP" == "x-v" ]; then
VERBOSE=true
elif [ "x$STRIP" == "x-i" ]; then
COUNT=$((COUNT+1))
IP=${ARGV[COUNT]}
if [ "x$IP" == "x" ]; then
pl
pr "Error: Unknown argument"
COUNT=$ARGC
HELP=true
fi
elif [ "x$STRIP" == "x-h" ]; then
HELP=true
elif [ "x$STRIP" == "x-d" ]; then
IDEBUG=true
elif [ "x$STRIP" == "x-s" ]; then
ISHELL=true
else
pl
pr "Error: Unknown argument"
COUNT=$ARGC
HELP=true
fi
COUNT=$((COUNT+1))
done
log "Started"
pl
pg "iot-firmware-debug ($VERSION)"
if [ "$HELP" == "true" ]; then
pn "Usage:"
pl
pn "Debug payload on domain specific device."
pl
pn "iot-firmware-debug [-v] [-h] ..."
pl
pn "Help:"
pl
pn "-v (--verbose) Verbose output for operations"
pn "-i (--ip) Manually supply endpoint IP"
pn "-h (--help) Print this"
pn "-d (--debug) Interactive shell after payload run"
pn "-s (--shell) Interactive shell only"
pl
exit 0
fi
#----------------------------------------------------#
# PREPARE #
#----------------------------------------------------#
pn
pg ":: Setup & Checks ::"
pn
common_setup
log "Common Setup OK"
#----------------------------------------------------#
# BODY #
#----------------------------------------------------#
pl
pg ":: Device Selection ::"
pl
DEVICE=$(device)
if [ -z "$DEVICE" ] || [ ! -d $IOT_DEVICE_DIR/$DEVICE ]; then
if [ "x$ADMIN" == "x1" ]; then
select_device
else
err "No default device is set. Without Admin mode we cannot proceed."
fi
else
# Default device (Non-admin mode compatible)
pnn "- Device selected : "
pg "$DEVICE"
fi
pl
pg ":: Domain Selection ::"
pl
DOMAIN=$(domain)
if [ -z "$DOMAIN" ] || [ ! -d $MASTER_DB/$DOMAIN ]; then
if [ "x$ADMIN" == "x1" ]; then
select_domain
else
err "No default domain is set. Without Admin mode we cannot proceed."
fi
else
# Single Default domain (Non-admin mode compatible)
CUST=$(dirname $DOMAIN)
SITE=$(basename $DOMAIN)
fi
pnn "- Domain selected : "
pg "$CUST/$SITE"
pl
pg ":: Endpoint Selection ::"
pl
ENDPOINT=$(endpoint)
if [ -z "$ENDPOINT" ]; then
if [ "x$ADMIN" == "x1" ]; then
select_endpoint
else
err "No default endpoint is set. Without Admin mode we cannot proceed."
fi
fi
pnn "- Endpoint selected : "
if [ ! -z "$IP" ]; then
pg "Manual IP"
else
pg "$ENDPOINT"
fi
pl
pg ":: Debug Prepare ::"
pl
if [ ! -z "$IP" ]; then
pnn "- Manual Endpoint IP : "
ENDPOINT_IP=$IP
pg "$ENDPOINT_IP"
else
pnn "- Last Endpoint IP : "
get_endpoint_ip
pg "$ENDPOINT_IP"
fi
pnn "- Endpoint Last Seen : "
get_endpoint_ts
NOW=$(date --utc +%s)
DELTA=$((NOW - ENDPOINT_TS))
HOURS=$((DELTA / 3600))
MINS=$(((DELTA - (HOURS * 3600)) / 60))
SECS=$((DELTA - (HOURS * 3600) - (MINS * 60)))
pg "${HOURS}h-${MINS}m-${SECS}s ago"
pnn "- Endpoint Ping : "
run ping -c1 $ENDPOINT_IP
return_check "Failed to ping endpoint IP $ENDPOINT_IP"
pg "OK"
USER=iot
SSHPASS=training
export SSHPASS=$SSHPASS
pnn "- SSH Connection Test : "
run sshpass -e ssh -q -o ConnectTimeout=5 -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" $USER@$ENDPOINT_IP exit
return_check "Failed to connect to endpoint $ENDPOINT on IP $ENDPOINT_IP"
pg "Pass"
pnn "- Endpoint Match : "
SERIAL_VERIFY=$(sshpass -e ssh -q -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" $USER@$ENDPOINT_IP cat /proc/cpuinfo | grep Serial | cut -d':' -f2 | xargs)
if [ "$ENDPOINT" != "$SERIAL_VERIFY" ]; then
pr "The endpoint we are correcting to has a different endpoint serial ($SERIAL_VERIFY)."
pl
exit 1
fi
pg "Yes"
if [ "$ISHELL" = "false" ]; then
pnn "- Payload service stop : "
run sshpass -e ssh -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" $USER@$ENDPOINT_IP 'sudo systemctl stop payload || true'
return_check "Failed to connect to endpoint $ENDPOINT on IP $ENDPOINT_IP"
pg "Done"
pnn "- Injecting latest payload : "
run sshpass -e ssh -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" $USER@$ENDPOINT_IP 'sudo mkdir -p /run/x-payload; sudo chmod 777 /run/x-payload || true'
return_check "Failed to connect to endpoint $ENDPOINT on IP $ENDPOINT_IP"
run sshpass -e scp -r -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" $REPO_ROOT/payload/* $USER@$ENDPOINT_IP:/run/x-payload/
return_check "Failed to connect to endpoint $ENDPOINT on IP $ENDPOINT_IP"
pg "Done"
set +e
pl
pg "Running: payload/payload-start"
pl
pg "Press CTRL-C to exit ..."
pl
# Run payload
sshpass -e ssh -t -q -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" $USER@$ENDPOINT_IP 'cd /run/x-payload; sudo bash ./payload-start; echo; echo "Crash or exit detected, falling to terminal..."; echo; echo "Run \"exit\" to stop the debug session"; echo; bash'
if [ "$IDEBUG" = "true" ]; then
pl
pg "Starting terminal ..."
pl
pg "Run \"exit\" to return."
pl
# Shell
set +e
sshpass -e ssh -t -q -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" $USER@$ENDPOINT_IP 'cd /run/x-payload; sudo bash'
set -e
fi
pl
pg "Running: payload/payload-stop"
pl
pg "Press CTRL-C to exit ..."
pl
# Run payload
sshpass -e ssh -t -q -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" $USER@$ENDPOINT_IP 'cd /run/x-payload; sudo bash ./payload-stop'
set -e
pl
pnn "- Removing injected payload : "
run sshpass -e ssh -q -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" $USER@$ENDPOINT_IP 'sudo rm -rf /run/x-payload || true'
return_check "Failed to connect to endpoint $ENDPOINT on IP $ENDPOINT_IP"
pg "Done"
pnn "- Payload service restarted : "
run sshpass -e ssh -q -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" $USER@$ENDPOINT_IP 'sudo systemctl start payload || true'
return_check "Failed to connect to endpoint $ENDPOINT on IP $ENDPOINT_IP"
pg "Done"
else
pl
pg ":: Endpoint ::"
pl
pg "Starting terminal ..."
pl
pg "Run \"exit\" to return."
pl
# Shell
set +e
sshpass -e ssh -t -q -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" $USER@$ENDPOINT_IP 'cd /home/iot/payload; sudo -E bash'
set -e
fi
pl