在Ruby中通过ioctl获取Essid [英] Getting essid via ioctl in ruby

查看:89
本文介绍了在Ruby中通过ioctl获取Essid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为避免依赖无线工具,我想直接使用ioctl从设备获取essid,在C语言中这不是问题,但在Ruby中则完全不同.

To avoid relying on the wireless tools I want to get the essid directly from the device with ioctl, in C this wouldn't be a problem, but in Ruby it's quite different.

问题出在 wireless.h 的结构上,该结构用作ioctl的输入/回复:

The problem is following struct from wireless.h that is used as input/reply of ioctl:

struct  iw_point
{
  void __user *pointer; /* Pointer to the data  (in user space) */
  __u16   length;   /* number of fields or size in bytes */
  __u16   flags;    /* Optional params */
};

指针部分必须是存储区的有效地址,其后是字节长度,然后是标志字段.我尝试使用Array#pack和位结构gem,但尚未找到解决方案.

The pointer part must be a valid address of a memory area, followed by the length in bytes, followed by a flag field. I tried with Array#pack and the bit-struct gem, but haven't found a solution yet.

是否有办法绕过此内存指针问题?

Is there a way to bypass this memory pointer problem?

推荐答案

我终于开始工作了,解决方案是以这种方式使用pack的'p'参数:

I finally got it working, the solution was to use the 'p' argument of pack in this way:

require "socket"

# Copied from wireless.h
SIOCGIWESSID      = 0x8B1B
IW_ESSID_MAX_SIZE = 32

iwreq = [ "wlan0", " " * IW_ESSID_MAX_SIZE, IW_ESSID_MAX_SIZE, 0 ].pack("a16pII")

sock = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)

sock.ioctl(SIOCGIWESSID, iwreq)

interface, essid, len, flags = iwreq.unpack("a16pII")

puts essid

这篇关于在Ruby中通过ioctl获取Essid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