Shell command for getting mac address in OS X - macos

I have been handicapped by the GUI and always seem to ask of help when it comes to the command line.
On Mac OS X only I need a command line to get the mac address of the wifi currently in use.
Help!

ifconfig en1 gets the interface details for wifi, the mac is on a line starting with ether, and is the second word on that line so:
ifconfig en1 | awk '/ether/{print $2}'

I think the best and easiest way to get the information is using this command:
networksetup -listallhardwareports
It will return a nice list of devices like this:
Hardware Port: USB 10/100/1000 LAN
Device: en6
Ethernet Address: 00:e0:4c:...
Hardware Port: Wi-Fi
Device: en0
Ethernet Address: 80:e6:50:...
Hardware Port: Bluetooth PAN
Device: en3
Ethernet Address: 80:e6:50:...
Hardware Port: Thunderbolt 1
Device: en1
Ethernet Address: 72:00:05:...
Hardware Port: Thunderbolt 2
Device: en2
Ethernet Address: 72:00:05:...
Hardware Port: Thunderbolt Bridge
Device: bridge0
Ethernet Address: 72:00:05:...
VLAN Configurations
===================

networksetup -getmacaddress <interface>

Wifi mac address is normally can be found in en0. So you may try this command on Terminal
ifconfig en0 | awk '/ether/{print $2}'

ifconfig should do the trick, it'll display a bunch of info including your MAC address. Alternatively it'll be in your network settings under system preferences.
EDIT
On a computer with just a wireless connection, en0 will have your wifi settings. The tag labeled with ether will most likely be your MAC address.
If you have both a wired and wireless connection, it'll be under ether in the en1 tag
Source: http://m.iclarified.com/entry/index.php?enid=30929

This will easily give you the specific Mac Address for your Wifi Interface
networksetup -listallhardwareports | grep Wi-Fi -A 3 | awk '/Ethernet Address:/{print $3}'

Related

MACOS - How to connect to a 802.1x wifi without entering the password from terminal. Password can be pre-configured with a wifi-profile

in Windows i'm using as below.
netsh wlan connect ssid=YOUR-WIFI-SSID name=PROFILE-NAME
Need similar for the mac os.
I think it will depend on whether you have saved the WiFi password in your keychain but the basic command is networksetup as follows:
networksetup -setairportnetwork en0 YOUR_NETWORD_SSID
For more information, try:
man networksetup

How to read data from serial-to-usb device?

I have a sensor device connected to my Ubuntu 20 machine using a serial to usb cable. I understand that to stream data from it I need to run the following commands:
Enter root with sudo -I.
Setup a virtual COM port with stty -F /dev/ttyACM0 9600 cs8 -cstopb -parenb.
Send a command to the device to initiate data streaming with echo -e "some_string" > /dev/ttyACM0.
Stream data with cat < /dev/ttyACM0.
However, I cannot be sure that my device is connected as /dev/ttyACM0. How can I set this up? Currently it has the path /dev/bus/usb/001/005, although the last number changes every time the device is plugged in.
In response to the comment below, here is the output I get when I disconnect the device and run dmesg | tail (I've edited some real values):
[89092.291151] usb 1-1: USB disconnect, device number 11
[89100.310893] usb 1-1: new full-speed USB device number 12 using xhci_hcd
[89100.467562] usb 1-1: New USB device found, idVendor=SOME_ID, idProduct=SOME_PRODUCT, bcdDevice=SOME_BCD_DEVICE
[89100.467565] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[89100.467567] usb 1-1: Product: SOME_PRODUCT
[89100.467569] usb 1-1: Manufacturer: SOME_MANUFACTURER
EDIT: I have just found that I can use sudo modprobe usbserial vendor=SOME_ID product=SOME_PRODUCT to make ttyUSB0 available. However, plugging this into the above commands and running screen /dev/ttyUSB0 only returns lots of ``` characters. Note that this stops when the device is unplugged.
I managed to get this working. The commands were:
sudo -i
rmmod ftdi_sio
rmmod usbserial
modprobe ftdi-sio
echo -n VENDOR_ID PRODUCT_ID | sudo tee /sys/bus/usb-serial/drivers/ftdi_sio/new_id
stty -F /dev/ttyUSB0 9600 cs8 -cstopb -parenb
echo -e "uud1\r" > /dev/ttyUSB0
screen /dev/ttyUSB0 9600

macOS set DNS server to default value

I'm building a tool to manage DNS in macOS.
I know how to set dns server in terminal.
networksetup -setdnsservers Wi-Fi 1.1.1.1
but I don't to know how to unset it, or set it to the default value
networksetup -setdnsservers Wi-Fi
seems not working.
Anyone help ?
Give a try to:
networksetup -setdnsservers Wi-Fi empty
Then check with:
scutil --dns
From man networksetup:
-setdnsservers networkservice dns1 [dns2] [...]
...
If you want to clear all DNS entries for the specified network service,
type "empty" in place of the DNS server names.

Mac OS terminal command How to differentiate wlan from ethernet address?

I'm making a mac os app. I'm trying to get the ethernet and wlan addresses. I know these are en0 and en1 addresses but depending on devices, en0 can be the ethernet or the wlan one. Is there a way to know which one it is?
So far I'm using this which gets me both addresses but don't differentiate them:
let task=Process.init()
task.launchPath="/sbin/ifconfig"
task.arguments=["en0"] //or en1
let pipe=Pipe()
task.standardOutput=pipe
task.launch()
let data=pipe.fileHandleForReading.readDataToEndOfFile()
guard let stringResult=String(data: data, encoding: String.Encoding.utf8) as NSString? else{wlanFailed();return}
print("en0:", stringResult)
EDIT
So now I'm trying to run this command networksetup -listnetworkserviceorder which works from my terminal.
But I don't know how to make it work from my Mac app. For example, with this:
let task=Process.init()
task.launchPath="/sbin/networksetup"
task.arguments=["-listnetworkserviceorder"]
I get:
launch path not accessible
These are some commands that help to match the interface with the hardware name:
networksetup -listallhardwareports
networksetup -listnetworkserviceorder
system_profiler SPNetworkDataType
scutil <<< "list" | grep -i airport
Thanks #artem-dorodovskiy and #nbari for giving me the command line.
I found how to write it thanks to this SOF answer.
let task=Process.init()
task.launchPath="/usr/bin/env"
task.arguments=["networksetup", "-listnetworkserviceorder"]

BASH on OS X: Why does this script not work?

I wrote the following BASH script to change my MAC address back to the normal one after I am done using a spoofed MAC address:
sudo ifconfig en1 ether 00:11:22:33:44:55
It works. However, the following code doesn't work:
mac=$(cat /volumes/KYLE-OSX/mac.txt)
sudo ifconfig en1 ether $mac
I am given the following error message:
ifconfig: can't set link-level netmask or broadcast
I am unable to predict what the MAC address is going to be, so I need to be able to use any possible MAC address in the 'sudo ifconfig en1 ether' statement.
The fundamental problem was diagnosed in comments already -- the file contains more than just the MAC address. Here's a simple workaround for that.
mac=$(grep -Eo '\<[0-9a-f]{2}(:[0-9a-f]{2}){5}\>' /Volumes/KYLE-OSX/mac.txt)

Resources