iprenum.php

<?php

// iprenum.php - Windows network settings renumbering

// ver 0.0.4

// Jody K Delauney

// jodydelauney@elder-n00b.org

// https://www.elder-n00b.org

// 2009/02/17

//

// Copyright (c) 2009, Jody K Delauney

// All rights reserved.

//

// Redistribution and use in source and binary forms, with or without

// modification, are permitted provided that the following conditions are met:

//

// * Redistributions of source code must retain the above copyright notice,

// this list of conditions and the following disclaimer.

// * Redistributions in binary form must reproduce the above copyright notice,

// this list of conditions and the following disclaimer in the documentation

// and/or other materials provided with the distribution.

//

// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"

// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE

// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE

// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR

// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF

// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN

// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)

// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE

// POSSIBILITY OF SUCH DAMAGE.

//

// Take partial IP address, full subnet mask, default gateway, and optionally DNS and WINS servers

// and change the computers network settings accordingly. If DHCP is used for IP and DNS or WINS

// is not specified, then DHCP is assumed for the unspecified arguments.

//

// This version assumes that only the last octet of the client IP will stay the same. Future versions

// will allow 1 through 3 octets to be entered and the difference will be computed on the fly. No

// attempt will ever be made to assume subnet masks or gateways.

//

// If static DNS or WINS servers are already present, and those entries are not provided, the script

// will assume that the network range for those servers is also changing and update them accordingly.

// Future versions may offer switches to over ride this behavior.

//

// This script also assumes only one active adaptor, no plans to support multi adaptor clients at

// this time. If you want to target a specific adaptor, change the reference to the $adaptor_name.

//

// No sanity checking is done against values, netsh should just fail if insane values are used.

//

// New in version 0.0.1

// - It exists. What more do you want?

//

// New in version 0.0.2

// - The DHCP argument can be accepted in mixed case.

//

// New In version 0.0.3

// - Both DNS and WINS can be given the NONE argument to disable that option.

// - If DNS or WINS are not specified, DHCP is not specified as the first option, and either were NONE,

// they'll stay that way

// - If DHCP is given as first option, DNS and WINS can follow directly without need to pad out netmask

// and gateway options.

//

// New in version 0.0.4

// - Some sanity checking is done on DNS and WINS arguments.

// - DNS and WINS settings starting as NONE staying as NONE actually works now.

// Define a few things before we get going.


$adaptor_name = "Local Area Connection"; // Change this to affect a different adaptor, might be an option later.

$dns_serv = "NO";

$new_dns = "NO";

$dns_arg = 4;

$wins_serv = "NO";

$new_wins = "NO";

$wins_arg = 5;

// Displays usage information.

function usage($appname)

{

echo "\nUsage - $appname <Partial IP> <Subnet Mask> <Default Gateway> [DNS] [WINS]";

echo "\n $appname DHCP [DNS] [WINS]";

echo "\n";

echo "\nDNS and WINS can be specified by their IP address, DHCP, or NONE";

echo "\nIf IP is set by DHCP and DNS or WINS is not specified, then DHCP is assumed.\n\n";

}

// Make sure we got enough arguments, but not too many.

// This will be replaced with proper parsing later.

if ($argc == 1) // Did you forget what to do?

{

usage($argv[0]);

die();

}

elseif ($argc < 5 && strtolower($argv[1])=="dhcp") // If first argument is dhcp we don't need anything else.

{

$dns_arg = 2;

$wins_arg = 3;

}

elseif ($argc < 4) // Not enough arguments to complete the renumber without assuming too much.

{

echo "\nNot enough arguments.\n\n";

usage($argv[0]);

die();

}

elseif ($argc > 6) // Too many arguments.

{

echo "\nToo many arguments.\n\n";

usage($argv[0]);

die();

}

echo "\nReading current network configuration.\n";

system("netsh int ip show conf name=\"$adaptor_name\"> ip_config.txt"); // Get current config

$ip_config = file('ip_config.txt'); // Read data into array for processing

foreach ($ip_config as $line_to_process) // Start finding out what we have

{

$config_line = trim(strrev(strstr(strrev($line_to_process),":"))); // What is the values name?

$config_param = trim(substr(trim($line_to_process),strlen($config_line))); // What is the value?

switch ($config_line)

{

case "DHCP enabled:": // See if DHCP is currently in use

$dhcp_status = $config_param;

echo "DHCP enabled: " . $dhcp_status . "\n";

break;

case "IP Address:": // Acquire current IP

echo "Current IP: " . $config_param . "\n";

$ip_address = substr($config_param,strlen(strstr(strrev($config_param),"."))); // Strip IP down to last octet

break;

case "Statically Configured DNS Servers:": // Get static DNS if assigned

$dns_serv = substr($config_param,strlen(strstr(strrev($config_param),"."))); // Strip IP down to last octet

echo "Statically Configured DNS Server: " . $config_param . "\n";

break;

case "Statically Configured WINS Servers:": // Get static WINS if assigned

$wins_serv = substr($config_param,strlen(strstr(strrev($config_param),"."))); // Strip IP down to last octet

echo "Statically Configured WINS Server: " . $config_param . "\n";

break;

default: // All other lines are ignored, so no need to case them.

echo "";

}

}

