Mobile Wi-Fi Power Tools

As a digital nomad I find myself trying to work among a plethora of WiFi arrangements and issues. One of the most common frustrations is the act of connecting/reconnecting to a network, and had I taken the time a year ago to develop the following scripts, I would have more than made up for the spent time. Nonetheless, the inspiration and know-how only came after a time of brushing up my BASH-fu while reading man bash.

These snippets should be saved in ~/.bash_profile or ~/.bashrc depending on your setup.

Problem 1: Randomize MAC address

Although programs exist to do this for you, none are required. I’ve been manually running it from the terminal and making up the numbers on the fly, but a more automated and more random solution is better.

# randomize MAC address
function macr () {
	local CMD="sudo ifconfig en0 ether "
	for i in `seq 1 12`; do
		C=`echo "obase=16; $(((RANDOM % 15)+1))" | bc`
		CMD="$CMD$C"
		if [ $((i % 2)) == 0 ] && [ $i != 12 ]; then
			CMD="$CMD:"
		fi
	done
	eval $CMD
}

Running macr from the command line will immediately prompt your for your password (if not already cached) and change the MAC, meaning it might kick you off your authenticated session.

Many thanks to Jonathan Zdziarski for his original script which I modified.

Problem 2: Swap DNS servers

Many places that offer free Wi-Fi have a login screen with a notice, maybe a button to accept the terms of service, and maybe a login screen. If you are like me and tend to use Google’s DNS servers or any custom DNS, then you run into the problem that you will never see this login screen and thus won’t ever authenticate to the wider Internet. It’s a hassle to open the network settings and manually remove the DNS servers, click login, then repeat and add the custom ones again. This command will allow you to check on the DNS settings from the command line and swap out your custom servers of choice.

# swap DNS servers
function ldns () {
	if [ "on" = $1 ]; then
		sudo networksetup -setdnsservers Wi-Fi 8.8.8.8 8.8.4.4
	elif [ "off" = $1 ]; then
		sudo networksetup -setdnsservers Wi-Fi Empty
	elif [ "get" = $1 ]; then
		local NETWORK_SETUP=$(sudo networksetup -getdnsservers Wi-Fi)
		if ! [[ $NETWORK_SETUP =~ \b(?:\d{1,3}\.){3}\d{1,3}\b ]]; then
			NETWORK_SETUP=''
		fi
		local RESOLVE_CONF=$(cat /etc/resolv.conf | grep nameserver | cut -d' ' -f2,2)
		local SCUTIL=$(scutil --dns | grep nameserver | cut -d':' -f2,2)
		echo "$(echo $NETWORK_SETUP $RESOLVE_CONF $SCUTIL | tr ' ' '\n' | sort -u)"
	fi
}

You can turn on “local DNS settings” with ldns on and revert back to the router-given DNS settings with ldns off. Verification comes with ldns get. When I connect to a network, I can now follow this quick and easy sequence.

ldns off
# connect to WiFi network, login or accept terms
ldns on

That simple!

1 thought on “Mobile Wi-Fi Power Tools

  1. Reblogged this on Snell Family Adventures and commented:

    Traveling with a Mac? Maybe you will find these commands handy when trying to manage the jungle of public WiFi.

Leave a Reply

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close