Python在Ubuntu上通过wifi连接到设备

时间:2017-07-12 12:56:28

标签: python-2.7 wifi ubuntu-16.04 connect

欢迎我在Ubuntu 16.04上通过wifi连接设备时遇到问题。我找到了有人解释如何做的页面: https://wifi.readthedocs.io/en/latest/wifi_command.html#tutorial

我像创作者一样一步一步地走了:

在终端:

$ pip install wifi

sudo wifi扫描

#wifi connect --ad-hoc essid

之后我写了密码。

但是当我这样做时,我得到了一个错误:

追踪(最近一次呼叫最后一次):

文件“/ usr / local / bin / wifi”,第202行,in     args.func(参数)

文件“/ usr / local / bin / wifi”,第90行,在connect_command中     scheme.activate()

文件“/home/krzysztof/.local/lib/python2.7/site-packages/wifi/scheme.py”,第172行,激活

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

文件“/usr/lib/python2.7/subprocess.py”,第574行,在check_output中     引发CalledProcessError(retcode,cmd,output = output)

subprocess.CalledProcessError:命令'['/ sbin / ifdown','wlan0']'  返回非零退出状态1

我确信我写的密码是正确的。

我已经通过

使其成为第二种方式

$ WIFI_CLI_NAME = cool-wifi pip install wifi

但效果是一样的。

1 个答案:

答案 0 :(得分:0)

我在Github找到了示例代码:

import wifi


def Search():
    wifilist = []

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

    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('wlan0', ssid)

    if cell:
        return cell

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


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

    scheme = wifi.Scheme.for_cell('wlan0', 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


if __name__ == '__main__':
    # Search WiFi and return WiFi list
    print Search()

    # Connect WiFi with password & without password
    print Connect('OpenWiFi')
    print Connect('ClosedWiFi', 'password')

    # Delete WiFi from auto connect list
    print Delete('DeleteWiFi')

enter code here

但是我在编译期间遇到错误:

我已将此文件保存到polacz.py,之后我制作了sudo chmod + x polacz.py

和./polacz.py

./ polacz.py:linia 6:由无法预料的标记语法错误('

./ polacz.py:linia 6:`def Search():'