echo "Configuration processing done.\nParsing command line arguments\n";

if (strtolower($dhcp_status) == "no" && strtolower($argv[1]) != "dhcp") // Work out what new IP should be.

{

$new_ip = "static " .$argv[1] . "." . "$ip_address";

}

elseif (strtolower($dhcp_status) == "yes" && strtolower($argv[1]) == "dhcp") // DHCP is already set for IP settings, nothing to do.

{

$new_ip = "skip";

}

else // Only thing left should be that we're setting to DHCP, so just do it.

{

$new_ip = "dhcp";

}

if ($new_ip != "dhcp" && $new_ip != "skip") // Prepare for new subnet

{

$new_subnet = $argv[2];

}

else

{

$new_subnet = "";

}

if ($new_ip != "dhcp" && $new_ip != "skip") // Prepare for new gateway

{

$new_gateway = "$argv[3] 0";

}

else

{

$new_gateway = "";

}

if ($new_ip == "skip")

{

echo "IP already configured for DHCP.\n";

$new_ip = "dhcp";

}

else

{

echo "Configuring IP Settings: ";

system("netsh int ip set address \"$adaptor_name\" $new_ip $new_subnet $new_gateway");

}

// Work out DNS settings

if ($argc > $dns_arg && strtolower($argv[$dns_arg]) == "dhcp") // DNS argument given, and it's DHCP.

{

$new_dns = "dhcp";

}

elseif ($argc > $dns_arg && strtolower($argv[$dns_arg]) == "none" && strtolower($argv[1])=="dhcp") // DNS argument given, and it's NONE, and IP is DHCP.

{

$new_dns = "NO";

}

elseif ($argc > $dns_arg && strtolower($argv[$dns_arg]) == "none") // DNS argument given, and it's NONE.

{

$new_dns = "static none";

}

elseif ($argc > $dns_arg && strtolower($argv[$dns_arg]) != "dhcp") // DNS argument given, and it's not DHCP.

{

$new_dns = "static " . $argv[$dns_arg];

}

elseif (strtolower($argv[1]) == "dhcp") // IP set DHCP and no DNS given.

{

$new_dns = "dhcp";

}

elseif ($dns_serv == "None") // DNS not currently static.

{

$new_dns = "static none";

}

else // Change static DNS server to new network.

{

$new_dns = "static " . $argv[1].".".$dns_serv;

}

if ($new_dns != "NO") // DNS needs to be updated, so lets do it.

{

echo "Configuring DNS Settings: ";

system("netsh int ip set dns \"$adaptor_name\" $new_dns");

}

// Work out WINS settings

if ($argc > $wins_arg && strtolower($argv[$wins_arg]) == "dhcp") // WINS argument given, and it's DHCP.

{

$new_wins = "dhcp";

}

elseif ($argc > $wins_arg && strtolower($argv[$wins_arg]) == "none" && strtolower($argv[1]) == "dhcp") // WINS argument given, and it's NONE, and IP is DHCP.

{

$new_wins = "NO";

}

elseif ($argc > $wins_arg && strtolower($argv[$wins_arg]) == "none") // WINS argument given, and it's NONE.

{

$new_wins = "static none";

}

elseif ($argc > $wins_arg && strtolower($argv[$wins_arg]) != "dhcp") // WINS argument given, and it's not DHCP.

{

$new_wins = "static " . $argv[$wins_arg];

}

elseif (strtolower($argv[1]) == "dhcp") // IP set DHCP and no WINS given.

{

$new_wins = "dhcp";

}

elseif ($wins_serv == "None") // WINS not currently static.

{

$new_wins = "static none";

}

else // Change static WINS server to new network.

{

$new_wins = "static " . $argv[1].".".$wins_serv;

}

if ($new_wins != "NO") // WINS needs to be updated, so lets do it.

{

echo "Configuring WINS Settings: ";

system("netsh int ip set wins \"$adaptor_name\" $new_wins");

}

echo "IP Address: " . $new_ip . "\n";

if ($new_subnet != "")

{

echo "Subnet: " . $new_subnet . "\n";

}

if ($new_gateway != "")

{

echo "Gateway: " . $new_gateway . "\n";

}

if ($new_dns != "NO")

{

echo "DNS: " . $new_dns . "\n";

}

if ($new_wins != "NO")

{

echo "WINS: " . $new_wins . "\n";

}

echo "\n";

?>