Mac – Set your static IP address fast!

A quick update on the last post – but for OS X!

The OS X terminal is more comfortable than the Windows CLI for route/switch guys like me. Here’s how I make quick IP changes in OS X.

You can create command aliases just like in Cisco IOS by adding them to the profile that terminal loads when you open the app. Just open terminal, and type “open .profile”. Here I’ve done just that, and you can see my terminal profile:

profile Sublime Text, Today at 10.21.49 AM

If you’ve never messed with the terminal profile, it will likely be empty. But the basic syntax is straightforward. Save the file and close/reopen terminal for the aliases to become available.

Here are a couple of my examples:

  • “setair” will statically set my Wi-Fi adapter address
  • “setethdhcp” will revert my ethernet NIC to DHCP
  • “shair” is like ipconfig in Windows, or a simpler version of ifconfig for OS X:

shair— -bash — 139×39 Terminal, Today at 10.26.43 AM.png

Note that any commands running SUDO will prompt you for your elevated password.

Set your static IP address fast!

Hello! Sorry for the radio silence – I have been a little sidetracked from welcoming my second daughter into the world on March 21st! Lack of sleep and going back to work this week are my excuses for a little blogging break – I’m not sure my ramblings would have made much sense anyhow…

But I was inspired by this tweet today:

John here wrote a really cool post about some of the ways he uses the netsh command in the Windows CLI. I knew about the “netsh WLAN show …” commands already, but John has demonstrated a neat way to monitor roaming with a simple loop. It’s elegant because it’s simple and works.

This got me thinking about a little trick I’ve been using for years now with netsh to avoid a certain pain in the arse I would otherwise be dealing with in Windows. Maybe I can help someone with my trick like John helped me with his.

I’m a CLI guy – if you’re a Cisco Route/Switch person, you know why. Heck, I try to avoid taking my hands off of the keyboard as much as possible.

If there is one thing I do a lot in Windows, it’s set static IP addresses on my NICs. And then set them back to DHCP. Sometimes several times a day.

Worse yet, when I need to bootstrap a router/switch/controller/network voodoo box, 9 times out of 10, I’m using wifi to remain internet connected while using my wired NIC to connect to the new unit – a lot of which provide DHCP out of the box. The problem with this is that my Windows wired NIC is preferred and I’m getting a default gateway from the DHCP server on the box I’m bootstrapping, even though that’s a dead end. There goes my internet until I set a static IP without a gateway on my wired NIC.

The default work flow to change the IP settings on your NIC is terrible (Windows 8.1 example):

Start > Control Panel > Network and Sharing Centre > Change Adapter Settings > Right-click adapter > Properties > TCP/IPv4 > Properties: Set your IP/Mask/Gateway if required > OK.

And don’t forget the three windows you now have to close. Then do it all over again to set back to DHCP. Sure, there are some shortcuts you can use to this process, but I still have nightmares about this. Enough complaining – here’s what I do.

First, I renamed my NICs to “wired” and “wireless” in the “network connections” window (after “Change Adapter Settings” in the work flow above). Just right click and rename, it won’t hurt anything and makes it so much easier to read CLI output.

Then I write myself a little script in notepad:

@ECHO off
set /p ipadd=Enter address and mask, press enter:
netsh interf ip set add wireless static %ipadd%
timeout /t 5
goto end

Then I save it as a batch file called “Wireless Static.bat”. But wait, there’s more…

I right-click that sucker and choose “create shortcut”, which creates a “Wireless Static.bat – shortcut” file in the same folder. I rename it to “Wireless Static”, then right-click again and go to the file properties – here’s the magic:

CMD Batch file

The “Target” field will have the full path to the file. I prepend the path to the cmd.exe executable like so:

C:\Windows\System32\cmd.exe /c “full path to script”, then hit “Ok”. Now I have two files, the original batch file, and a shortcut with the CMD icon:

IP bat and shortcut

That CMD shortcut right there can be pinned to the Start page or Taskbar – the batch file can’t.

Still with me? Here’s the result! I pin mine to the Start page, so I hit the Windows key, and click the shortcut I want.

Start Page.png

Here’s the prompt, after I slap in my new static IP. The format is IP address, Mask, Gateway (if required), with a space between each.

Enter Address

I press enter, the script runs the netsh command, and counts down for 5 seconds before running an ipconfig to verify the result (I was remote desktop-ed to my Windows notebook when I took the screenshots, so I erased the wireless NIC addressing output to avoid confusion).

Static Wait.png

Finally, the “pause” parameter in the batch file leaves the output there for me to look at until I’m ready to dismiss it with a tap of the “Enter” key.

Three buttons, one click, and typing the addressing. YES.

You can do another script for returning to DHCP. I’ve got separate shortcuts for static and DHCP for each NIC, but you can modify the batch file to make the user type the NIC name and put it into the netsh command as well so you can use one shortcut for any NIC, if you prefer (this is where it helps to rename the NICs).

Here attached are my basic batch files for anyone who wants to try them out. PDF because wordpress won’t allow me to attach text files.

IP Static


Wireless Static

Wireless DHCP