I would like to write a script (.sh or similar) that automatically connects to a wifi network when I know the name and password of the network. Can anyone give me an example of how to do this? I am having trouble getting wpa supplicant to work.
Asked
Active
Viewed 791 times
1 Answers
0
You can create a script to write AP config to /etc/wpa_supplicant.conf
and starting up wpa_supplicant
with the created configuration file manually. Please refer to the following example:
wlan_interface="wlan0" # Modify as needed
connect_wpa2_ap() {
echo Connecting to WPA2 AP with SSID $1
# config_wpa_supplicant $1 $2
echo -e "\
ctrl_interface=/tmp/ctrl-$wlan_interface.pid
eapol_version=1
ap_scan=1
p2p_disabled=1
network={
ssid=\"$1\"
scan_ssid=1
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP
psk=\"$2\"
}
" >/etc/wpa_supplicant.conf
/sbin/wpa_supplicant -i "$wlan_interface" -B -c/etc/wpa_supplicant.conf
}
Connect with connect_wpa2_ap <ssid> <password>
.

Kong Chun Ho
- 406
- 4
- 12