Skip to content

Instantly share code, notes, and snippets.

@xshyamx
Created December 8, 2015 06:55
Show Gist options
  • Save xshyamx/7c22ad4d2bcebda5cf26 to your computer and use it in GitHub Desktop.
Save xshyamx/7c22ad4d2bcebda5cf26 to your computer and use it in GitHub Desktop.
Toggle WiFi on and off from the command line
# simple commands to (dis)connect from wifi network
# docker-machine and docker are unable to handle the changing ip addresses :(
# but once, disconnect and reconnect everything is happy in paradise :)
wifi_off() {
netsh wlan show interfaces > /tmp/wifi_status
status=$(awk -F: '/State/ {gsub(/^ *| *$/, "", $2); print$2; }' /tmp/wifi_status)
name=$(awk -F: '/Name/ { gsub(/^ *| *$/, "", $2); print$2; }' /tmp/wifi_status)
if [ "$status" = "connected" ]; then
netsh wlan disconnect interface="$name"
fi
}
wifi_on() {
netsh wlan show interfaces > /tmp/wifi_status
status=$(awk -F: '/State/ {gsub(/^ *| *$/, "", $2); print$2; }' /tmp/wifi_status)
name=$(awk -F: '/Name/ { gsub(/^ *| *$/, "", $2); print$2; }' /tmp/wifi_status)
if [ "$status" = "disconnected" ]; then
netsh wlan show networks > /tmp/wifi_nets
grep -E 'one|two|three' /tmp/wifi_nets > /tmp/known_hotspots
if [ $? -eq 0 ]; then
ssid=$(sed 's/^[^:]*: //' /tmp/known_hotspots)
netsh wlan connect Name="$ssid" SSID="$ssid" interface="$name"
else
echo 'No known hotspots to connect'
fi
fi
}
wifi() {
case $1 in
on) wifi_on
;;
off) wifi_off
;;
*) echo 'Usage: wifi [on|off]'
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment