1

My router is quite a distance from my work space, but I have an ethernet cable running that distance. When I plug it into my laptop, is there a way to automatically turn on the laptop's hotspot so that my phone can connect to a stronger WiFi signal?

Also, it would need to turn off the hotspot when the ethernet connection is removed.

  • https://askubuntu.com/questions/928594/restart-wi-fi-hotspot-automatically-on-ubuntu-17-04 –  Nov 10 '18 at 19:17
  • I want it to start when ethernet is in, and stop when ethernet is out. – Costa Michailidis Nov 10 '18 at 19:20
  • If there's no internet connection - Ethernet unplugged - why does it matter? And - I'm not sure - it ptobably stops if the Ethernet cable is unplugged anyway. –  Nov 10 '18 at 19:22
  • If I remove the ethernet from the laptop, and the hotspot is on, then there's not network connection. – Costa Michailidis Nov 10 '18 at 19:24
  • Yes, of course, That's what I said. In that situation why do you need to stop the hotspot? Just so the phone searches for another WiFi? It's much faster doing it manually in the phone. –  Nov 10 '18 at 19:28
  • No, I need to stop the hotspot so that the laptop can have a connection. Also, If I disconnect the ethernet, and the phone remains connected to the laptop, it won't have a connection anyway. – Costa Michailidis Nov 10 '18 at 21:02

2 Answers2

2

Use nmcli d|grep -E "^eth0"|grep connected to judge if cable is connected.

Write a script like

AP=0
while :; do
    if nmcli d|grep -E "^eth0"|grep connected ; then
        if [[ AP -eq 0 ]]; then
            # bring hotspot up
            AP=1
        fi
    else
        if [[ AP -eq 1 ]]; then
            # turn hotspot off
            AP=0
        fi
    fi
    sleep 2
done
Bob Johnson
  • 161
  • 4
  • Hmm... It's the commented section that would be useful. I don't know how to turn the hotspot on from bash. – Costa Michailidis Nov 12 '18 at 23:53
  • It depends on how you turn on your hotspot. You didn't give that info – Bob Johnson Nov 13 '18 at 02:47
  • There's an alternate approach that would be great, which is just getting a keyboard shortcut up, but I can't get that to work. Seems to be something wrong with keyboard shortcuts in general on my system: https://askubuntu.com/questions/1092387/can-i-create-a-keyboard-shortcut-to-kicking-on-my-wifi-hotspot – Costa Michailidis Nov 13 '18 at 03:44
  • I would suggest to use nmcli device show [ethernetdevice] | grep IP4.ADDRESS, it's is agnostic to language and prevents to keep WiFi as AP when a cable is connected but is not giving IP address. – leoperbo Jun 07 '19 at 01:34
1

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.

leoperbo
  • 753