使用Getting essid via ioctl in ruby作为模板我想获得BSSID而不是ESSID。但是,不是C开发人员,有一些我不理解的事情。
到目前为止我所做的不工作:( ...
注意我有点困惑,因为根据wireless.h中的一些评论,我认为BSSID只能通过ioctl 设置。但是,存在获取的ioctl。这与我几乎完全缺乏对更中间的C类型主义(结构,联合和东西;))的理解,我根本就不知道。
def _get_bssid(interface)
# Copied from wireless.h
# supposing a 16 byte address and 32 byte buffer but I'm totally
# guessing here.
iwreq = [interface, '' * 48,0].pack('a*pI')
sock = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
# from wireless.h
# SIOCGIWAP 0x8B15 /* get access point MAC addresses */
sock.ioctl('0x8B15', iwreq) # always get an error: Can't convert string to Integer
puts iwreq.inspect
end
所以,与此同时,我正在使用wpa_cli方法来获取BSSID,但我更喜欢使用IOCTL:
def _wpa_status(interface)
wpa_data = nil
unless interface.nil?
# need to write a method to get the src_sock_path
# programmatically. Fortunately, for me
# this is going to be the correct sock path 99% of the time.
# Ideas to get programmatically would be:
# parse wpa_supplicant.conf
# check process table | grep wpa_suppl | parse arguments
src_sock_path = '/var/run/wpa_supplicant/' + interface
else
return nil
end
client_sock_path = '/var/run/hwinfo_wpa'
# open Domain socket
socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0)
begin
# bind client domain socket
socket.bind(Socket.pack_sockaddr_un(client_sock_path))
# connect to server with our client socket
socket.connect(Socket.pack_sockaddr_un(src_sock_path))
# send STATUS command
socket.send('STATUS', 0)
# receive 1024 bytes (totally arbitrary value)
# split lines by \n
# store in variable wpa_data.
wpa_data = socket.recv(1024)
rescue => e
$stderr.puts 'WARN: unable to gather wpa data: ' + e.inspect
end
# close or next time we attempt to read it will fail.
socket.close
begin
# remove the domain socket file for the client
File.unlink(client_sock_path)
rescue => e
$stderr.puts 'WARN: ' + e.inspect
end
unless wpa_data.nil?
@wifis = Hash[wpa_data.split(/\n/).map\
{|line|
# first, split into pairs delimited by '='
key,value = line.split('=')
# if key is camel-humped then put space in front
# of capped letter
if key =~ /[a-z][A-Z]/
key.gsub!(/([a-z])([A-Z])/,'\\1_\\2')
end
# if key is "id" then rename it.
key.eql?('id') && key = 'wpa_id'
# fix key so that it can be used as a table name
# by replacing spaces with underscores
key.gsub!(' ','_')
# lower case it.
key.downcase!
[key,value]
}]
end
end
编辑:
到目前为止,没有人能够回答这个问题。我认为我更喜欢wpa方法,因为我从中获取更多数据。也就是说,我想提出的一个问题是,如果有人使用wpa代码,请注意它需要升级的权限才能读取wlan
套接字。
EDIT ^ 2(完整代码段): 感谢@dasup我已经能够将我的类重新分配到正确使用系统ioctls拉出bssid和essids。 (YMMV给出了Linux发行版的实现,年龄和任何其他可能的不稳定因素 - 以下代码片段与3.2和3.7内核一起使用。)
require 'socket'
class Wpa
attr_accessor :essid, :bssid, :if
def initialize(interface)
@if = interface
puts 'essid: ' + _get_essid.inspect
puts 'bssid: ' + _get_bssid.inspect
end
def _get_essid
# Copied from wireless.h
iwreq = [@if, " " * 32, 32, 0 ].pack('a16pII')
sock = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
sock.ioctl(0x8B1B, iwreq)
@essid = iwreq.unpack('@16p').pop.strip
end
def _get_bssid
# Copied from wireless.h
# supposing a 16 byte address and 32 byte buffer but I'm totally
# guessing here.
iwreq = [@if, "\0" * 32].pack('a16a32')
sock = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
# from wireless.h
# SIOCGIWAP 0x8B15 /* get access point MAC addresses */
sock.ioctl(0x8B15, iwreq) # always get an error: Can't convert string to Integer
@bssid = iwreq.unpack('@18H2H2H2H2H2H2').join(':')
end
end
h = Wpa.new('wlan0')
答案 0 :(得分:1)
我对Ruby并不是很熟悉,但我发现了两个错误:
gdb
调试)。下面给出的初始化有效。请注意,如果任何数据结构或常量发生更改,您的代码将会中断(IFNAMSIZ,sa_family,struct sockaddr等)。但是,我认为不会很快发生此类更改。
require 'socket'
def _get_bssid(interface)
# Copied from wireless.h
# supposing a 16 byte address and 32 byte buffer but I'm totally
# guessing here.
iwreq = [interface, "\0" * 32].pack('a16a32')
sock = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
# from wireless.h
# SIOCGIWAP 0x8B15 /* get access point MAC addresses */
sock.ioctl(0x8B15, iwreq) # always get an error: Can't convert string to Integer
puts iwreq.inspect
end
你将获得一个数组/缓冲区:
0x00
个字节,总长度为16个字节。struct sockaddr
,即一个双字节标识符0x01 0x00
(来自ARPHRD_ETHER
?),后跟填充0x00
字节的BSSID,总长度为14个字节。