Module: OmfRc::Util::Iw
- Includes:
- Cocaine, Hashie, ResourceProxyDSL, Hostapd, Ip, Wpa
- Included in:
- ResourceProxy::Wlan
- Defined in:
- lib/omf_rc/util/iw.rb
Overview
Utility for executing command ‘iw’
Constant Summary
Constants included from ResourceProxyDSL
ResourceProxyDSL::DEFAULT_PROP_ACCESS, ResourceProxyDSL::PROXY_DIR, ResourceProxyDSL::UTIL_DIR
Instance Method Summary collapse
-
#add_interface ⇒ String
Add interface to device.
-
#configure_mode(opts) ⇒ String
Configure the interface with mode: managed, master, adhoc or monitor.
-
#delete_interface ⇒ String
Delete current interface, clean up.
-
#join_ibss ⇒ String
Set up or join a ibss network.
-
#request_info ⇒ Mash
Parse iw info command output and return as a mash.
-
#request_link ⇒ Mash
Parse iw link command output and return as a mash.
-
#validate_iw_properties ⇒ nil
Validate internal properties based on interface mode.
Methods included from Hostapd
#hostapd, #init_ap_conf_pid, #stop_hostapd
Methods included from ResourceProxyDSL
#call_hook, #hook_defined?, included
Methods included from Wpa
#init_wpa_conf_pid, #stop_wpa, #wpasup
Methods included from Ip
#configure_ip_addr, #flush_ip_addrs, #interface_up, #request_ip_addr, #request_mac_addr
Instance Method Details
#add_interface ⇒ String
Add interface to device
178 179 180 181 182 183 184 |
# File 'lib/omf_rc/util/iw.rb', line 178 work :add_interface do |device, type| c=CommandLine.new("iw", "phy :phy interface add :dev type :type") c.run( { :phy => device.property.phy, :dev => device.property.if_name, :type => type.to_s }) end |
#configure_mode(opts) ⇒ String
Configure the interface with mode: managed, master, adhoc or monitor
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/omf_rc/util/iw.rb', line 123 configure :mode do |device, opts| # capture opts hash and store internally device.property.update(opts) if device.property.phy && device.property.phy =~ /^%(\d+)%$/ wlan_phy_device = device.request_wlan_devices[$1.to_i] if wlan_phy_device device.property.phy = wlan_phy_device[:name] else raise ArgumentError, "Could not find your wifi device no #{$1.to_i}" end end device.validate_iw_properties device.delete_interface rescue logger.warn "Interface #{device.property.if_name} not found" # TODO should just remove all interfaces from physical device, at least make it optional case device.property.mode.to_sym when :master device.add_interface(:managed) device.hostapd when :managed device.add_interface(:managed) device.wpasup when :adhoc device.add_interface(:adhoc) device.interface_up device.join_ibss when :monitor device.add_interface(:monitor) device.interface_up end device.configure_ip_addr(device.property.ip_addr) if device.property.ip_addr end |
#delete_interface ⇒ String
Delete current interface, clean up
169 170 171 172 |
# File 'lib/omf_rc/util/iw.rb', line 169 work :delete_interface do |device| c=CommandLine.new("iw", "dev :dev del") c.run({ :dev => device.property.if_name }) end |
#join_ibss ⇒ String
Set up or join a ibss network
190 191 192 193 194 195 196 |
# File 'lib/omf_rc/util/iw.rb', line 190 work :join_ibss do |device| c=CommandLine.new("iw", "dev :device ibss join :essid :frequency") c.run( { :device => device.property.if_name.to_s, :essid => device.property.essid.to_s, :frequency => device.property.frequency.to_s }) end |
#request_info ⇒ Mash
Parse iw info command output and return as a mash
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/omf_rc/util/iw.rb', line 76 request :info do |device| known_properties = Mash.new command = CommandLine.new("iw", "dev :dev info") command.run({ :dev => device.property.if_name }).chomp.split("\n").drop(1).each do |v| v.match(/^\W*(.+) (.+)$/).tap do |m| m && known_properties[m[1].downcase.gsub(/\W+/, '_')] = m[2].gsub(/^\W+/, '') end end known_properties end |
#request_link ⇒ Mash
Parse iw link command output and return as a mash
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/omf_rc/util/iw.rb', line 53 request :link do |device| known_properties = Mash.new command = CommandLine.new("iw", "dev :dev link") command.run({ :dev => device.property.if_name }).chomp.gsub(/^\t/, '').split("\n").drop(1).each do |v| v.match(/^(.+):\W*(.+)$/).tap do |m| m && known_properties[m[1].downcase.gsub(/\W+/, '_')] = m[2].gsub(/^\W+/, '') end end known_properties end |
#validate_iw_properties ⇒ nil
Validate internal properties based on interface mode
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/omf_rc/util/iw.rb', line 204 work :validate_iw_properties do |device| raise ArgumentError, "Missing phyical device name" if device.property.phy.nil? unless %w(master managed adhoc monitor).include? device.property.mode raise ArgumentError, "Mode must be master, managed, adhoc, or monitor, got #{device.property.mode}" end case device.property.mode.to_sym when :master unless %w(a b g n).include? device.property.hw_mode raise ArgumentError, "Hardware mode must be a, b, g, or n, got #{device.property.hw_mode}" end %w(channel essid).each do |p| raise ArgumentError, "#{p} must not be nil" if device.property.send(p).nil? end when :managed %w(essid).each do |p| raise ArgumentError, "#{p} must not be nil" if device.property.send(p).nil? end when :adhoc %w(essid frequency).each do |p| raise ArgumentError, "#{p} must not be nil" if device.property.send(p).nil? end end end |