【问题标题】:Python: wifi subprocess.CalledProcessError: Command '['/sbin/ifdown', 'wlp4s0']' returned non-zero exit status 1Python:wifi subprocess.CalledProcessError:命令'['/sbin/ifdown','wlp4s0']'返回非零退出状态1
【发布时间】:2017-05-24 09:15:20
【问题描述】:

我正在编写一个 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

【问题讨论】:

  • 如果你手动运行/sbin/ifdown wlp4s0会发生什么?如果它不起作用,那么我们知道 python 脚本没有错。它可能需要以root 用户身份运行,例如通过sudo
  • 我已编辑帖子以回答您的问题。附言。如果我使用 sudo 运行 python 脚本,我会得到同样的错误

标签: python wifi


【解决方案1】:

除非我弄错了,否则我发现 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)

我自己目前仍在研究第二点。

祝你好运!

【讨论】:

    【解决方案2】:

    确保您的界面在

    中配置

    /etc/network/interfaces

    我的配置,例如:

    auto wlp7s0
    iface wlp7s0 inet loopback
    

    【讨论】:

      猜你喜欢
      • 2014-01-13
      • 2016-07-11
      • 2016-08-09
      • 2021-02-15
      • 2021-01-11
      • 2014-04-09
      • 2016-01-01
      • 1970-01-01
      • 2019-11-28
      相关资源
      最近更新 更多