Is it possible to get the MAC address of a user that posts a new record?
If I had column called mac_address
, how can I write my controller to put the MAC address into that column?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
A user's mac address is not part of the web request.
I know it's not your question, but you can get their IP address using the request object:
request.ip
In your create
action, you could have something like the following (assuming you have a column ip_address
):
def create
@item = Item.new(params[:item])
@item.ip_address = request.ip
if @item.save
...
end
end
回答2:
You can take the request.ip
and call a ping on it. This should put the ip in your machine's arp cache. By dumping the arp cache you can match the MAC Address.
`ping -c 1 #{request.ip}`
sleep(10) # For dramatic effect
arptable = `arp -a`
entries = arptable.split("\n")
ipmap = {}
entries.each do |e|
ent = e.split(" ")
ipmap["#{ent[1].gsub(/\(|\)/, "")}"] = ent[3]
end
puts ipmap["#{request.ip}"]
This code is tested on osx mavericks, tools and formatting may vary on linux/bsd/solaris but should be the same approach, the other caveat is that it will only work on your local intranet / network segment.
Update
Checked on amazon linux and the formatting seems the same.