Python: wifi subprocess.CalledProcessError: Command '['/sbin/ifdown', 'wlp4s0']' returned non-zero exit status 1

牧云@^-^@ 提交于 2021-01-27 13:25:44

问题


I am working on a python script to automatically connect to a known Wifi. I am using the following library https://wifi.readthedocs.io/en/latest/ which seems to work very well. The only problem is that when a try to connect to a chosen Wifi through the scheme.activate() command, it returns the following error:

    Traceback (most recent call last):
  File "wifi_connection.py", line 100, in <module>
    print Connect('dotbot', 'pass')
  File "wifi_connection.py", line 64, in Connect
    savedcell.activate()
  File "/home/pietro/.local/lib/python2.7/site-packages/wifi/scheme.py", line 172, in activate
    subprocess.check_output(['/sbin/ifdown', self.interface], stderr=subprocess.STDOUT)
  File "/usr/lib/python2.7/subprocess.py", line 574, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['/sbin/ifdown', 'wlp4s0']' returned non-zero exit status 1

which I really don't understand.

The name of the script is wifi_connection.py and the code is the following:

import wifi


def Search():
    wifilist = []

    cells = wifi.Cell.all('wlp4s0')

    for cell in cells:
        wifilist.append(cell)

    return wifilist


def FindFromSearchList(ssid):
    wifilist = Search()

    for cell in wifilist:
        if cell.ssid == ssid:
            return cell

    return False


def FindFromSavedList(ssid):
    cell = wifi.Scheme.find('wlp4s0', ssid)

    if cell:
        return cell

    return False


def Add(cell, password=None):
    if not cell:
        return False

    scheme = wifi.Scheme.for_cell('wlp4s0', cell.ssid, cell, password)
    scheme.save()
    return scheme


def Delete(ssid):
    if not ssid:
        return False

    cell = FindFromSavedList(ssid)

    if cell:
        cell.delete()
        return True

    return False


def Connect(ssid, password):
    cell = FindFromSearchList(ssid)

    if cell:
        savedcell = FindFromSavedList(cell.ssid)

        # Already Saved from Setting
        if savedcell:
            savedcell.activate()
            return cell

        # First time to connect
        else:
            if cell.encrypted:
                if password:
                    scheme = Add(cell, password)

                    try:
                        scheme.activate()

                    # Wrong Password
                    except wifi.exceptions.ConnectionError:
                        Delete(ssid)
                        return False

                    return cell
                else:
                    return False
            else:
                scheme = Add(cell)

                try:
                    scheme.activate()
                except wifi.exceptions.ConnectionError:
                    Delete(ssid)
                    return False

                return cell

    return False

print " "
print Search()
print " "
print Connect('dotbot', 'pass')
print " "

where wlp4s0 is the name of the wifi interface, "dotbot" and "pass" are respectively the name of the wifi and its password.

Thank you in advance for your help.

The strange thing is that when I run the command "ifconfig", I get:

wlp4s0    Link encap:Ethernet  IndirizzoHW e0:06:e6:f8:53:29  
          indirizzo inet:192.168.0.116  Bcast:192.168.0.255  
          Maschera:255.255.255.0
          indirizzo inet6: fe80::525e:7c8d:6f43:9d98/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:222347 errors:0 dropped:0 overruns:0 frame:96541
          TX packets:147762 errors:0 dropped:0 overruns:0 carrier:0
          collisioni:0 txqueuelen:1000 
          Byte RX:208449235 (208.4 MB)  Byte TX:17616899 (17.6 MB)
          Interrupt:19 

but if I try "/sbin/ifdown wlp4s0", then I get:

Unknown interface wlp4s0

回答1:


Unless I am mistaken, what I have found is that ifdown/ifup don't seem to be used anymore. I have fixed your first error within my own project but I cannot seem to fix the second part.

ifdown wlan0 has been changed to ifconfig wlan0 down and ifup to ifconfig wlan0 up

So, change the scheme.py script that comes from this wifi package here:

    subprocess.check_output(['/sbin/ifdown', self.interface], stderr=subprocess.STDOUT)

change that to be:

    subprocess.check_output(['/sbin/ifconfig', self.interface,'down'], stderr=subprocess.STDOUT)

I am still working on the second bit at the moment myself.

Good luck!




回答2:


make sure your interface is configured in

/etc/network/interfaces

my config, for example:

auto wlp7s0
iface wlp7s0 inet loopback


来源:https://stackoverflow.com/questions/44154053/python-wifi-subprocess-calledprocesserror-command-sbin-ifdown-wlp4s0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!