Based on Bob Johnson's answer, I would suggest to use nmcli device show [ethernetdevice] | grep IP4.ADDRESS
, it's agnostic to language and prevents to keep WiFi as AP when a cable is connected but is not giving IP address:
#!/bin/bash
AP=0
while :;
do
if nmcli device show eth0 | grep IP4.ADDRESS ; then
if [[ AP -eq 0 ]]; then
nmcli device disconnect wlan0
nmcli connection up 'Shared WiFi connection name'
AP=1
fi
else
if [[ AP -eq 1 ]]; then
nmcli connection down 'Shared WiFi connection name'
nmcli device connect wlan0
AP=0
fi
fi
sleep 5
done
Where:
eth0
is the ethernet device identified by ifname, eth0 in most cases, (not mine) you can get the correct name for your device by typing nmcli device
in a terminal.
wlan0
is the WiFi device identified by ifname.
Shared WiFi connection name
is the name of the CONNECTION to share WiFi as AP (not always equivalent to SSID, it depends on how it is configured).
Add the script to autostart apps in your shell. It's working like a charm for me.