Wireless can be enabled or disabled using rfkill tool.
Here is solution based on it, that will allow to save state and restore it at system startup.
Step 00: creation of file to store wifi state
cd /usr/local/etc
sudo touch .wifistate
sudo chmod 666 .wifistate
Step 01: script
#!/bin/bash
IFACE="wlan1"
STATE_FILE="/usr/local/etc/.wifistate"
STATE="$(iwconfig $IFACE | grep Tx | cut -d '=' -f2 | grep off)"
if [ "$STATE" ]
then
rfkill unblock wifi &&
echo 1 > "$STATE_FILE" &&
echo "Wireless enabled"
else
rfkill block wifi &&
echo 0 > "$STATE_FILE" &&
echo "Wireless disabled"
fi
exit 0;
Step 10: making script executable
chmod +x <script name>
Step 11: modifying Ubuntu startup script
open /etc/rc.local
in any text editor (must be edited as root) and add following code
before exit 0;
line:
FILE="/usr/local/etc/.wifistate"
if [ -r "$FILE" ]
then
if [ $(cat $FILE) -eq 0 ]
then
rfkill block wifi
fi
else
rfkill block wifi
fi
Done, now script from step 01 may be linked to keyboard event. After first use it will write 0 or 1 in .wifistate file, and on system startup rc.local script will take attempt to read this value and, if it is 0, disable wifi.
If .wifistate file does not exist, by default wifi will be disabled at startup.