awkman / pywifi

A cross-platform module for manipulating WiFi devices.
MIT License
447 stars 154 forks source link

Not working on Ubuntu 18.04 as normal user #48

Open bitinerant opened 5 years ago

bitinerant commented 5 years ago
$ lsb_release -d
Description:    Ubuntu 18.04.2 LTS
$ python3 example.py  # this is the code from README.md
Traceback (most recent call last):
  File "example.py", line 7, in <module>
    iface = wifi.interfaces()[0]
  File "/home/cor/.local/lib/python3.6/site-packages/pywifi/wifi.py", line 42, in interfaces
    for interface in wifi_ctrl.interfaces():
  File "/home/cor/.local/lib/python3.6/site-packages/pywifi/_wifiutil_linux.py", line 281, in interfaces
    for f in sorted(os.listdir(CTRL_IFACE_DIR)):
PermissionError: [Errno 13] Permission denied: '/var/run/wpa_supplicant'
$ sudo chmod 755 /var/run/wpa_supplicant
$ python3 example.py
Traceback (most recent call last):
  File "example.py", line 7, in <module>
    iface = wifi.interfaces()[0]
  File "/home/cor/.local/lib/python3.6/site-packages/pywifi/wifi.py", line 42, in interfaces
    for interface in wifi_ctrl.interfaces():
  File "/home/cor/.local/lib/python3.6/site-packages/pywifi/_wifiutil_linux.py", line 288, in interfaces
    self._connect_to_wpa_s(f)
  File "/home/cor/.local/lib/python3.6/site-packages/pywifi/_wifiutil_linux.py", line 304, in _connect_to_wpa_s
    sock.connect(ctrl_iface)
PermissionError: [Errno 13] Permission denied
$ 
niezw commented 5 years ago

i am also, has some solution?

Minipada commented 5 years ago

Running into the same issue

Oxmel commented 4 years ago

I'm a bit late to the party, but this issue is not directly related to pywifi. Because by default, root privileges are required to access the control interface of wpa_supplicant.

Also, since pywifi uses the same control interface than wpa_cli, you can reproduce the error by using this command as a regular user on Ubuntu :

$ wpa_cli status
Failed to connect to non-global ctrl_ifname: (nil)  error: Permission denied

To fix the issue, you have to allow non-root users to access the control interface. To do so, edit (or create) the config file of wpa_supplicant and add these options :

(replace 'some_group' by the name of a group which the user belongs. Or the name of the user itself)

#/etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=some_group
update_config=1

Then kill wpa_supplicant if it's running and start it with the new options :

(add the option '-B' to run it in the background)

$ sudo wpa_supplicant -D nl80211 -i wlan_interface_name -c /etc/wpa_supplicant/wpa_supplicant.conf
bitinerant commented 4 years ago

@Oxmel - This is a slow-moving party. :smiley:

You are correct that, on most Linux distributions, root is required, at least temporarily, to manipulate WPA supplicant. In my opinion, this is not an acceptable solution because not all end users have access to root.

Fortunately, there is an alternative, at least for distributions that use Network Manager such as Ubuntu. The nmcli tool can do (I think) everything that pywifi needs, and it can do it all without root permissions. We just need someone to program the interface between pywifi and nmli. I actually began this work some months ago, but didn't have time to finish or test. It is still something I am interested in though.

Oxmel commented 4 years ago

@bitinerant You're completely right, the workaround i given above is rather hackish compared to interfacing with nmcli, which would be a much better solution.

emendir commented 2 years ago

@Oxmel - This is a slow-moving party. smiley

You are correct that, on most Linux distributions, root is required, at least temporarily, to manipulate WPA supplicant. In my opinion, this is not an acceptable solution because not all end users have access to root.

Fortunately, there is an alternative, at least for distributions that use Network Manager such as Ubuntu. The nmcli tool can do (I think) everything that pywifi needs, and it can do it all without root permissions. We just need someone to program the interface between pywifi and nmli. I actually began this work some months ago, but didn't have time to finish or test. It is still something I am interested in though.

Great! Two and a half years later, any progress?

moonninja commented 1 year ago

Solution that worked for. Install python3, pip3 and pywifi on root ( use sudo for all installation if your user is in sudoers file, if you don't understand this just go an google). Then "sudo python3 scirptname.py" and it will work. Main issue is root privileges.