簡體   English   中英

Python:wifi subprocess.CalledProcessError:命令'['/ sbin / ifdown','wlp4s0']'返回非零退出狀態1

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

我正在使用python腳本自動連接到已知的Wifi。 我使用以下庫https://wifi.readthedocs.io/en/latest/似乎工作得很好。 唯一的問題是,當嘗試通過scheme.activate()命令連接到選定的Wifi時,它會返回以下錯誤:

    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

我真的不明白。

腳本的名稱是wifi_connection.py,代碼如下:

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 " "

其中wlp4s0是wifi接口的名稱,“dotbot”和“pass”分別是wifi及其密碼的名稱。

預先感謝您的幫助。

奇怪的是,當我運行命令“ifconfig”時,我得到:

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 

但如果我嘗試“/ sbin / ifdown wlp4s0”,那么我得到:

Unknown interface wlp4s0

除非我弄錯了,我發現ifdown / ifup似乎不再被使用了。 我已經在我自己的項目中修復了你的第一個錯誤,但我似乎無法修復第二部分。

ifdown wlan0已經更改為ifconfig wlan0 down而ifup更改為ifconfig wlan0 up

所以,在這里更改來自這個wifi包的scheme.py腳本:

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

改變是:

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

我現在還在努力做第二點。

祝好運!

確保您的界面已配置

的/ etc /網絡/接口

我的配置,例如:

auto wlp7s0
iface wlp7s0 inet loopback

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM