It's my understanding that when you plug in the Ethernet cable that trumps the wifi connection and the wifi connection drops automatically. In similar fashion, disconnecting the Ethernet cable auto connects to your preferred wifi (provided of course that you have the box checked to auto connect to that Wifi network on the general tab of network manager. Tested on a default install of Ubuntu 16.04.3
So if I'm understanding you correctly you just want to automatically start the hotspot service on your laptop when the Ethernet cable is connected and disable the service when the Ethernet cable is disconnected.
This should be fairly simple to do with a bash script and nmcli
'nmcli' is a powerful networknig tool that allows you to bring connections up and down at will and obtain tons of relevant information.
To find out what connections you will be scripting for simply run nmcli -t monitor| grep primary
when connected via wifi and plug in your Ethernet cable. The connections you are interested in bringing up and down with nmcli
will be those surrounded by '
s Example: 'Wired connection 1' the logic is that when 'Connection' is connected bring up the hotspot (you may find it necessary to use the command sleep
or utilize the -w
switch to make nmcli
in your script wait for a specified number of seconds for a command to complete before issuing the next.
Bringing a connection down is as simple as nmcli 'connection name' down
and bringing one up is nmcli 'connection name' up
Note: For full control via the script you may need to actually disable the automatically connect option at least for your wifi connection in Network manager as it will attempt to auto-connect whenever it's available and can interfere with what you are attempting to accomplish. Excerpt from man nmcli
:
There may be multiple connections that apply
to a device, but only one of them can be active on that device at any
given time. The additional connections can be used to allow quick
switching between different networks and configurations.
So once you've determined that your Ethernet is connected you can issue the commands to bring the wifi connection down Example: nmcli connection my-wifi down
and then the hotspot connection up Example: nmcli connection my-hotspot up
For more on how to automatically run scripts based on network connection see this. For more detail about 'nmcli' check the man page. And here's info on how to create a hotspot.
Best Answer
The best answer might be from here:
Go to the command line and type:
Assuming your wifi is called wlan0 (it might be wlan0 or something else) see ifconfig for details.
This command will refresh the list by asking the wifi to rescan. You can only do this as root and if you do it as non-root it will simply print the list of known networks without refreshing.