-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcp_hook_async.sh
executable file
·165 lines (117 loc) · 2.83 KB
/
dhcp_hook_async.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
#!/bin/bash
set -euo pipefail
IP=$2
MAC=$3
faketty () {
script -qefc "$(printf "%q " "$@")" /dev/null
}
E() {
echo $(tput bold)\# $MAC $IP $@$(tput sgr0)
}
ssh_host() {
PASS="$1"
USER="$2"
shift 2
PRECMD=()
if [ -v FAKETTY ]; then
PRECMD+=(faketty)
fi
PRECMD+=(sshpass -p "$PASS")
"${PRECMD[@]}" ssh -o "ControlPath=none" -o "ControlMaster=no" -o "UserKnownHostsFile=/dev/null" -o StrictHostKeyChecking=no "$USER@$IP" "$@"
}
ssh_openwrt() {
ex=255
count=0
if [ $ex -eq 255 ]; then
set +e
ssh_host "" root "$@" ; ex=$?
set -e
count=$(( $count + 1 ))
if [ $count -gt 5 ]; then
return $ex
fi
fi
return $ex
}
ssh_host_openwrt() {
ssh_openwrt "$@" &>/dev/null
}
ssh_host_openwrt_out() {
ssh_openwrt "$@" 2> >(grep -v "Warning: Permanently added" >&2)
}
post() {
cat "post/$1.sh" | ssh_host_openwrt_out "sh -"
}
scp_file() {
sshpass -p "$1" scp -o "ControlPath=none" -o "ControlMaster=no" -o "UserKnownHostsFile=/dev/null" -o StrictHostKeyChecking=no $3 "$4" "$2"@$IP:/"$5" &>/dev/null
if [ $? -ne 0 ]; then
E "failed to upload file" $3
exit 1
fi
}
scp_file_openwrt() {
scp_file "" root "-O" "$1" "$2"
}
E AP connected
timeout 40 /bin/sh -c -- "while ! timeout 0.5 ping -c 1 -n $IP &>/dev/null; do :; done"
if [ $? -ne 0 ]; then
E "timeout, aborting"
exit 1
fi
sleep 1
if cat /dev/null | ssh_host_openwrt; then
TYPEACTION=($(post get-type))
TYPE="${TYPEACTION[0]}"
ACTION="${TYPEACTION[1]}"
case "$ACTION" in
flash)
E "$TYPE" AP: preparing gluon flash
BOARD_NAME=$(ssh_host_openwrt_out cat /tmp/sysinfo/board_name)
E GLUON: resolving image for "$BOARD_NAME" channel "$CHANNEL"
URL=$(bash try_board_to_image.sh "$BOARD_NAME")
if [ -z "$URL" ]; then
E GLUON: failed to resolve
exit 2
fi
E GLUON: fetching image "$URL"
BASE=$(basename "$URL")
pushd "$(mktemp -d)" >/dev/null
wget -q "$URL" "-O$BASE"
E "$TYPE" AP: scp image "$BASE"
scp_file_openwrt "$BASE" "/tmp/$BASE"
E "$TYPE" AP: wait
popd >/dev/null
set +e
post wait
set -e
E "$TYPE" AP: executing sysupgrade
echo "sysupgrade -n -v /tmp/$BASE" | ssh_host_openwrt_out -t sh -l - || true
E "$TYPE" AP: rebooting into config mode
exit 0
;;
setup)
E "$TYPE" AP wait
set +e
post wait
set -e
E "$TYPE" AP "Configure gluon"
if [ -e post/custom.sh ]; then
E "$TYPE" AP "Using custom script"
post custom
else
post set-name
fi
if ! [ -v GLUON_NO_SKIP ]; then
post configure-gluon-skip
fi
(post blink-and-die &)
sleep 5s
E "$TYPE" AP DONE "Configured $MAC"
exit 0
;;
?)
E MAIN "General failure."
exit 2
;;
esac
fi