Skip to content

Instantly share code, notes, and snippets.

@eagletmt
Created July 28, 2013 09:51
Show Gist options
  • Save eagletmt/6098100 to your computer and use it in GitHub Desktop.
Save eagletmt/6098100 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __FILE__)
require 'bundler/setup'
require 'dbus'
class Wicd
NOT_CONNECTED = 0
CONNECTING = 1
WIRELESS = 2
WIRED = 3
SUSPENDED = 4
def initialize(bus)
@bus = bus
service = bus.service 'org.wicd.daemon'
wireless = service.object '/org/wicd/daemon/wireless'
wireless.introspect
@wireless = wireless['org.wicd.daemon.wireless']
wicd = service.object '/org/wicd/daemon'
wicd.introspect
@wicd = wicd['org.wicd.daemon']
@wicd.on_signal 'StatusChanged', &method(:on_status_changed)
@wireless.on_signal 'SendEndScanSignal', &method(:on_send_end_scan_signal)
@essid_priority = %w[
moxanet2
moxanet
moxanet1
lambda-net2
titech-pubnet
]
ret = @wicd.GetConnectionStatus.first
@last_state = ret[0]
on_status_changed *ret
end
def scan!
@wireless.Scan false
end
def on_send_end_scan_signal
n = @wireless.GetNumberOfNetworks.first
@scan = {}
n.times do |i|
@scan[@wireless.GetWirelessProperty(i, 'essid').first] = i
end
connect!
end
def connect!
essid = @essid_priority.find { |essid| @scan.has_key? essid }
if essid
@wireless.ConnectWireless @scan[essid]
else
puts 'No available networks'
sleep 5
scan!
end
end
def on_status_changed(state, info)
case state
when Wicd::NOT_CONNECTED
if @last_state != Wicd::NOT_CONNECTED
on_disconnected
end
scan!
when Wicd::SUSPENDED
puts 'SUSPENDED'
when Wicd::CONNECTING
if info[0] == 'wired'
puts 'CONNECTING (wired)'
else
essid = info[1]
puts "CONNECTING (wireless) #{essid}"
end
when Wicd::WIRELESS
if @last_state != Wicd::WIRELESS
on_wireless_connected *info
end
when Wicd::WIRED
if @last_state != Wicd::WIRED
on_wired_connected *info
end
else
raise "Unknown state #{state}"
end
@last_state = state
end
def on_disconnected
puts 'Disconnected'
end
def on_wired_connected(ip)
puts "Connected (#{ip})"
end
def on_wireless_connected(ip, network, network_id, strength, bitrate)
puts "Connected to #{network} (#{ip})"
end
end
bus = DBus::SystemBus.instance
wicd = Wicd.new bus
main = DBus::Main.new
main << bus
main.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment