Question : Need help with a perl script taking keyboard input

Hi

I am looking for assistance with what seems like a simple script but it's giving me problems. My script simply needs to take input from a user in the form of an IP address and then pass that IP to a function as an argument. Firstly I have no real clue as to how to "enforce" the person entering the data to be made to enter a valid IP address i.e. 1.1.1.1 would not be valid but 192.168.1.1 would be a valid input. Secondly, I need to know how to take that input, once validated as a correct IP address (correctly formatted I guess would be easiest here), and pass it in the form of a variable to another function. To clarify, the script will:

Prompt the user for an IP address
validate that the IP entered is correct in it's formatting
then prompt the user for a subnet mask
determine whether the mask entered was 255.255.255.0 or 255.255.0.0
determine the type of network based on the mask entered i.e. 255.255.0.0 indicates a class B and 255.255.255.0 indicates a class C network
pass that inputted IP to a variable
pass the inputted mask to a variable

Take that information (IP in a variable and mask in a variable) and pass both to a function. This question is an amendment to a previous question.


Any help greatly appreciated.

Thanks.

Answer : Need help with a perl script taking keyboard input

That's a good recommendation from perldork.  I thought there should be a module to do the validating but I didn't know which module to use.  I've spent the last 15 minutes looking for one but I was looking in the wrong area; and now I see that perldork found it.

After running a test, I verified that the instructions that I gave you this morning will give you the bit info but I also found an error I made.  I have a couple of invalid sub-net masks in the hash.  Here's the updated version.

#!/usr/bin/perl

use strict;

# Make sure we load the Infoblox PERL Module
#use InfoBloxDNS;

#====== global var declarations ======

my ($ip, $mask, $bits);
my %netmask = (
            '255.0.0.0'         => '8',
            '255.128.0.0'       => '9',
            '255.192.0.0'       => '10',
            '255.224.0.0'       => '11',
            '255.240.0.0'       => '12',
            '255.248.0.0'       => '13',
            '255.252.0.0'       => '14',
            '255.254.0.0'       => '15',
            '255.255.0.0'       => '16',
            '255.255.128.0'     => '17',
            '255.255.192.0'     => '18',
            '255.255.224.0'     => '19',
            '255.255.240.0'     => '20',
            '255.255.248.0'     => '21',
            '255.255.252.0'     => '22',
            '255.255.254.0'     => '23',
            '255.255.255.0'     => '24',
            '255.255.255.128'   => '25',
            '255.255.255.192'   => '26',
            '255.255.255.224'   => '27',
            '255.255.255.240'   => '28',
            '255.255.255.248'   => '29',
            '255.255.255.252'   => '30',
         );

#=========== script body =============

# open a connection to the Infoblox server
ib_open_connection("223.4.200.244", "infoblox") or
   die "Could not open a connection to the 'infoblox' DNS server <$!>\n";

# Create networks...
print "Enter a Q to exit\n";
while (1) {
   ($ip, $mask, $bits) = get_IP_info();
   ib_insert_network($ip, $mask, "0.0.0.0", "0.0.0.0", "authoritative");
}
print "\nAll specified networks now imported on";
print " primary dnsprd.trb.oxhp.com server...\n";

# Close the connection to the Infoblox server
ib_close_connection();

#====== subroutine declarations ======

sub get_IP_info {
   my ($octet1, $ip, $mask);
   while (not defined $ip) {
      print "Enter an IP address: ";
      chomp (my $inputIP = );
      if ($inputIP =~ /^Q/i) {
         ib_close_connection();
         exit;
      }
      if( $inputIP =~ m/
         ^  ( [1-9] | [01]?\d\d | 2[0-4]\d | 25[0-5] )
         \.  ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
         \.  ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
         \.  ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
         $
         /xo) {
         $ip = $inputIP;
         $octet1 = $1;
      }
      else {
         print "$inputIP is not a valid IP address\n";
      }
   }
   while (not defined $mask) {
      print "Enter a subnet mask: ";
      chomp (my $inputmask = );
      if ($inputmask =~ /Q/i) {
         ib_close_connection();
         exit;
      }
      if (! exists $netmask{$inputmask}) {
         print "You entered an invalid subnet mask $inputmask\n";
         $inputmask = '255.0.0.0' if ($octet1 >= 1 && $octet1 <= 126);
         $inputmask = '255.255.0.0' if ($octet1 >= 128 && $octet1 <= 191);
         $inputmask = '255.255.255.0' if ($octet1 >= 192 && $octet1 <= 223);
         print "The default netmask is $inputmask\n";
         print "Do you want to use the default mask Y|N|Quit ";
         my $ans = ;
         if ($ans =~ /N/i) {
            redo;
         } elsif ($ans =~ /Q/i) {
            ib_close_connection();
            exit;
         }
      }
      $mask = $inputmask;
   }
   print "\n";
   return ($ip, $mask, $netmask{$mask});
}
Random Solutions  
 
programming4us programming4us