Wifi Manager - Ruby Script

Tagged with Ruby wifi

Language: Ruby

View as text

#!/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

SnippetStash costs money to host and develop. The service is free for everyone to use
but if you found it useful please consider making a small donation.