help ..... plz plz help me!!!

[b][red]This message was edited by White_Shadow at 2005-11-11 22:10:48[/red][/b][hr]
hi guys im new here and i need help with my assignment! i dont know how to get my code to do wat i need it to do. I would usually direct my questions towards my teacher but as its the weekend i can not and the assignment is due on monday morning. so could someone plz help me with this.

this is the question.....

Write a Perl program which will check whether or not an IP Address entered by a user is in fact a valid one. The user is to enter the IP Address AS A COMMAND LINE PARAMETER [HINT @ARGV].

eg the user will type check_ip.pl 192.168.9.23 and the script will attempt to validate the IP Address 192.168.9.23.

The script must first check whether the user has input any data at all and if not display an appropriate error message.

The rules for validating an IP Address are as follows. A valid IP Address must have:

4 octets, each separated by .
numbers in each of the 4 octets (ie no alphabetic or punctuation characters are allowed within each octet)
have the first octet between 1 and 255
have the 2nd, 3rd & 4th octets between 0 and 255


this is wat ive got so far, i havent used the argv yet, i wanted to get the basic working first....

use strict;

my $user_ip = 000.000.000.000;
my $first_octet = 000;
my $second_octet = 000;
my $third_octet = 000;
my $fourth_octet = 000;
#my $ip_ok;

system ("cls");

print "
Please enter an IP address you wish to check.
--> ";
$user_ip = ;
chomp ($user_ip);
my @temp_array = ($user_ip);

@temp_array = split(/./, $_);
$first_octet = @temp_array [0];
$second_octet = @temp_array [1];
$third_octet = @temp_array [2];
$fourth_octet = @temp_array [3];

if ($first_octet < 1 or $first_octet > 255) {
print "$user_ip, this ip address is not valid";
}
if ($second_octet < 0 or $second_octet > 255) {
print "$user_ip, this ip address is not valid";
}

if ($third_octet < 0 or $second_octet > 255) {
print "$user_ip, this ip address is not valid";
}

if ($fourth_octet < 0 or $second_octet > 255) {
print "$user_ip, this ip address is not valid";
}
else {
print "$user_ip, this ip address is valid";
}
this is probably all wrong but can someone plz help me....plz plz help me!!!!

Comments

  • change these two lines:

    my @temp_array = ($user_ip);
    @temp_array = split(/./, $_);

    to:

    my @temp_array = split(/./,$user_ip);

    and see if that helps.

  • [b][red]This message was edited by White_Shadow at 2005-11-12 0:32:41[/red][/b][hr]
    : change these two lines:
    :
    : my @temp_array = ($user_ip);
    : @temp_array = split(/./, $_);
    :
    : to:
    :
    : my @temp_array = split(/./,$user_ip);
    :
    : and see if that helps.
    :
    :

    no mate it still doesnt do it right. the program works, it just when u run it, it always says its not valid something to do with the way the IF statements are place or work i think!!! THANKS FOR THE INPUT ANYWAY!!!


  • [b][red]This message was edited by rsmanu at 2005-11-12 10:34:37[/red][/b][hr]
    Hi friend,

    Read the question posted by you. With my knowledge of the question I am posting this solution that I could get.

    ######################################################################

    #! /usr/bin/perl

    use warnings;
    use strict;

    my @ipoctets = split /./, shift;

    if ($ipoctets[0] < 1 or $ipoctets[0] > 255) {
    print "Invalid IP Address.
    ";
    exit;
    }

    for (1 .. 3) {
    if ($ipoctets[$_] < 0 or $ipoctets[$_] > 255) {
    print "Invalid IP Address.
    ";
    exit;
    }
    }

    print "
    The IP Address entered is valid.";

    #######################################################################

    Try out thhis program and check whether it suits your requirements.

    Good Luck.

    rsmanu.


  • [b][red]This message was edited by White_Shadow at 2005-11-12 17:42:8[/red][/b][hr]
    hey dude thanks,

    i would use it but we havent done some of that stuff in class yet so it might look a bit sus! :D but thanks again ill try it out and see if it works. i finially got mine working anyway but its messy then yours and alot more lines. (hey but it works!!) well the only thing i cant do is put it through the @ARGV thing it jus dont work ill keep at it but if you know how this @ARGV works give me a shout!!

    Thanks for ya help

    heres my script with everything working bar the @ARGV

    #james mcgill
    #november 2005
    #assignment 2 question 1 check_ip
    #this program will test and check if the users ip they have entered is a valid ip address.



    use strict;

    my $user_ip;
    my $first_octet;
    my $second_octet;
    my $third_octet;
    my $fourth_octet;
    my $ip_test = "true";
    my $num_msg;
    my $letter_msg;


    system ("cls");

    print "
    Please enter an IP address you wish to check.
    --> ";
    $user_ip = ;
    chomp ($user_ip);

    if ($user_ip !~ /D [^0-9]/) {
    $ip_test = "false";
    $letter_msg = "The IP address contained a letter, IP addresses can only be numbers.";
    }


    my @ip_address = ($user_ip);


    foreach (@ip_address){

    my @temp_array = split(/./, $_ );
    $first_octet = @temp_array [0];
    $second_octet = @temp_array [1];
    $third_octet = @temp_array [2];
    $fourth_octet = @temp_array [3];



    if ($fourth_octet < 0 <=> $fourth_octet >= 256) {
    $ip_test = "false";
    $num_msg = "It failed on the fourth octet, octet four must be between 0 and 255.";
    }

    if ($third_octet < 0 <=> $third_octet >= 256) {
    $ip_test = "false";
    $num_msg = "It failed on the third octet, octet Three must be between 0 and 255.";
    }

    if ($second_octet < 0 <=> $second_octet >= 256) {
    $ip_test = "false";
    $num_msg = "It failed on the second octet, octet Two must be between 0 and 255.";
    }

    if ($first_octet < 1 <=> $first_octet >= 256) {
    $ip_test = "false";
    $num_msg = "It failed on the first octet, octet one must be between 1 and 255.";
    }


    }

    if ($ip_test eq "false") {
    print "
    $user_ip, this ip address is not valid
    ";
    print $num_msg,"
    ",$letter_msg,"
    ";
    }
    else {
    print "
    $user_ip, this ip address is a valid address
    ";
    }

  • [b][red]This message was edited by KevinADC at 2005-11-12 17:45:7[/red][/b][hr]
    your code that you posted has:

    if(){
    }
    if(){
    }
    if(){
    }
    else {
    }

    when you probably meant to use:

    if(){
    }
    elsif(){
    }
    elsif(){
    }
    else{
    }

    which makes it one continuous block of related conditions.

    Since it's a school assignment I don't want to just post the exact code you need based on the code you posted, but take a look at using a if/elsif/else block


  • [b][red]This message was edited by White_Shadow at 2005-11-12 18:12:14[/red][/b][hr]
    yeah i know bout elsif statement jus makes it tider doesn't, it still works the same either way cause u dont need an else for every if, its working atm jus i cant figure the @ARGV function out. i know why it wasnt workin b4 it was because i had use copy and paste and the if statments were the same so it only test the one octet!!! LOL simple mistake i thought it was something worse!! :D

    well if u know how to get the @ARGV function to work it would be great. this is only question 1 of 6 i have to do LOL should get em done by monday (i hope) i got till 8 pm monday Australian time to email the scripts in.

    i know its an assignment and i dont want u to post the script for the solution just some hints and tips is very much apprieciated!! thanks for ya input i will prolly tidy it up with the elsif statements when i get the @ARGV function working!!!

    thanks for all ya commments
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories