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

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)

Related

MAC address randomizer - MAC Address does not change?

I've tried to create a small and basic bash script to change my MAC address on MacOS, upon reboot. The script trows no errors, however nor does it change my mac address.
The script:
#!/bin/bash
sudo echo "Welcome"
preAddress="$(ifconfig en0|grep ether)"
numZero=2
numOne=$(( ( RANDOM % 9 ) + 0 ))
numTwo="$(openssl rand -hex 1)"
numThree="$(openssl rand -hex 1)"
numFour="$(openssl rand -hex 1)"
numFive="$(openssl rand -hex 1)"
numSix="$(openssl rand -hex 1)"
newAddress="$numZero$numOne:$numTwo:$numThree:$numFour:$numFive:$numSix"
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport --disassociate
sudo ifconfig en0 ether $newAddress
networksetup -detectnewhardware
sudo ifconfig en0 up
echo "MAC Address changed to: $newAddress from$preAddress"
I've re-used some code, originally posted here: Post 1 user: seren/user137369, Post 2 User: Luke Exton, Post 3 User: OrangeTux/py4on.
In short, the script creates a set of variables which all contains a random number. These variables are formatted as a MAC Address in another variable. I then change (or at least try to) the current MAC address to the random one.
But when I run this, I see no change under Network>Advanced in system settings or if I run "ifconfig" in the terminal?
I've also tried to just copy in the following to the terminal, but no dice..:
sudo ifconfig en0 down
sudo ifconfig en0 ether 27:ab:29:b9:be:ef
sudo ifconfig en0 up
I simply cannot figure out why this does not work.. Any help is very appreciated!

macOS terminal run Reverse DNS on LAN devices to get hostnames

I made some research and can't find a solution, I'm starting to think this is not possible.
I'm running arp -a in my terminal and I'd like to get the hostnames of the LAN devices.
host x.x.x.x returns:
Host x.x.x.x.in-addr.arpa. not found: 3(NXDOMAIN)
nslookup x.x.x.x returns:
** server can't find x.x.x.x.in-addr.arpa: NXDOMAIN
Is there a way to do this?
arp -a does the reverse DNS by default on linux machines. On macOS I guess you will have to run nslookup on each entry returned by arp -a table.
I found a way thanks to #gordon-davisson who set me on the right path.
First I get the gateway IP with: route get default | grep gateway
Then for every LAN device I run: host LAN_IP Gateway_IP
This returns:
Using domain server:
Name: 192.168.x.x
Address: 192.168.x.x#x
Aliases:
x.x.168.192.in-addr.arpa domain name pointer LAN-host-name.
I parse the response to get the name displayed at the end.
Side note: it still doesn't display the LAN host-names with arp -a after that.

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

How to map ip:port to a new ip or a domain in mac

I am using macOS 10.12 and I want to do ip:port mapping
ex. 127.0.0.1:32769 to 10.0.0.1
then I can add 10.0.0.1 somedomain.com to my /etc/hosts
I did some search, and got solutions to this question on this post:
https://serverfault.com/questions/102416/iptables-equivalent-for-mac-os-x/673551#673551
but the command in this post works for only the newest one.
every time I use this command the system replies me:
$ sudo ifconfig lo0 10.0.0.2 alias
$ echo "rdr pass on lo0 inet proto tcp from any to 10.0.0.2 port 80 -> 127.0.0.1 port 32771" | sudo pfctl -ef -
pfctl: Use of -f option, could result in flushing of rules
present in the main ruleset added by the system at startup.
See /etc/pf.conf for further details.
No ALTQ support in kernel
ALTQ related functions disabled
pfctl: pf already enabled
how can I prevent flushing rules?
or is there any ways to get this work easier?
Thanks a lot

How do I find the DHCP assigned IP address via bash on Fedora?

Is there a command within the bash shell of fedora that will give me the currently assigned IP address?
ifconfig is what you're looking for
Try the following command:
ipconfig getifaddr eth0

Resources