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?
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.
Checked on amazon linux and the formatting seems the same.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With