Created
May 29, 2014 23:55
-
-
Save aphyr/182e4cf9b7c2be8487c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'open3' | |
# What's the network interface name? | |
def ifaces | |
`iw dev`.lines.select { |l| l["Interface "] }.map { |l| l.split(/\s+/).last } | |
end | |
# First iface | |
def iface | |
ifaces.first | |
end | |
# Is iface up? | |
def up? iface | |
!!(`ip link show #{iface}` =~ /<.*?UP.*?>/) | |
end | |
# Force an iface up | |
def up! iface | |
`sudo ip link set #{iface} up` | |
iface | |
end | |
# Force an iface down | |
def down! iface | |
`sudo ip link set #{iface} down` | |
iface | |
end | |
# Is there a network connection? | |
def link? iface | |
! (`iw #{iface} link` =~ /Not connected/) | |
end | |
# Available networks, as a map of BSS->{...} | |
def networks | |
`sudo iw wlan0 scan`.lines.reduce([nil, {}]) do |pair, line| | |
bss, map = pair | |
net = map[bss] | |
if line =~ /^BSS (.+?) / | |
# New network | |
map[$1] = {} | |
[$1, map] | |
else | |
case line | |
when /^\tSSID: (.+)/ | |
net[:SSID] = $1 | |
when /^\tRSN:/ | |
net[:RSN] = {} | |
end | |
map[bss] | |
pair | |
end | |
end | |
end | |
# Does a network require RSN? | |
def rsn? network | |
network[:RSN] | |
end | |
# Emits wpa_supplicant config file | |
def supplicant_config ssid, passphrase | |
Open3::popen2("wpa_passphrase #{ssid}") do |stdin, stdout, wait_thr| | |
stdin.puts passphrase | |
stdin.close | |
stdout.read | |
end | |
end | |
# Launch wpa_supplicant | |
def ensure_wpa_supplicant! | |
if `ps aux | grep wpa_supplicant`.lines.size <= 1 | |
`sudo wpa_supplicant -B -D wext -i wlan0 -c /etc/wpa_supplicant.conf` | |
end | |
end | |
def wpa_supplicant_config_file | |
"/etc/dbus-1/system.d/" | |
end | |
def supplicate! iface, ssid, passphrase | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment