Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Ask user to enter IP address repitatively for entities like Netmask, Gateway, DNS, bonds etc till correct address entered

by rahulruns (Scribe)
on Nov 28, 2014 at 06:43 UTC ( [id://1108623]=perlquestion: print w/replies, xml ) Need Help??

rahulruns has asked for the wisdom of the Perl Monks concerning the following question:

I need to write a SCRIPT where it will ask user to enter IP address for entities like Netmask, Gateway, DNS, bonds etc. and will check if valid IP is enetered. If the IP is not valid it will ask to enter it again for the specific entity till the IP entered is correct. The IP could be any IP but a valid IP address. The issue I am facing is how to repeatedly ask for IP of specific entity like Netmask till IP is correct. Sample of code what I am writing is below

use Data::Validate::IP qw(is_ipv4); use Data::Dumper; my %ipaddress_namei = (); print "Enter IP address of Bond2\n"; my $bond2 = <STDIN>; $ipaddress_namei{bond2} = {$bond2};
  • Comment on Ask user to enter IP address repitatively for entities like Netmask, Gateway, DNS, bonds etc till correct address entered
  • Download Code

Replies are listed 'Best First'.
Re: Ask user to enter IP address repitatively for entities like Netmask, Gateway, DNS, bonds etc till correct address entered
by vinoth.ree (Monsignor) on Nov 28, 2014 at 07:03 UTC

    Hi,

    Have your code inside the while(1) like below,

    my %ipaddress_namei = (); while(1) { print "Enter IP address of Bond2\n"; my $bond2 = <STDIN>; chomp($bond2); if(&validate_ip($bond2)) { $ipaddress_namei{bond2} = {$bond2}; last; } } sub validate_ip { #return 0; return 1; }

    It will ask you to enter the IP address, and calls a function to validate the IP address, in function validate_ip do your validation part and return 1 for true, 0 for false, based on that the while will continue to get the input. This code will get the valid IP only once.


    All is well
Re: Ask user to enter IP address repitatively for entities like Netmask, Gateway, DNS, bonds etc till correct address entered
by QM (Parson) on Nov 28, 2014 at 10:03 UTC
    Are you having trouble with the loop construct?

    Perhaps something like this? (Completely untested)

    my @bonds = qw(bond2 bond3 bond4 bond6 bond7 bond8); my %ipaddress_namei; while (my @bonds_with_invalid_ip = grep { not exists $ipaddress_namei{ +$_}, $_ }, @bonds) { for my $bond (@bonds_with_invalid_ip) { printf "Enter IP address of %s\n", ucfirst($bond); my $bond_in = <STDIN>; chomp($bond_in); if (is_ipv4($bond_in)) { $ipaddress_namei{$bond} = {$bond_in}; } } }

    Update, a little simpler, using a FIFO:

    my @bonds = qw(bond2 bond3 bond4 bond6 bond7 bond8); my %ipaddress_namei; # Use @bonds as a FIFO while (my $bond = shift @bonds) { printf "Enter IP address of %s\n", ucfirst($bond); my $bond_in = <STDIN>; chomp($bond_in); if (is_ipv4($bond_in)) { $ipaddress_namei{$bond} = {$bond_in}; } else { # try again next time around push @bonds, $bond; } }

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: Ask user to enter IP address repitatively for entities like Netmask, Gateway, DNS, bonds etc till correct address entered
by BillKSmith (Monsignor) on Nov 28, 2014 at 22:05 UTC
    It probably is to late now, but you should at least consider a module such as IO::Prompt::Hooked. It does all the looping for you. All you provide is the messages and a callback to validate the response.
    Bill
Re: Ask user to enter IP address repitatively for entities like Netmask, Gateway, DNS, bonds etc till correct address entered
by rahulruns (Scribe) on Nov 28, 2014 at 11:47 UTC

    Thank You QM for your input. I went through your code there was only one correction {$bond_in} to $bond_in else it was giving an error

    with {} $ipaddress_namei{$bond} = {$bond_in}; Odd number of elements in anonymous hash at shortscript.pl line 31, <S +TDIN> line 1.
Re: Ask user to enter IP address repitatively for entities like Netmask, Gateway, DNS, bonds etc till correct address entered
by rahulruns (Scribe) on Nov 28, 2014 at 08:37 UTC

    Let me be more specific. I need to write a Script that takes input from users for NIC bonds 2 to 8 (we do not have bond5), Netmask, gateway and DNS. Then it has to check all the IP's enetred for these entities and ask for a particular entity if IP is enetered wrong. After this it has to create specific bond files with respective entries of IP addresses taken from user. I am able to create if I ask from user and they enter wrong IP but repetatively asking a question for one entitiy and matching the IP address to be entered in file is the problem I am facing

      Hi there,

      vinoth.ree has given you a good answer for the problem you seem to be having. You want to:

        Ask question.
        Test answer.
        Repeat until answer is acceptable.

      What is your question relating to Perl?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1108623]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-20 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found