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:
set /p ipadd=Enter address and mask, press enter:
netsh interf ip set add wireless static %ipadd%
timeout /t 5
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:
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:
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.
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.
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).
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.