mac_address
mac_address copied to clipboard
Incorrect MAC address
Hi all,
On my machine (2019 Mac Book Pro running Mojave) the following code:
extern crate mac_address;
use mac_address::get_mac_address;
fn main() {
match get_mac_address() {
Ok(Some(ma)) => {
println!("MAC addr = {}", ma);
println!("bytes = {:?}", ma.bytes());
}
Ok(None) => println!("No MAC address found."),
Err(e) => println!("{:?}", e),
}
}
retrieves an incorrect MAC address. Any idea why? ~
unfortunately I don't have access to that kind of machine, so I'll need more context. what is the "incorrect MAC address" and why do you believe it to be incorrect?
Let me elabatore. I get the loopback address 00-00-00-00-00-00 which is uninteresting (not incorrect, but uninteresting). People are generally interested in the network adapter (en0) mac address
I am currently writing a small rust library to generate a serial number linked to hardware-specific identifiers such as mac address. On my code, this is how I get the MAC to address, in case you might find it helpful:
#[cfg(target_os = "macos")]
pub fn get() {
// On mac, you can get all the mac addresses by running /sbin/ifconfig | grep ether
let output = Command::new("/sbin/ifconfig")
.args(["en0"])
.output()
.expect("Failed to retrieve hardware information");
assert!(output.status.success());
let tmp = String::from_utf8(output.stdout)
.expect("Found invalid UTF-8");
match tmp
.lines()
.find(|l| l.contains("ether"))// find the line containing the UUID
.unwrap_or("")
.split(' ')
.nth(1)
{
None => panic!("No network interface found"),
Some(id) => {
println!("The MAC address is {}", id);
// return id.to_string();
}
}
}