4

I have the following script that disconnects from the current WiFi network and reconnects based on the content of wpa_supplicant.conf:

#!/bin/bash

if iwgetid; then
    sudo wpa_cli terminate
    sudo ip addr flush wlan0
    sudo ip link set dev wlan0 down
    sudo rm -r /var/run/wpa_supplicant > /dev/null
fi

sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1
sudo service ntp restart
sudo ip link set dev wlan0 down
sudo ip link set dev wlan0 up
sudo dhclient wlan0

The problem is, if the credentials in wpa_supplicant are not correct (these are based on user input in my app, so very possible), dhclient hangs for quite a while, trying to connect to a DHCP server and assign an IP address before eventually failing.

Is there a way to test that the credentials are correct before calling dhclient?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250

2 Answers2

3

This is a sample ./script including the wpa_passphrase command to set up The SSID and the password:

#!/bin/bash

if iwgetid; then
    wpa_cli terminate
    ip addr flush wlan0
    ip link set dev wlan0 down
    rm -r /var/run/wpa_supplicant > /dev/null
    ip link set dev wlan0 up
fi

wpa_supplicant -B -i wlan0 -c<(wpa_passphrase "$1" "$2") > /dev/null 2>&1
dhclient wlan0
service ntp restart
exit

Usage:

sudo ./script "My SSID" "My-Password"

Update

If the wpa_supplicant is successful authenticated with the given ssid and pass you will get WPA: Key negotiation completed on the log file.

The -f option added to the wpa_supplicant command to provid a log file.

You can check the exit status of grep before executing the dhclient command.

#!/bin/bash

if iwgetid; then
    wpa_cli terminate
    ip addr flush wlan0
    ip link set dev wlan0 down
    rm -r /var/run/wpa_supplicant > /dev/null
    ip link set dev wlan0 up
fi
# A minimal wpa_supplicant.conf configuration file.

echo -e "\nctrl_interface=/run/wpa_supplicant \nupdate_config=1\n" > /etc/wpa_supplicant/wpa_supplicant.conf

# A prompt for SSID and WPA passphrase. The -f option to get a log file.
# sleep command will be useful because the authentication will take a few 
# second before executing grep.

echo "" > logfile

wpa_supplicant -B -i wlan0 -c<(wpa_passphrase "$1" "$2") -f logfile

sleep 10

grep -c "WPA: Key negotiation completed" logfile

if [ $? -eq 0 ] 
   then
   echo "Key negotiation completed successfully" 
   timeout 15 dhclient wlan0
   exit 0
else
  echo "Authentication failed"
fi
exit 1

Usage:

sudo ./script "My SSID" "My-Password"
GAD3R
  • 63,407
  • 31
  • 131
  • 192
  • This almost works great. I can connect over and over with the correct credentials. I get the appropriate message when I enter the wrong credentials. However, when I input the wrong ones, then the right ones, I get Authentication Failed. – Ryan Bobrowski Mar 13 '18 at 22:43
  • wlan0: CTRL-EVENT-SSID-REENABLED id=0 ssid="my_ssid" wlan0: Associated with some_ipv6_address wlan0: CTRL-EVENT-DISCONNECTED bssid=some_ipv6_address reason=0 locally_generated=1 wlan0: CTRL-EVENT-DISCONNECTED bssid=some_ipv6_address reason=0 locally_generated=1 wlan0: WPA: 4-Way Handshake failed - pre-shared key may be incorrect wlan0: WPA: 4-Way Handshake failed - pre-shared key may be incorrect wlan0: CTRL-EVENT-SSID-TEMP-DISABLED id=0 ssid="my_ssid" auth_failures=8 duration=116 reason=WRONG_KEY – Ryan Bobrowski Mar 13 '18 at 22:44
1

I would break it up into separate functions, allowing me to test the connection to wifi before reconfiguring the network.

#!/usr/bin/env bash
CONF="/etc/wpa_supplicant/wpa_supplicant.conf"

function connect_wifi() {
    wpa_supplicant -B -i wlan0 -c $CONF >/dev/null 2>&1
}

function connect_tcpip() {
    service ntp restart
    ip link set dev wlan0 down
    ip link set dev wlan0 up
    dhclient wlan0
}

FUNC_CW=$(declare -f connect_wifi)
FUNC_CT=$(declare -f connect_tcpip)

if (sudo bash -c "$FUNC_CW; connect_wifi"); then
    sudo bash -c "$FUNC_CT; connect_tcpip"
else
    echo "Error Connecting to Wifi.  Please check your credentials and try again."
fi
Tim Kennedy
  • 19,369
  • 4
  • 38
  • 58