Wifi Manager - Ruby Script
#!/usr/bin/ruby
require 'rubygems'
require 'yaml'
root_path = File.dirname(__FILE__)
preferred_essids = YAML::load(File.open("#{root_path}/.hotspots.yml")).sort{|a,b| a[1]["priority"]<=>b[1]["priority"]}.collect{|x| x[0]}
class Hotspot
attr_accessor :essid, :encrypted
def initialize(essid="",encrypted="")
@essid = essid
@encrypted = encrypted
end
end
hotspots = `iwlist ath0 scanning`.split("Cell").collect{|x| x.split("\n").select{|y| y.include?("ESSID") || y.include?("Encryption key")}}.reject{|x| x.empty?}.collect{|x| [x[0][x[0].index("\"")+1..x[0].rindex("\"")-1],!x[1].include?("off")]}.reject{|x| x[0].empty?}.collect{|x| [x[0],x[1]]}.uniq.collect{|x| Hotspot.new(x[0],x[1])}
process = (process = `ps -e | grep dhcpcd`)[1..process.index(" ",2)]
viewable_essids = hotspots.collect{|x| x.essid}
connected = false
preferred_essids.each do |x|
unless connected
if viewable_essids.include?(x)
`kill #{process}`
`iwconfig ath0 essid #{x}`
`dhcpcd ath0`
# Should check to ensure connection
connected=true
puts "Connected to #{x}"
end
end
end
Original snippet written by Kevin Gisi
Last updated at 16:07 PM on Aug 08, 2008