Skip to content

Commit 8cace8f

Browse files
committed
Fix script
1 parent 3a836ff commit 8cace8f

1 file changed

Lines changed: 36 additions & 37 deletions

File tree

roles/ap/templates/CheckAP.sh.j2

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,61 @@
22
set -euo pipefail
33

44
AP_CONN="{{ ap_conn_name }}"
5-
ETH_IFACE=""
65
DEBUG="${1:-}"
76

8-
# nmcli is required
7+
# Ensure nmcli is available
98
if ! command -v nmcli >/dev/null 2>&1; then
10-
echo "CheckAP: nmcli not found." >&2
11-
exit 2
9+
echo "CheckAP: nmcli not found." >&2
10+
exit 2
1211
fi
1312

14-
# Detect managed Ethernet interface
15-
ETH_IFACE=$(nmcli -t -f DEVICE,TYPE,STATE device status \
16-
| awk -F: '$2=="ethernet" && $3!="unavailable" {print $1; exit}')
17-
18-
if [ -z "$ETH_IFACE" ]; then
19-
# Do not consider as fatal error: log and exit with 0
20-
echo "CheckAP: No managed Ethernet interface found; nothing to do." >&2
21-
exit 0
22-
fi
13+
# Detect the first Ethernet interface managed by NetworkManager
14+
ETH_IFACE=$(
15+
nmcli -t -f DEVICE,TYPE device status \
16+
| awk -F: '$2=="ethernet" {print $1; exit}'
17+
)
2318

2419
if [ "$DEBUG" = "debug" ]; then
25-
echo "DEBUG: ETH_IFACE detected as '$ETH_IFACE'" >&2
20+
echo "DEBUG: Ethernet interface detected: '${ETH_IFACE:-none}'"
21+
nmcli -t -f DEVICE,TYPE,STATE device status
2622
fi
2723

28-
# Returns true if Ethernet is connected/activated
24+
# Check whether Ethernet is connected
2925
eth_connected() {
30-
nmcli -t -f DEVICE,STATE device status | grep "^${ETH_IFACE}:connected" >/dev/null 2>&1
26+
[ -n "${ETH_IFACE:-}" ] || return 1
27+
28+
nmcli -t -f DEVICE,STATE device status \
29+
| grep -q "^${ETH_IFACE}:connected"
3130
}
3231

33-
# Returns true if AP connection is active
32+
# Check whether the AP connection is active
3433
ap_active() {
35-
nmcli -t -f NAME connection show --active | grep -Fxq "$AP_CONN"
34+
nmcli -t -f NAME connection show --active | grep -Fxq "$AP_CONN"
3635
}
3736

38-
# Main action
37+
# Main logic
3938
if eth_connected; then
40-
if ap_active; then
41-
# Bring down AP if Ethernet is connected and AP is active
42-
nmcli connection down "$AP_CONN" >/dev/null 2>&1 || true
43-
echo "CheckAP: AP '$AP_CONN' brought down (Ethernet connected)."
44-
else
45-
# Ethernet is connected but AP is already inactive
46-
echo "CheckAP: Ethernet connected — AP '$AP_CONN' already inactive."
47-
fi
39+
40+
if ap_active; then
41+
nmcli connection down "$AP_CONN" >/dev/null 2>&1 || true
42+
echo "CheckAP: AP '$AP_CONN' disabled (Ethernet connected)."
43+
else
44+
echo "CheckAP: Ethernet connected; AP '$AP_CONN' already disabled."
45+
fi
46+
4847
else
49-
if ap_active; then
50-
# Ethernet is disconnected but AP is already active
51-
echo "CheckAP: Ethernet disconnected — AP '$AP_CONN' already active."
52-
else
53-
# Bring up AP if Ethernet is disconnected and AP is inactive
54-
if nmcli connection up "$AP_CONN" >/dev/null 2>&1; then
55-
echo "CheckAP: AP '$AP_CONN' brought up (Ethernet disconnected)."
48+
49+
if ap_active; then
50+
echo "CheckAP: Ethernet unavailable; AP '$AP_CONN' already enabled."
5651
else
57-
echo "CheckAP: Failed to bring up AP '$AP_CONN'." >&2
58-
exit 1
52+
if nmcli connection up "$AP_CONN" >/dev/null 2>&1; then
53+
echo "CheckAP: AP '$AP_CONN' enabled (Ethernet unavailable)."
54+
else
55+
echo "CheckAP: Failed to enable AP '$AP_CONN'." >&2
56+
exit 1
57+
fi
5958
fi
60-
fi
59+
6160
fi
6261

6362
exit 0

0 commit comments

Comments
 (0)