Skip to content

Instantly share code, notes, and snippets.

@rjcorwin
Last active April 26, 2022 23:07
Show Gist options
  • Save rjcorwin/4999792f4a7aedd532b2 to your computer and use it in GitHub Desktop.
Save rjcorwin/4999792f4a7aedd532b2 to your computer and use it in GitHub Desktop.
A simple `wifi` command for Debian that will connect you to a WPA2 WiFi network
#!/bin/sh
## A simple `wifi` command for Debian that will connect you to a WPA2 WiFi network
## usage:
## sudo ./wpa2-wifi-connect.sh <ssid> <pass>
ifdown wlan0
# build the interfaces file that will point to the file that holds our configuration
rm /etc/network/interfaces
touch /etc/network/interfaces
echo 'auto lo' >> /etc/network/interfaces
echo 'iface lo inet loopback' >> /etc/network/interfaces
echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
echo 'allow-hotplug wlan0' >> /etc/network/interfaces
echo 'iface wlan0 inet manual' >> /etc/network/interfaces
echo 'wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf' >> /etc/network/interfaces
echo 'iface default inet dhcp' >> /etc/network/interfaces
# build the supplicant file that holds our configuration
rm /etc/wpa_supplicant/wpa_supplicant.conf
touch /etc/wpa_supplicant/wpa_supplicant.conf
echo 'ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev' >> /etc/wpa_supplicant/wpa_supplicant.conf
echo 'update_config=1' >> /etc/wpa_supplicant/wpa_supplicant.conf
wpa_passphrase $1 $2 >> /etc/wpa_supplicant/wpa_supplicant.conf
ifup wlan0
@gustavostuff
Copy link

gustavostuff commented Apr 13, 2021

If I'm already connected to a wifi network, the script seems to fail, is there a way to force a reconnect and avoid errors?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment