Connect to Wifi via Bash Script in Debian

This command seems to be kinda outdated because it only supports wep:

iwconfig wlan0 essid WIFI_NETWORK key s:WIFI_PASSWORD 
dhclient wlan0

I found an alternative way that supports wpa2, but it involved changing a .conf file.
I want really to connect to wifi networks just by running the script. :(

1

1 Answer

I built a quick shell script that does the necessary configuration for connecting to a WiFi network with WPA2 from Debian.

#!/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
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest