Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get Mac Address of user's when creating new record?

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?

like image 691
cat Avatar asked Dec 11 '22 19:12

cat


2 Answers

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.

like image 92
j_mcnally Avatar answered May 19 '23 12:05

j_mcnally


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
like image 44
theIV Avatar answered May 19 '23 10:05

theIV