0

I have a Raspberry-Pi zero w and I want to connect it to the internet using a python script. I know the network ESSID and Password. Is there any python library that can do this? Is there any way I can do it via command line? If there is, I can use the subprocess library to execute that command. I have used the following command but it doesnt seem to work:

sudo iwconfig wlan0 essid NETWORKID key s:PASSWORD

It seems to run successfully without any errors but it doesn't connect the raspberry-pi to the internet.

I know I can use nmcli, but for some reason, I am avoiding it. Is there any other way I can connect to the internet using a python script/commandline?

3
  • You probably COULD - but WHY? See How to set up networking/WiFi NOTE Raspbian DOES NOT use nmcli and other obsolete methods are incompatible with dhcpcd
    – Milliways
    Sep 15 '18 at 0:13
  • I need to run a python program which reads ESSID and password from a file, checks whether the the connected wifi is the same as that read from the file, and reconnects if its not. The link you gave above is for network configuration. Sep 15 '18 at 0:19
  • Your comment does not explain what you want to do - also Paste additional detail into your question. You can put as many networks as you like into wpasupplicant and set priorities.
    – Milliways
    Sep 15 '18 at 0:24
2

On a Raspberry Pi we use wpa_supplicant for wifi. It has a command line interface called wpa_cli. With it you can do almost all with wpa_supplicant including get/set network and set identity and password. I would start wpa_supplicant on boot up as usual because wpa_cli wants to connect to a running daemon. Then you should be able to compare and reconnect the network with wpa_cli also by script.

2
  • dhcpcd starts wpa_supplicant through its 10-wpa_supplicant hook
    – Milliways
    Sep 15 '18 at 1:00
  • @Milliways Yes, but it is completely isolated running outside the init system. systemctl status wpa_supplicant.service shows a dead and disabled service. Very confusing.
    – Ingo
    Sep 16 '18 at 13:27

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.