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

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"]

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

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.

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)

Shell command for getting mac address in OS X

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}'

How to delete a password from keychain with a shell command?

I wanted to make a shell script that connects to a hidden wireless network on my macbook pro by creating a network. However, I only want to connect to that network when I want to and to prevent airport from connecting automatically I'll have to delete the network's password from keychain.
So far I have:
networksetup -setairportnetwork en1 $ssid $networkpassword
networksetup -removepreferredwirelessnetwork en1 $ssid
Im also aware that in OSX 10.7 the security command can modify the keychain.
Anyway I would appreciate any help someone can give me. I'm also willing to use applescript or something else if it works. Thank you.
I figured it out. The command I used is:
sudo security delete-generic-password -l $ssid "/Library/Keychains/System.keychain"
sudo was needed because this keychain is located in the systems library. security also contains a lot more delete and useful commands and you can specify how you want to indicate the location in this command.
edit: I was having some trouble using this command and went back and learned I could use another command to not remember the network at all which I would have used before if I had found it. With this option you can do a wealth of things with your adapter along with the networksetup option.
Here is my final script which first runs as root. then it changes the system preferences to not remember new networks. Then it disconnects from any current networks. Then it adds the new network. Lastly, it makes the system remember new wifi networks.
sudo -i
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport prefs RememberRecentNetworks=NO
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -Z
networksetup -setairportnetwork en1 $ssid $password
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport prefs RememberRecentNetworks=YES

Resources