Code critique

Kevin Guidry kguidry at utk.edu
Fri Jan 18 16:37:09 CST 2002


	Much thanks in advance.

	The code is, in its present form, just a method of getting three
pieces of information from a user at the console, inserting them
into a command, and then exec'ing that command.  The specific pieces of
info are IP address, subnet mask, and gateway.  I do a quick check to make
sure that they all "look" like IP addresses before passing them to the
command.  The specific command is a Windows 2000 command "netsh" which
allows, among other things, you to change your network settings from the
command line.  In this case, I am just changing the three afore mentioned
items on a laptop that will be moved around quite a bit.
	My sincere apologies if	this is too long or not 
understandable; please bear with me as I learn.  Without further ado, here
it is:


use strict;

my $ip = 0.0.0.0;
my $netmask = 0.0.0.0;
my $gateway = 0.0.0.0;
my $validip = 0;
my $digit;
my @digits;
sub checkip;

print "Enter the IP address: ";
$ip = <stdin>;
chomp($ip);
if(!checkip($ip)){
# Crap code follows
# I want to user to see the error message, so I have to pause for a
keystroke
# since this will be run from the desktop and not a terminal
	print "\n$ip is not a valid IP address\n";
	print "\n\nPlease press enter...";
	$ip = <stdin>;
	die "$ip is not a valid IP address";
}

print "Enter the subnet mask: ";
$netmask = <stdin>;
chomp($netmask);
if(!checkip($netmask)){
# More of the same crap code
	print "\n$netmask is not a valid subnet mask\n";
	print "\n\nPlease press enter...";
	$ip = <stdin>;
	die "$netmask is not a valid subnet mask";
}

print "Enter the default gateway: ";
$gateway = <stdin>;
chomp($gateway);
if(!checkip($gateway)){
# More of the same crap code
	print "\n$gateway is not a valid IP address for a gateway\n";
	print "\n\nPlease press enter...";
	$ip = <stdin>;
	die "$gateway is not a valid IP address for a gateway\n";
}

#exec "netsh interface ip set address \"Local Area Connection\" static $ip
$netmask $gateway 1\n";
print "netsh interface ip set address \"Local Area Connection\" static $ip
$netmask $gateway 1\n";

# Another instance of crap code to pause for the user to read
print "\n\n\nPlease press enter...";
$ip = <stdin>;



# checkip checks the one variable passed in to see if it's a valid IP
address
# If returns a 1 on success and a 0 on failure
sub checkip{
	my $ip_being_checked = @_[0];
	if ($ip_being_checked =~ /(\d+)(\.\d+){3}/){ 
		$validip = 1;
	}
	if($validip){
		@digits = split(/\./, $ip_being_checked);
		foreach $digit (@digits){ 
			if ($digit < 0 || $digit > 255){ 
				$validip = 0;
				last;
			}
		}
	}
	if ($validip == "0"){
		return 0;
	}
	else{
		return 1;
	}
}

http://knoxville.pm.org/




More information about the Knoxville-pm mailing list