In episode 424 of Hak5 a viewer question led to Darren and Matt discussing renumbering a whole subnet of Windows machines using the netsh command in a script, but how would you specify an IP for each machine with only one script? PHP to the rescue, because it's not just for web pages anymore.
On this week's episode of Hak5 I show off a script to do just that, as well as two different PHP runtime compilers.
Since most Windows workstations don't have PHP installed a compiler will let you prepare your code to run on systems that don't have PHP installed.
First we have Roadsend PHP, which is available for Linux, FreeBSD, Mac OS X and Microsoft Windows. It's released under GNU GPL, and it's runtime libraries are GNU LGPL so compiled programs may be used for both open source and commercial projects.
Roadsend PHP is not just to package up your PHP into nice friendly bundles, it comes with Roadsend Studio, a full development environment (IDE) with support for the Glade interface builder (*nix/Win32), to give your PHP a GUI front end.
It supports PHP 4 and 5, and so far all the code I've compiled with it runs just as it would if launched from the command line using the php command. The only drawback to it is the compile process seems to take a while even on relatively small projects, and the file sizes are a little large (simple scripts weighing in at over 3MB); but if you want to stay in one environment from start to finish Roadsend will do the job.
Second is the Bambalam PHP EXE Compiler/Embedder, which as the name implies is for Windows only. Like Roadsend PHP, Bambalam PHP is free to use as it's released under the PHP license, generates code that will run without a full PHP install, and with the use of the WindBinder library can produce programs with GUI front ends. That is about where the similarities end.
Bambalam is small, consisting of a hand full of files, and is only for the actual building of the executable code. Bring your own editor, debugger, GUI builder, and project manager. That's not what Bambalam is for. What it is for, though, is producing small, fast programs out of any PHP that will run under PHP 4.4.4. The same +3MB code that Roadsend produced came in at just over 1MB with Bambalam, and under 700KB with compression turned on.
The problem my code solves is how to write one script to renumber a whole group of machines without having to issue a different version of the script to each machine. As this is more of a proof of concept we will assume that only the last octet of the IP address will be changing.
The command is issued with the following options:
<New IP> <Subnet Mask> <Default Gateway> [DNS] [WINS]
The new IP is given as the first three octets in xxx.xxx.xxx format, subnet and gateway will be a full four octets a peice. IP, DNS, and WINS can each be assigned as DHCP (using DHCP for IP preclueds the need for subnet and gateway). DNS and WINS can also be assigned as NONE so long as IP is not DHCP. Furthermore specifying WINS requires that some value be given for DNS.
If a new first three octets are given without specifying DNS or WINS and those values were already staticly assigned then the new first three octets will be used for those values as well. Also if IP is currently assigned via DHCP that can't not be changed at this time.
The following code is my first attempt at PHP, and before writing this I had never even seen PHP code before. While comments and advice are welcome, keep that in mind.
This code is released under the BSD license (two clause), so feel free to use it as you see fit, just don't complain to me when it plots with your cat to take over the world.
Don't ask me how to deploy this script across a Windows network without physically accessing every machine, I don't do Windows. I have a feeling Darren has some thoughts on it, though.