2

I have a use case where I need to read USB drive for wifi credential and add it to wpa_supplicant.conf file. It seems possible to add those details into the wpa_supplicant.conf. But I can see there will be multiple entries for the same SSID and password whenever USB with same network details are inserted.

I can see three possible ways but not sure how to implement that -

  1. I wanted to create a custom conf file and to somehow link this new file to wpa_supplicant.conf. So that I only need to override that custom file with wifi details. Is this something possible?

  2. Can I directly connect to wifi using that SSID and Password without adding those details to wpa_supplicant.conf?

  3. Is there any conf parser that can be used to check same SSID and Password and then add entries?

1

As far as I understand you want to get the WiFi credentials from an USB drive to be used for the current WiFi connection. There are several ways to do it. I will use a simple example bash script.

First solution: the credential file on the USB drive is just the wpa_supplicant.conf

This is very simple. You can just set a symlink to the credential file:

rpi ~$ sudo rm /etc/wpa_supplicant/wpa_supplicant.conf
rpi ~$ sudo ln /media/usb-drive/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf

When the credential file has changed, maybe with mounting another USB-drive, then just reconnect wpa_supplicant with:

rpi ~$ sudo wpa_cli -i wlan0 disconnect
rpi ~$ sudo wpa_cli -i wlan0 reconfigure
rpi ~$ sudo wpa_cli -i wlan0 reconnect

Second solution: modify wpa_supplicant.conf

As credential file I will use:

rpi ~$ cat /media/usb-drive/credential.conf
SSID="mynetwork"
PSK="mypassword"
rpi ~$

and use this script ./change_credential.sh:

#!/bin/bash
CREDENTIAL_CONF=$1
WPA_SUPPLICANT_CONF=/etc/wpa_supplicant/wpa_suppliant.conf

# get credentials in $SSID and $PSK
source $CREDENTIAL_CONF

# modify wpa_supplicant.conf in place using sed
/bin/sed -i "s/ssid=\".*\"/ssid=\"$SSID\"/" $WPA_SUPPLICANT_CONF
/bin/sed -i "s/psk=\".*\"/psk=\"$PSK\"/" $WPA_SUPPLICANT_CONF

# switch the connection with wpa_cli
/sbin/wpa_cli -i wlan0 disconnect
/sbin/wpa_cli -i wlan0 reconfigure
/sbin/wpa_cli -i wlan0 reconnect

Don't forget to make the script executable and execute it:

rpi ~$ chmod +x change_credential.sh
rpi ~$ sudo ./change_credential.sh /media/usb-drive/credential.conf

Third solution: manage wpa_supplicant direct

Here we do not use a wpa_supplicant.conf. We do everything on the fly. The credential file is the same as from the second solution. For example I have this script ./change_connection.sh:

#!/bin/bash
CREDENTIAL_CONF=$1

# get credentials in $SSID and $PSK
source $CREDENTIAL_CONF

# manage the connection with wpa_cli
/sbin/wpa_cli -i wlan0 disconnect
/sbin/wpa_cli -i wlan0 remove_network 0
/sbin/wpa_cli -i wlan0 add_network
/sbin/wpa_cli -i wlan0 set_network 0 ssid \"$SSID\"
/sbin/wpa_cli -i wlan0 set_network 0 psk \"$PSK\"
/sbin/wpa_cli -i wlan0 enable_network 0
/sbin/wpa_cli -i wlan0 reconnect

Use it with:

rpi ~$ chmod +x change_connection.sh
rpi ~$ sudo ./change_connection.sh /media/usb-drive/credential.conf

Of course you are free to use your favorite programming language and make your own script/program to manage wpa_supplicant.

1
  • Thank you, I will try these solution and will get back to you.
    – Irfan
    May 14 '19 at 10:22
1
  1. It should be possible to use a link. You would loose the original content, so why not just overwrite the file?

  2. I remember seeing configurations with a wpa_supplicant.conf.d directory, but my man page doesn't mention that.

  3. You don't need a full parser, just grep for ssid="SSID-Of-WLAN".

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.