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.

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

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.

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

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.

<br />
ldns off<br />
# connect to WiFi network, login or accept terms<br />
ldns on<br />

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