1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rubyでI/FのMACアドレスを取得する

Posted at
require 'socket'

# バイナリString(6octet)で返す
def if_hwaddr(if_name)
  sockaddr = Socket.getifaddrs
                   .select{|a| a.name == if_name.to_s}
                   .map(&:addr)
                   .find{|a| a.pfamily == Socket::PF_PACKET}
                   .to_sockaddr
  sockaddr[-6..-1]
end

# 文字列(16進、":"なし)で返す
def if_hwaddr_str(if_name)
  if_hwaddr(if_name).unpack('H*')[0]
end
if_hwaddr_str('eth0') #=> "382e4eb312cd"
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?