项目作者: mark2b

项目描述 :
wpa-connect
高级语言: Go
项目地址: git://github.com/mark2b/wpa-connect.git
创建时间: 2016-12-09T20:13:03Z
项目社区:https://github.com/mark2b/wpa-connect

开源协议:MIT License

下载


wpa-connect

Package provides API for connection Linux device to Wi-Fi Network.

wpa-connect communicates with WPA supplicant over D-Bus (linux message bus system).

This package was developed as part of IoT project in order to add Wi-Fi connectivity to headless Raspberry Pi like devices. No need for connman or Network Manager be installed.

Setup

On Linux:

wpa_supplicant service should run with -u flag in order to enable DBus interface. Run it as Linux service before first call to wpa_supplicant. Otherwise system will start it automatically without -u flag.

Systemd service configuration file - /etc/systemd/system/wpa_supplicant@wlan0.service

  1. [Unit]
  2. Description=WPA supplicant for %i
  3. [Service]
  4. ExecStart=/usr/sbin/wpa_supplicant -u -i%i -c/etc/wpa_supplicant.conf -Dwext
  5. [Install]
  6. WantedBy=multi-user.target

On Raspberry PI OS (Debian Buster):

Raspbery PI OS (formerely known as Raspbian) uses dhcpd-run-hooks to setup and invoke the wpa_supplicant daemon.

  1. Disable the systemd managed wpa_supplicant located under /etc/systemd/dbus-fi.w1.wpa_supplicant1.service by running sudo systemctl disable wpa_supplicant
  2. Modify the existing wpa_supplicant dhcpd-run-hook available under /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant by adding the -u flag to the invocation of the wpa_supplicant daemon in the wpa_supplicant_start() function.
  3. Alternatively run sudo sed -i 's/wpa_supplicant -B/wpa_supplicant -u -B/g' /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant to modify the hook in place.

On Project:

  1. go get github.com/mark2b/wpa-connect

Usage

Please see godoc.org for documentation. (Not ready yet)

Examples

Connect to Wi-Fi network

  1. import wifi "wpa-connect"
  2. if conn, err := wifi.ConnectManager.Connect(ssid, password, time.Second * 60); err == nil {
  3. fmt.Println("Connected", conn.NetInterface, conn.SSID, conn.IP4.String(), conn.IP6.String())
  4. } else {
  5. fmt.Println(err)
  6. }

Scan for Wi-Fi networks

  1. import wifi "wpa-connect"
  2. if bssList, err := wifi.ScanManager.Scan(); err == nil {
  3. for _, bss := range bssList {
  4. print(bss.SSID, bss.Signal, bss.KeyMgmt)
  5. }
  6. }

Package release under a MIT license.