|
2 | 2 | set -euo pipefail |
3 | 3 |
|
4 | 4 | AP_CONN="{{ ap_conn_name }}" |
5 | | -ETH_IFACE="" |
6 | 5 | DEBUG="${1:-}" |
7 | 6 |
|
8 | | -# nmcli is required |
| 7 | +# Ensure nmcli is available |
9 | 8 | 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 |
12 | 11 | fi |
13 | 12 |
|
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 | +) |
23 | 18 |
|
24 | 19 | 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 |
26 | 22 | fi |
27 | 23 |
|
28 | | -# Returns true if Ethernet is connected/activated |
| 24 | +# Check whether Ethernet is connected |
29 | 25 | 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" |
31 | 30 | } |
32 | 31 |
|
33 | | -# Returns true if AP connection is active |
| 32 | +# Check whether the AP connection is active |
34 | 33 | 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" |
36 | 35 | } |
37 | 36 |
|
38 | | -# Main action |
| 37 | +# Main logic |
39 | 38 | 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 | + |
48 | 47 | 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." |
56 | 51 | 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 |
59 | 58 | fi |
60 | | - fi |
| 59 | + |
61 | 60 | fi |
62 | 61 |
|
63 | 62 | exit 0 |
0 commit comments