Skip to content

Instantly share code, notes, and snippets.

@bulletmark
Last active August 29, 2025 22:02
Show Gist options
  • Select an option

  • Save bulletmark/8e051a0a9ffdce689d86988c528e7764 to your computer and use it in GitHub Desktop.

Select an option

Save bulletmark/8e051a0a9ffdce689d86988c528e7764 to your computer and use it in GitHub Desktop.

Revisions

  1. bulletmark revised this gist Aug 29, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@
    # (i.e. `sudo chmod 755 /etc/NetworkManager/dispatcher.d/99-wifi`). No other
    # configuration is required. Get the latest version from
    # https://gist.github.com/bulletmark/8e051a0a9ffdce689d86988c528e7764
    # Python 3.7 or later is required.
    # Author: Mark Blakeney, Jun 2020.
    from __future__ import annotations
    import subprocess
    @@ -14,8 +15,7 @@ import sys
    def run(cmd: str) -> list[str]:
    'Run given cmd'
    try:
    res = subprocess.run(cmd.split(), stdout=subprocess.PIPE,
    universal_newlines=True)
    res = subprocess.run(cmd.split(), stdout=subprocess.PIPE, text=True)
    except Exception as e:
    sys.exit(str(e))

  2. bulletmark revised this gist Mar 1, 2025. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,10 @@
    #!/usr/bin/env python3
    # If you are using Linux NetworkManager then this program toggles your
    # wifi radio on whenever all your wired connections are not connected,
    # or turns the wifi radio off when any wired connection is connected.
    # Simply copy this to /etc/NetworkManager/dispatcher.d/99-wifi and
    # ensure it is executable
    # (i.e. `sudo chmod 755 /etc/NetworkManager/dispatcher.d/99-wifi`).
    # No other configuration is required. Get the latest version from
    # If you are using Linux NetworkManager then this dispatcher script toggles
    # your wifi radio ON whenever all your wired connections are not connected, or
    # turns the wifi radio OFF when any wired connection is connected. Simply copy
    # this to /etc/NetworkManager/dispatcher.d/99-wifi and ensure it is executable
    # (i.e. `sudo chmod 755 /etc/NetworkManager/dispatcher.d/99-wifi`). No other
    # configuration is required. Get the latest version from
    # https://gist.github.com/bulletmark/8e051a0a9ffdce689d86988c528e7764
    # Author: Mark Blakeney, Jun 2020.
    from __future__ import annotations
  3. bulletmark revised this gist Jun 11, 2024. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,11 @@
    #!/usr/bin/env python3
    # If you are using Linux NetworkManager then this program toggles your
    # wifi connection off->ON whenever your wired connection goes ON->off,
    # and vice-versa. Simply copy this to
    # /etc/NetworkManager/dispatcher.d/99-wifi and ensure it is executable.
    # wifi radio on whenever all your wired connections are not connected,
    # or turns the wifi radio off when any wired connection is connected.
    # Simply copy this to /etc/NetworkManager/dispatcher.d/99-wifi and
    # ensure it is executable
    # (i.e. `sudo chmod 755 /etc/NetworkManager/dispatcher.d/99-wifi`).
    # No configuration is required. Get the latest version from
    # No other configuration is required. Get the latest version from
    # https://gist.github.com/bulletmark/8e051a0a9ffdce689d86988c528e7764
    # Author: Mark Blakeney, Jun 2020.
    from __future__ import annotations
  4. bulletmark revised this gist Jun 11, 2024. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,11 @@
    # No configuration is required. Get the latest version from
    # https://gist.github.com/bulletmark/8e051a0a9ffdce689d86988c528e7764
    # Author: Mark Blakeney, Jun 2020.
    from __future__ import annotations
    import subprocess
    import sys

    def run(cmd: str):
    def run(cmd: str) -> list[str]:
    'Run given cmd'
    try:
    res = subprocess.run(cmd.split(), stdout=subprocess.PIPE,
  5. bulletmark revised this gist Jun 11, 2024. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,10 @@
    # No configuration is required. Get the latest version from
    # https://gist.github.com/bulletmark/8e051a0a9ffdce689d86988c528e7764
    # Author: Mark Blakeney, Jun 2020.
    from __future__ import annotations
    import subprocess
    import sys

    def run(cmd: str) -> list[str]:
    def run(cmd: str):
    'Run given cmd'
    try:
    res = subprocess.run(cmd.split(), stdout=subprocess.PIPE,
  6. bulletmark revised this gist Jun 11, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    # wifi connection off->ON whenever your wired connection goes ON->off,
    # and vice-versa. Simply copy this to
    # /etc/NetworkManager/dispatcher.d/99-wifi and ensure it is executable.
    # (i.e. `sudo chmod 755 /etc/NetworkManager/dispatcher.d/99-wifi`).
    # No configuration is required. Get the latest version from
    # https://gist.github.com/bulletmark/8e051a0a9ffdce689d86988c528e7764
    # Author: Mark Blakeney, Jun 2020.
  7. bulletmark revised this gist Jan 2, 2024. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -21,8 +21,7 @@ def run(cmd: str) -> list[str]:
    if res.returncode != 0:
    sys.exit(res.returncode)

    out = res.stdout and res.stdout.strip()
    return out.splitlines() if out else []
    return res.stdout.strip().splitlines()

    # Firstly, ensure airplane mode is off because GNOME has an unfortunate
    # habit of automatically turning it on.
  8. bulletmark revised this gist Jan 2, 2024. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,32 @@
    #!/usr/bin/python3
    #!/usr/bin/env python3
    # If you are using Linux NetworkManager then this program toggles your
    # wifi connection off->ON whenever your wired connection goes ON->off,
    # and vice-versa. Simply copy this to
    # /etc/NetworkManager/dispatcher.d/99-wifi and ensure it is executable.
    # No configuration is required. Get the latest version from
    # https://gist.github.com/bulletmark/8e051a0a9ffdce689d86988c528e7764
    # Author: Mark Blakeney, Jun 2020.
    import sys
    from __future__ import annotations
    import subprocess
    import sys

    def run(cmd):
    def run(cmd: str) -> list[str]:
    'Run given cmd'
    try:
    res = subprocess.run(cmd.split(), stdout=subprocess.PIPE,
    universal_newlines=True)
    universal_newlines=True)
    except Exception as e:
    sys.exit(str(e))

    if res.returncode != 0:
    sys.exit(res.returncode)

    return res.stdout
    out = res.stdout and res.stdout.strip()
    return out.splitlines() if out else []

    # Firstly, ensure airplane mode is off because GNOME has an unfortunate
    # habit of automatically turning it on.
    for line in run('rfkill -n').splitlines():
    # habit of automatically turning it on.
    for line in run('rfkill -n'):
    devid, devtype, pdev, sstate, hstate = line.split()
    if devtype == 'wlan' and sstate == 'blocked':
    run(f'rfkill unblock {devid}')
    @@ -33,7 +35,7 @@ eth_up = False
    wifi_up = None

    # Look at current state
    for line in run('nmcli -t -c no device').splitlines():
    for line in run('nmcli -t -c no device'):
    dev, devtype, state, *junk = line.split(':')
    if devtype == 'ethernet':
    if state == 'connected':
  9. bulletmark revised this gist Oct 2, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,8 @@ def run(cmd):

    return res.stdout

    # Firstly, ensure airplane mode is off
    # Firstly, ensure airplane mode is off because GNOME has an unfortunate
    # habit of automatically turning it on.
    for line in run('rfkill -n').splitlines():
    devid, devtype, pdev, sstate, hstate = line.split()
    if devtype == 'wlan' and sstate == 'blocked':
  10. bulletmark revised this gist Sep 18, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ def run(cmd):

    return res.stdout

    # Ensure Airplane mode is off
    # Firstly, ensure airplane mode is off
    for line in run('rfkill -n').splitlines():
    devid, devtype, pdev, sstate, hstate = line.split()
    if devtype == 'wlan' and sstate == 'blocked':
  11. bulletmark revised this gist Sep 18, 2020. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    # No configuration is required. Get the latest version from
    # https://gist.github.com/bulletmark/8e051a0a9ffdce689d86988c528e7764
    # Author: Mark Blakeney, Jun 2020.
    import sys
    import sys
    import subprocess

    def run(cmd):
    @@ -22,9 +22,16 @@ def run(cmd):

    return res.stdout

    # Ensure Airplane mode is off
    for line in run('rfkill -n').splitlines():
    devid, devtype, pdev, sstate, hstate = line.split()
    if devtype == 'wlan' and sstate == 'blocked':
    run(f'rfkill unblock {devid}')

    eth_up = False
    wifi_up = None

    # Look at current state
    for line in run('nmcli -t -c no device').splitlines():
    dev, devtype, state, *junk = line.split(':')
    if devtype == 'ethernet':
  12. bulletmark revised this gist Sep 4, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    #!/usr/bin/python3
    # If you are using Linux NetworkManager then this program toggles your
    # wifi connection off/ON whenever your wired connection goes ON/off.
    # Simply copy this to /etc/NetworkManager/dispatcher.d/99-wifi and
    # ensure it is executable. No configuration is required. Get the latest
    # version from
    # wifi connection off->ON whenever your wired connection goes ON->off,
    # and vice-versa. Simply copy this to
    # /etc/NetworkManager/dispatcher.d/99-wifi and ensure it is executable.
    # No configuration is required. Get the latest version from
    # https://gist.github.com/bulletmark/8e051a0a9ffdce689d86988c528e7764
    # Author: Mark Blakeney, Jun 2020.
    import sys
  13. bulletmark revised this gist Jul 30, 2020. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,9 @@
    # If you are using Linux NetworkManager then this program toggles your
    # wifi connection off/ON whenever your wired connection goes ON/off.
    # Simply copy this to /etc/NetworkManager/dispatcher.d/99-wifi and
    # ensure it is executable. No configuration is required.
    # ensure it is executable. No configuration is required. Get the latest
    # version from
    # https://gist.github.com/bulletmark/8e051a0a9ffdce689d86988c528e7764
    # Author: Mark Blakeney, Jun 2020.
    import sys
    import subprocess
  14. bulletmark created this gist Jul 7, 2020.
    44 changes: 44 additions & 0 deletions wifi-switcher
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    #!/usr/bin/python3
    # If you are using Linux NetworkManager then this program toggles your
    # wifi connection off/ON whenever your wired connection goes ON/off.
    # Simply copy this to /etc/NetworkManager/dispatcher.d/99-wifi and
    # ensure it is executable. No configuration is required.
    # Author: Mark Blakeney, Jun 2020.
    import sys
    import subprocess

    def run(cmd):
    'Run given cmd'
    try:
    res = subprocess.run(cmd.split(), stdout=subprocess.PIPE,
    universal_newlines=True)
    except Exception as e:
    sys.exit(str(e))

    if res.returncode != 0:
    sys.exit(res.returncode)

    return res.stdout

    eth_up = False
    wifi_up = None

    for line in run('nmcli -t -c no device').splitlines():
    dev, devtype, state, *junk = line.split(':')
    if devtype == 'ethernet':
    if state == 'connected':
    eth_up = True
    elif devtype == 'wifi':
    if state == 'connected':
    wifi_up = True
    elif wifi_up is None:
    wifi_up = False

    # Set wifi off/on if ethernet device on/off.
    # Note we only switch wifi off/on if wifi device exists.
    if eth_up:
    if wifi_up:
    run('nmcli radio wifi off')
    else:
    if wifi_up is False:
    run('nmcli radio wifi on')