-
Notifications
You must be signed in to change notification settings - Fork 25
/
install_wpaconf.bash
executable file
·166 lines (152 loc) · 4.32 KB
/
install_wpaconf.bash
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
#!/bin/bash
# copy the wpa_text file to the lanforge and assign it to a station or stations
# set -veu
if [[ -z "$1" ]]; then
echo "Usage: $0 <gui_ip> <resource_ip> <wpa_txt_filename> <list of station short eids>"
exit 1
fi
q="'"
Q='"'
if ! which jq; then
echo "Utility jq not found. Script will not operate, bye."
exit 1
fi
CONF_FILE="UNSET"
GUI="UNSET"
HOSTIP="UNSET"
PORTS_JSON="/tmp/ports.json"
POST_DATA="/tmp/post_data"
STA_EIDS=()
URL_WIFI_CUSTOM="cli-json/set_wifi_custom"
URL_ADD_STATION="cli-json/add_sta"
while getopts "f:g:r:s:" OPTION; do
case "$OPTION" in
f) CONF_FILE="$OPTARG";;
g) GUI="$OPTARG";;
r) HOSTIP="$OPTARG";;
s) STA_EIDS+=("$OPTARG");;
*) echo "Unknown option [$OPTION]"
esac
done
errors=0
#if [[ $HOSTIP = UNSET ]]; then
# errors=$(( errors+1 ))
#fi
if [[ $CONF_FILE = UNSET ]]; then
errors=$(( errors+1 ))
fi
if [[ $CONF_FILE = UNSET ]]; then
errors=$(( errors+1 ))
fi
if (( errors > 0 )); then
echo "Usage: $0 -g <gui_ip> -r <resource_ip> -f <wpa_txt_filename> -s <list of station short eids>"
echo "Example:"
echo " $0 -g localhost -r 192.168.1.101 -f wifi.txt -s 1.1.wlan0 -s 1.wlan1 -s 1.sta0000"
echo " Please do not list radios from multiple chassis, that will not work."
exit 1
fi
if (( "${#STA_EIDS[@]}" < 1 )); then
echo "no stations provided"
exit 1
fi
function post() {
if [[ -z "${1:-}" ]]; then
echo "Post wants URL-path and DATA string. Example: "
echo " post ${Q}json-cli/add_sta$Q ${Q}{'shelf':1,'resource':1,'radio':'wiphy0'}$Q "
exit 1
fi
local url="$1"; shift
if [[ -z "${1:-}" ]]; then
echo "Post: no data"
exit 1
fi
local post_data="$1"
rm -f /tmp/post_response
curl -sq -H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-X POST -d "$post_data" \
-o /tmp/post_response \
"http://$GUI:8080/$url"
local retv=$?
echo -n " "
jq '.LAST|{command,response}' /tmp/post_response | tr -d "\n"
echo "."
return $retv
}
if [[ ! -z "$HOSTIP" ]] && [[ $HOSTIP != UNSET ]]; then
echo "Copying [$CONF_FILE] to [$HOSTIP]:"
scp "$CONF_FILE" "lanforge@$HOSTIP:/home/lanforge/"
if (( $? > 0 )); then
echo "SCP failed, will not post config changes"
exit 1
fi
fi
rm -f $PORTS_JSON
# collect list of ports for this resource
for short_eid in "${STA_EIDS[@]}"; do
resource="${short_eid%.*}"
port="${short_eid#*.}"
eid="1.$resource.$port"
#echo "resource[$resource] port[$port]"
echo "Clearing wifi_custom for $resource.$port"
JSON_CLEAN="{'shelf':1,'resource':$resource,'port':$port,'type':'NA','text':'[BLANK]'}"
post "$URL_WIFI_CUSTOM" "$JSON_CLEAN"
if (( $? > 0 )); then
echo "command failed, stopping"
exit 1
fi
if [[ ! -f $PORTS_JSON ]]; then
curl -sq -o $PORTS_JSON -s -H 'Accept: application/json'\
"http://$GUI:8080/port/1/$resource/list?fields=port,parent+dev"
if [[ ! -f $PORTS_JSON ]] || [[ ! -s $PORTS_JSON ]]; then
echo "Problem saving port list query, cannot check for radios"
exit 1
fi
echo -n " port list: "
jq ".interfaces[] | .[$Q$eid$Q] | select(. != null)" "$PORTS_JSON"
echo ""
fi
query=".interfaces[][$Q$eid$Q] | select(. != null).${Q}parent dev$Q"
radio=$(jq "$query" $PORTS_JSON)
if (( $? > 0 )); then
echo "Unable to find radio record for port[$port]"
continue
fi
echo "Setting $resource.$port radio[$radio] wpa_conf..."
cat > $POST_DATA << __EOF__
{
"shelf":1,
"resource":$resource,
"sta_name":$port,
"radio":$radio,
"flags": 0,
"wpa_cfg_file":"DEFAULT",
"mac":"NA",
"mode":"0",
"rate":"OS Default",
"flags_mask":"32"
}
__EOF__
post $URL_ADD_STATION "@$POST_DATA"
cat > $POST_DATA << __EOF__
{
"shelf":1,
"resource":$resource,
"sta_name":$port,
"radio":$radio,
"flags": 32,
"wpa_cfg_file":"$CONF_FILE",
"mac":"NA",
"mode":"0",
"rate":"OS Default",
"flags_mask":"32"
}
__EOF__
post $URL_ADD_STATION "@$POST_DATA"
post "cli-json/nc_show_ports" "{'shelf':1, 'resource':$resource, 'port':$port}"
if (( $? > 0 )); then
echo "command failed, stopping"
exit 1
fi
done
#