23.2.09

IP Renumbering w/PHP and Compilers.

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.

iprenum.php

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.

4 comments:

  1. Hey there Jody. Just caught you on Hac5, thought I'd drop by and say good work on the show. I wasn't aware there were compilers for turning PHP into .exe. I haven't worked in php since college, been on .NET since entering the working world...

    -Corby-
    http://www.nerdnology.com

    ReplyDelete
  2. Thanks, was weird being in front of a camera again. Haven't done it since high school. Really need to remember to look at the camera more.

    I didn't know you could compile PHP either, until Darren told me he wanted to use my script in a segment about PHP compilers. There's another one, that I didn't know about at the time that I did the segment, called ZZEE PHPExe. I've never used it, but someone on the Revision3 forums recommended it as an alternative.

    ReplyDelete
  3. Jody, very nice work indeed. You have producded an inspiring work. i watched hack5 episode other day and i must say that you are a genius. i would like to compile this for my organisation. one disadvantage, i dont know anything about PHP, or the usage of roadsend php program. it's something i would love to learn but i have a little time. Could you provide me some links that you used to achieve this task... because i dont want to ask you all the n00b questions! i would like to invistage first and than ask questions if i dont understand something (that's if you wish to provide me the help that i need). thanks

    ReplyDelete
  4. nEmo, all the links are actually in the post except the site I used as a reference to learn PHP. For that I went to www.w3schools.com , though if you just want to compile what I did without changing it I would just use bambalam, which a link to the website is in the post above.

    ReplyDelete