跳转到主要内容

wifi切换

项目描述

Netswitch

网络切换。确保您正在连接到您想要的优先级。

适用于Linux(依赖于wpa_supplicant)

安装

pip install netswitch

Docker

或者您也可以使用Docker

# copy your wpa supplicant configs
cp -r ./aps /etc/wpa_supplicant/aps

docker run -d --privileged --network="host" \
    -v /etc/wpa_supplicant:/etc/wpa_supplicant \
    -e LIFELINE_SSID=mylifeline-2G \
    --name netswitch beasteers/netswitch

其中 ./aps 包含单个 wpa_supplicant 网络配置,并且文件名与文件中的SSID匹配

# all of the networks you want to be able to connect to
./aps/
    networkA.conf
    myspectrum-2G.conf
    myspectrum-5G.conf
    ...

如果您想从当前位置编辑aps,可以使用 -v ./aps:/etc/wpa_supplicant/aps 而不是复制

重要:如果您不提供任何aps来管理,它将不会做任何事情。

用法

网络切换

import time
import netswitch

# tell netswitch where to get its AP list. see explanation below
netswitch.set_ap_path('path/to/aps')


# create a new wpa supplicant file
netswitch.generate_wpa_config('my-network-2G', 'wifipassword')

# create your network switching rules
witch = netswitch.NetSwitch([
    {'interface': 'wlan*', 'ssids': 'lifeline'},
    'eth*',  # equivalent to {'interface': 'eth*'}
    'ppp*',
    {'interface': 'wlan*'},  # implied - 'ssids': '*'
])

# periodically check network
while True:
    time.sleep(10)
    connected = witch.check()
# or equivalently
witch.run(interval=10)

例如,假设您的设置是

  • 接口:wlan0(内置Pi 4 wifi),wlan1(usb wifi外置卡),ppp0(蜂窝移动网络)
  • 受信任的SSID:nyu,nyu-legacy

步骤 - 在这些步骤中的任何一点,如果我们连接,我们就完成了

  • 检查wlan1是否有s0nycL1f3l1ne,如果有,连接
  • 检查wlan0是否有s0nycL1f3l1ne,如果有,连接
  • 检查eth0是否通过接口连接到互联网
  • 检查ppp0是否通过接口连接到互联网
    • 我们是否应该控制wvdial或whatevs在这个 ???
  • 检查wlan1是否有[nyu,nyu-legacy],如果有,连接
  • 检查wlan0是否有[nyu,nyu-legacy],如果有,连接
  • 检查是否已经通过任何接口连接到互联网

修改WPA Supplicant

对于您可以连接的可用AP集合和凭证,通常它们存储在单个 /etc/wpa_supplicant/wpa_supplicant.conf 文件中,wpa服务将根据指定的优先级选择最佳配置。但这种方法存在问题,当有一个网络比另一个网络更强时,它不会切换。

那么,有什么不同呢?

  • 而不是一个大的文件,我们将它拆分成每个接入点(AP)一个文件(文件名与AP的SSID相同)。
  • 定期,将执行上述检查,我们将检查哪些网络在范围内,哪些是受信任的,哪些质量最高。如果不同的AP具有持续更高的质量(目前3/5个ping),我们将切换到新的AP。

所以调用netswitch.sync_aps('path/to/aps')告诉netswitch,您的每个AP凭据都存储在'path/to/aps'下的单独文件中。

您还可以调用netswitch.sync_aps('path/to/aps'),这将同步该目录与/etc/wpa_supplicant/aps/,并从中操作,这样就不会触及原始目录中的任何内容。

命令行界面(CLI)

# run the wifi switcher with a lifeline ssid to look out for
python -m netswitch run --lifeline mylifelinenetwork-2G

# get ip addresses for available interfaces
python -m netswitch ip
python -m netswitch ip 'wlan*' 'eth*'  # certain interfaces

# get available aps
python -m netswitch aps
python -m netswitch aps en0  # for mac

# get available interfaces
python -m netswitch iface
python -m netswitch iface '*tun*' 'eth*'

# restart interface
python -m netswitch restart wlan0

# list current wpa supplicant info
python -m netswitch wpa info

WPA Supplicant

import netswitch

# get current wpa supplicant file
wpa = netswitch.Wpa()

# get

蜂窝网络

import netswitch.cell

assert isinstance(netswitch.cell.signal_strength(), float)

with netswitch.cell.Cell('/dev/ttyUSB2') as cell:
    # read from stdind
    cell.chat()

待办事项

  • 测试 - .travis.yml
  • eth0连接,ppp0连接
  • /etc/network/interfaces设置?/说明?
  • 如果接口丢失或未连接,请重新启动接口?也许只针对0,或者如果它出现了一次?
  • 自连接以来的时间 - 在一段时间后重启。
  • /etc/network/interfaces ????

项目详情


下载文件

下载适用于您平台的应用程序。如果您不确定选择哪一个,请了解更多关于安装包的信息。

源分发

netswitch-0.1.4.tar.gz (16.1 KB 查看散列

上传时间:

支持