Skip to content

Instantly share code, notes, and snippets.

@ross-nordstrom
Last active August 29, 2015 14:07
Show Gist options
  • Save ross-nordstrom/6c87d0e0695d48041b5a to your computer and use it in GitHub Desktop.
Save ross-nordstrom/6c87d0e0695d48041b5a to your computer and use it in GitHub Desktop.
Get a breakdown of 2.4GHz channel utilization. Assumes wireless interface accessible at "wlan0".
#!/usr/bin/env ruby
require 'json'
ALLOWED_CHANNELS = ['Ch 1', 'Ch 6', 'Ch 11']
wifi = `iwlist wlan0 scan`
pieces2ghz = wifi.split("Cell ")[1..-1].select{|x| x.include?('Frequency:2') && x.include?('Channel ') }
data = pieces2ghz.map{ |x| [ x.split('Channel ')[1].split(')')[0].to_i, (x.split('Signal level=')[1].split('/')[0] rescue '0').to_i, (x.split('Protocol:')[1].split("\n")[0] rescue '[UNKNOWN]') ] }
channels = {}
channels = data.reduce(channels) do |channels, x|
k = "Ch #{x[0]}"
channels[k] = channels[k] ? channels[k] + 1 : 1
channels
end
if channels.empty?
puts "Unable to get Wi-Fi information. Is Wi-Fi on?"
exit 1
end
interferers = channels.keys - ALLOWED_CHANNELS
if !interferers.empty?
interference = interferers.reduce(0) { |sum, ch| channels[ch].to_i + sum }
end
channels[:interference] = (interference == 0) ? null : interference
channels[:interference_state] = interference > 5 ? 3 : (interference > 1 ? 2 : 1)
puts channels.to_json.gsub(/"/, "'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment