Repository navigation
Expand file tree
/
Copy pathrce.sh
More file actions
391 lines (318 loc) · 9.59 KB
/
Copy pathrce.sh
File metadata and controls
391 lines (318 loc) · 9.59 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/bin/bash
#
# UART Remote Command Execution
# Execute commands on remote device via serial port
#
set -euo pipefail
# Configuration
UART_PORT="${UART_PORT:-/dev/ttyUSB0}"
BAUD_UART="${BAUD_UART:-115200}"
TIMEOUT="${TIMEOUT:-10}"
LOG_FILE="${LOG_FILE:-/var/log/uart_command.log}"
VERBOSE="${VERBOSE:-0}"
INTERACTIVE="${INTERACTIVE:-0}"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
# Logging
log() {
local level="$1"
shift
local msg="$*"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
case "$level" in
ERROR) echo -e "${RED}[ERROR]${NC} $msg" >&2 ;;
WARN) echo -e "${YELLOW}[WARN]${NC} $msg" ;;
INFO) echo -e "${GREEN}[INFO]${NC} $msg" ;;
DEBUG) [ "$VERBOSE" -eq 1 ] && echo -e "${BLUE}[DEBUG]${NC} $msg" ;;
esac
if [ -w "$(dirname "$LOG_FILE")" ] 2>/dev/null; then
echo "[$timestamp] [$level] $msg" >> "$LOG_FILE"
fi
}
# Initialize UART
init_uart() {
log INFO "Initializing UART on $UART_PORT at $BAUD_UART baud"
if [ ! -e "$UART_PORT" ]; then
log ERROR "Port $UART_PORT not found"
return 1
fi
if [ ! -r "$UART_PORT" ] || [ ! -w "$UART_PORT" ]; then
log ERROR "Cannot access $UART_PORT (permission denied)"
return 1
fi
# Kill processes using the port
if fuser "$UART_PORT" 2>/dev/null; then
local pids=$(fuser "$UART_PORT" 2>/dev/null)
log WARN "Killing processes using port: $pids"
kill -9 $pids 2>/dev/null || true
sleep 1
fi
# Configure port
stty -F "$UART_PORT" \
$BAUD_UART \
raw \
-echo \
-echoe \
-echok \
cs8 \
-parenb \
-cstopb 2>/dev/null
log INFO "UART initialized successfully"
return 0
}
# Execute single command
execute_command() {
local cmd="$1"
local wait_response="${2:-1}"
log INFO "Executing: $cmd"
# Send command
echo -e "${cmd}\r" > "$UART_PORT"
if [ "$wait_response" -eq 0 ]; then
log DEBUG "Not waiting for response"
return 0
fi
# Read response with timeout
log DEBUG "Waiting for response (timeout: ${TIMEOUT}s)..."
local response=""
local start_time=$(date +%s)
local end_time=$((start_time + TIMEOUT))
while [ $(date +%s) -lt $end_time ]; do
if read -t 1 line < "$UART_PORT" 2>/dev/null; then
response+="$line"$'\n'
echo -e "${CYAN}${line}${NC}"
fi
done
if [ -z "$response" ]; then
log WARN "No response received"
return 1
fi
log DEBUG "Response received (${#response} bytes)"
return 0
}
# Execute script file
execute_script() {
local script_file="$1"
if [ ! -f "$script_file" ]; then
log ERROR "Script file not found: $script_file"
return 1
fi
log INFO "Executing script: $script_file"
local line_num=0
while IFS= read -r line; do
((line_num++))
# Skip empty lines and comments
[[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
log INFO "[$line_num] $line"
# Check for special directives
if [[ "$line" =~ ^@sleep[[:space:]]+([0-9]+) ]]; then
local sleep_time="${BASH_REMATCH[1]}"
log DEBUG "Sleeping for ${sleep_time}s"
sleep "$sleep_time"
continue
fi
if [[ "$line" =~ ^@wait[[:space:]]+([0-9]+) ]]; then
local wait_time="${BASH_REMATCH[1]}"
TIMEOUT="$wait_time"
log DEBUG "Set timeout to ${wait_time}s"
continue
fi
# Execute command
if ! execute_command "$line"; then
log WARN "Command failed (line $line_num), continuing..."
fi
sleep 0.5 # Small delay between commands
done < "$script_file"
log INFO "Script execution completed"
return 0
}
# Interactive shell
interactive_shell() {
log INFO "Starting interactive shell (Ctrl+C or 'exit' to quit)"
echo -e "${CYAN}=== Interactive UART Shell ===${NC}"
echo -e "${CYAN}Connected to: $UART_PORT @ $BAUD_UART baud${NC}"
echo -e "${CYAN}Commands are sent directly to device${NC}"
echo -e "${CYAN}Special commands:${NC}"
echo -e "${CYAN} !local <cmd> - Run command locally${NC}"
echo -e "${CYAN} !send <file> - Send file content${NC}"
echo -e "${CYAN} !wait <sec> - Change timeout${NC}"
echo -e "${CYAN}================================${NC}"
echo
# Start background reader
{
while true; do
if read -t 0.1 line < "$UART_PORT" 2>/dev/null; then
echo -e "${GREEN}< ${line}${NC}"
fi
done
} &
local reader_pid=$!
# Cleanup function
cleanup_interactive() {
kill $reader_pid 2>/dev/null || true
log INFO "Interactive shell closed"
}
trap cleanup_interactive EXIT INT TERM
# Interactive loop
while true; do
echo -n -e "${BLUE}> ${NC}"
read -r cmd
# Check for exit
[[ "$cmd" == "exit" || "$cmd" == "quit" ]] && break
# Handle special commands
if [[ "$cmd" =~ ^!local[[:space:]]+(.*) ]]; then
eval "${BASH_REMATCH[1]}"
continue
fi
if [[ "$cmd" =~ ^!send[[:space:]]+(.*) ]]; then
local file="${BASH_REMATCH[1]}"
if [ -f "$file" ]; then
log INFO "Sending file: $file"
cat "$file" > "$UART_PORT"
else
log ERROR "File not found: $file"
fi
continue
fi
if [[ "$cmd" =~ ^!wait[[:space:]]+([0-9]+) ]]; then
TIMEOUT="${BASH_REMATCH[1]}"
log INFO "Timeout set to ${TIMEOUT}s"
continue
fi
# Skip empty commands
[[ -z "$cmd" ]] && continue
# Send command
echo -e "${cmd}\r" > "$UART_PORT"
done
cleanup_interactive
}
# Monitor mode - just display output
monitor_mode() {
log INFO "Starting monitor mode (Ctrl+C to exit)"
echo -e "${CYAN}=== UART Monitor ===${NC}"
echo -e "${CYAN}Port: $UART_PORT @ $BAUD_UART baud${NC}"
echo -e "${CYAN}===================${NC}"
echo
while true; do
if read -t 0.1 line < "$UART_PORT" 2>/dev/null; then
echo -e "${GREEN}${line}${NC}"
fi
done
}
# Display usage
usage() {
cat << EOF
Usage: $0 [OPTIONS] COMMAND [ARGS]
UART Remote Command Execution
Commands:
exec "COMMAND" Execute single command
script FILE Execute commands from script file
shell Interactive shell mode
monitor Monitor output (no input)
Options:
-p PORT Serial port (default: $UART_PORT)
-b BAUD Baud rate (default: $BAUD_UART)
-t TIMEOUT Response timeout in seconds (default: $TIMEOUT)
-n No-wait mode (don't wait for response)
-i Interactive mode
-v Verbose output
-h Show this help
Environment Variables:
UART_PORT Override default port
BAUD_UART Override default baud rate
TIMEOUT Override default timeout
Examples:
# Execute single command
$0 exec "ls -la"
# Execute with no wait
$0 -n exec "reboot"
# Execute script
$0 script commands.txt
# Interactive shell
$0 shell
$0 -i
# Monitor output
$0 monitor
Script File Format:
# Comments start with #
ls -la
@sleep 2 # Sleep for 2 seconds
@wait 5 # Set timeout to 5 seconds
cat /proc/cpuinfo
Interactive Commands:
!local <cmd> Run command on local machine
!send <file> Send file content to device
!wait <sec> Change response timeout
exit Exit interactive mode
EOF
exit 0
}
# Parse arguments
NO_WAIT=0
parse_args() {
while getopts "p:b:t:nivh" opt; do
case "$opt" in
p) UART_PORT="$OPTARG" ;;
b) BAUD_UART="$OPTARG" ;;
t) TIMEOUT="$OPTARG" ;;
n) NO_WAIT=1 ;;
i) INTERACTIVE=1 ;;
v) VERBOSE=1 ;;
h) usage ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
# Interactive mode shortcut
if [ "$INTERACTIVE" -eq 1 ]; then
COMMAND="shell"
return
fi
if [ $# -lt 1 ]; then
log ERROR "Missing command"
usage
fi
COMMAND="$1"
shift
ARGS=("$@")
}
# Main
main() {
parse_args "$@"
log INFO "=== UART Remote Command Started ==="
log INFO "Port: $UART_PORT | Baud: $BAUD_UART"
init_uart || exit 1
case "$COMMAND" in
exec|execute)
if [ ${#ARGS[@]} -eq 0 ]; then
log ERROR "No command specified"
exit 1
fi
execute_command "${ARGS[*]}" $((1 - NO_WAIT)) || exit 1
;;
script)
if [ ${#ARGS[@]} -eq 0 ]; then
log ERROR "No script file specified"
exit 1
fi
execute_script "${ARGS[0]}" || exit 1
;;
shell|interactive)
interactive_shell
;;
monitor)
monitor_mode
;;
*)
log ERROR "Unknown command: $COMMAND"
usage
;;
esac
log INFO "=== Command execution completed ==="
exit 0
}
main "$@"