Skip to content

Instantly share code, notes, and snippets.

@jspc
Created December 8, 2015 13:44
Show Gist options
  • Save jspc/7fe40b14b5a53b14c204 to your computer and use it in GitHub Desktop.
Save jspc/7fe40b14b5a53b14c204 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'mac_vendor'
mac_vendor = MacVendor.new
stations = {}
dns_leases = %x{systemctl status wifi}.split("\n").grep(/dnsmasq/).first.split.last
%x{iw dev ap0 station dump}.split("Station").each do |station|
mac_str = ''
next if station.empty?
station.split("\n").each_with_index do |station_line, index|
station_line.strip!
if index == 0
mac = station_line.split.first
vendor = mac_vendor.lookup(mac)
mac_str = "#{mac} (#{vendor ? vendor[:name] : 'unknown'})"
stations[mac_str] = {}
ip = open(dns_leases) { |f| f.each_line.detect { |line| /#{mac}/.match(line) } }.split[2]
host = open(dns_leases) { |f| f.each_line.detect { |line| /#{mac}/.match(line) } }.split[3]
stations[mac_str]['hostname'] = host
stations[mac_str]['ip'] = ip
else
name, value = station_line.split(':')
stations[mac_str][name] = value.strip
end
end
end
stations.each do |mac, details|
puts "#{details['hostname']} - #{details['ip']}"
puts mac
puts 'speed'
puts "\tdown: #{details['rx bitrate']}, up: #{details['tx bitrate']}"
puts 'data'
puts "\tdown: #{details['rx bytes'].to_i / 1024} mb, up: #{details['tx bytes'].to_i / 1024} mb"
puts '--'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment