User avatar
NPDedyukhin
Posts: 29
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

The script that edits the file "wpa_supplicant.conf"

Tue Jan 07, 2020 6:38 am

Good afternoon! :)

I have a text file on my device that contains a list. (lines).

lines[0] is the name of the WiFi network that the user entered earlier.

lines[1] - this is the password from the WiFi network that the user entered earlier.

Now my task is to edit the file “wpa_supplicant.conf” with the help of a python script in such a way as to enter into it the data that the user entered.

Does anyone know how to do this? :|
Respectfully,
Nikita Dedyukhin

epoch1970
Posts: 4202
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: The script that edits the file "wpa_supplicant.conf"

Tue Jan 07, 2020 10:42 am

You could use wpa_cli for this kind of things. The only prerequisite would be that the initial wpa_supplicant.conf file has "update_config=1" in it.
https://unix.stackexchange.com/question ... t-always-r
Or D-Bus I suppose.

AFAIK the format of that file is proprietary; although it looks vaguely like json or libconfig, it is not one of these "standards" so a ready-made parser will not work.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

jahboater
Posts: 5021
Joined: Wed Feb 04, 2015 6:38 pm

Re: The script that edits the file "wpa_supplicant.conf"

Tue Jan 07, 2020 11:14 am

NPDedyukhin wrote:
Tue Jan 07, 2020 6:38 am
Now my task is to edit the file “wpa_supplicant.conf” with the help of a python script in such a way as to enter into it the data that the user entered.

Does anyone know how to do this? :|
Sounds like a great little project for learning python :)
Use a temporary file while experimenting so as not to mess up the real one.

Learn in small steps:-

Read in the source file.
Parse the source file contents.
Print the values to the screen so you know its been read and parsed OK.
Read in and write out the temporary wpa_supplicant.conf file (check its unchanged with cmp).
Write a second copy of the original wpa file as a backup (wpa_supplicant.conf.save)
Next insert the users data into it before writing.
Finally, add a check that its not there already to avoid duplicates.

Note that you could just open the wpa_supplicant.conf file for append and simply write the new entry to it.
The trouble with that is if you ran the program twice you get duplicate entries.

ankith26
Posts: 218
Joined: Mon Mar 25, 2019 11:08 am
Location: /home/pi/pythonprojects/test.py
Contact: Website

Re: The script that edits the file "wpa_supplicant.conf"

Wed Jan 08, 2020 9:58 am

But the most important point to remember is:-
When opening the wpa supplicant file, use
open(filename, “a”)
What this does is that it opens the file and allows you to only append(or add) extra data to it.
If you specify the write method, it completely ERASES THE FILES TEXT and writes whatever you want it to write.
So remember to use “append” method while working with this file
I sat thinking for 5 minutes on what to put here. Finally I put something like this.
Check out my github page @ https://github.com/ankith26

jahboater
Posts: 5021
Joined: Wed Feb 04, 2015 6:38 pm

Re: The script that edits the file "wpa_supplicant.conf"

Wed Jan 08, 2020 10:03 am

ankith26 wrote:
Wed Jan 08, 2020 9:58 am
So remember to use “append” method while working with this file
What happens if you run the program twice, or there is an existing entry with the same SSID but different credentials?

ankith26
Posts: 218
Joined: Mon Mar 25, 2019 11:08 am
Location: /home/pi/pythonprojects/test.py
Contact: Website

Re: The script that edits the file "wpa_supplicant.conf"

Thu Jan 09, 2020 3:22 am

The data is appended twice.
Will that be a problem?
I am not sure but i guess that the os ignores duplicate entries in the file.

If that is not the case then a smarter python system is required which reads the file first and overwrites it with some changed data. This program also has to check wether the network settings exist, etc etc.
I sat thinking for 5 minutes on what to put here. Finally I put something like this.
Check out my github page @ https://github.com/ankith26

Return to “Python”