How to toggle wireless using a script

My laptop comes with the option to toggle wireless with the Fn+F12 key, but unfortunately this key doesn't work. Now inspired by this question, I would like to write a script that can toggle the software state of the wireless, preferably without needing sudo.


Solution 1:

This basic script will do the job:

#!/bin/bash
status=$(nmcli -t -f WIFI nm)
if [ $status = "enabled" ] ; then
    notify-send -i network-wireless-disconnected "Wireless" "Wireless disabled"
    nmcli nm wifi off
else
    notify-send -i network-wireless-none "Wireless" "Wireless enabled"
    nmcli nm wifi on
fi
exit 0