tags:

views:

88

answers:

2

I have currently a DNS Reverse lookup script which works however there is a small little issue of the script being able to output the DNS system errors.

The problems goes like this:

  1. User keys in false/wrong internet address name etc. "www.whyisthednsnothappening.com"
  2. The script would then clear the screen using system(clear)
  3. The script would then print "can't resolve DNS. The error is due to: various System error"
  4. The script re directs the user back to the same menu/script to type in the name address again.

So the main problem is now step 3 which the script only shows me "Can't resolve DNS. The error is due to: BLANK " Which BLANK is suppose to show errors like "Bad arg length for Socket::inet_ntoa, length is 0, should be 4 at ./showdns.pl line 28, <> line 1." and the menu of the DNS script is located below of the error print.

The Codes:

#!/usr/bin/perl

use IO::Socket;
use warnings;
use strict;
use Term::ANSIColor;
use Socket;
use Sys::Hostname;

print "\nYou are now in Show DNS IP Address!\n\n";

print "*************\n";
print "|DNS Address|\n";
print "*************\n";

print "\nPlease enter a hostname that you wish to view\n\n";
print "\n\nEnter the hostname of Choice Here: ";
my $userchoice =  <>;
chomp ($userchoice);

my $hostname = $userchoice;

my $i_addr = scalar(gethostbyname($hostname || 'localhost'));
if ( ! defined $i_addr ) {
my $err = $!;
my $herr = int herror(const char *s);
system('clear');
print("Can't resolve $hostname: $herr, try again");
exec("/root/Desktop/showdns.pl");
exit();
}

my $name = inet_ntoa($i_addr);
my $coloredText = colored($name, 'bold underline blue');
print "\n\nThe hostname IP address is: $coloredText\n\n";

print "Press enter to go back to the main menu\n\n";
my $userinput2 =  <>;
chomp ($userinput2);

system("clear");
system("/root/Desktop/simpleip.pl");

Can someone please give advice on the codes? Thanks!

+2  A: 

Ah, I see what you mean. The system("clear") call is clearing the $! variable before you have a chance to print the error from gethostbyname.

my $i_addr = scalar(gethostbyname($hostname || 'localhost'));
if ( ! defined $i_addr ) {
    my $err = $!;
    system("clear");
    print("Can't resolve $hostname: $err, try again");
    system("/root/Desktop/showdns.pl");
    exit();
}

Though as far as I can tell, the particular error gethostbyname returns isn't very meaningful.

You may want to look into putting a loop in your script instead of having it start over using system(). You certainly don't want to continue on to inet_ntoa if there was a failure. Note that inet_ntoa doesn't have anything to do with a DNS lookup; that's done by gethostbyname. inet_ntoa just changes a 4-byte string into the normal 123.123.123.123 printable form of an ipaddress. sprintf("%vd", $i_addr) does the same thing.

ysth
The error from the gethostbyname still can't be seen. Same message like "Can't resolve www.thisisverywierd.com: , try again" appears without the error when being redirected back to the script. This time round the errors don't even appear after I exit from the script... This is getting trickier...
JavaNoob
Ok guys I narrowed down the error but I still can't solve it so please advice. The main error now is due to the type of "system("clear");" that is used to clear the screen. Are there any other kind of screen clearing that allows the system not to clear out its buffer cache? I tried almost all the screen clearing methods but the cache is still cleared.
JavaNoob
@JavaNoob: show your new code
ysth
@ysth Posted new codes thanks!
JavaNoob
You there mate?
JavaNoob
A: 

Hi,

Two additional questions:

  1. If you remove the call to system('clear') Does the error from gethostbyname get displayed then?

  2. Why do you use system('/root/Desktop/showdns.pl') To call the same script recursively? Wouldn't it be better to use exec instead of system? exec terminates the current process. while system forks of an entire new process and waits for that process to exit. So if your users enter, for example, 20 invalid hostnames, you'll end up with 20 processes just waiting for the one that was most recently created.

Gr, ldx

ldx
@Idx 1) If I use the "if" loop when I remove the system(clear) I still do not see the errors. 2) System seems like a better choice as I am also using system to clear the screen. But I have tried exec and there are no changes to the output. Thanks.
JavaNoob
system is an ok choice for 'clear', because that is a process that starts, clears the screen, and then exits. Thus returning to your original code. It is a bad choice for the recursive call to your script, because that does not exit until the user enters a valid hostname to look up, hence never returning to your script, except when the user enters a valid hostname. Then it returns to your original script, but just to exit. Concerning your original problem: What system are you on? If I run your code on a winxp sp3 system, I get "Unknown error". Not helpfull, but different than what you get.
ldx
What do you get when you print $? That is supposed to contain whatever h_error would contain if you would be using C instead of perl
ldx
Hi I am using Red Hat 5 to create the script and I don't get what you mean by printing $ cause the error has now been changed to a $err variable.
JavaNoob
The question mark is part of the variable :-) So I mean: what do you get when you print "$?". (Or the long version: what do you get when you assign "$?" to "$err" instead of "$!" and then print that)
ldx
I get a "1" result? Probably means that there is 1 line of code error.
JavaNoob
According to the perldocs for gethostbyname (http://perldoc.perl.org/functions/gethostbyname.html) the "$?" variable will contain the same value that "h_errno" would contain in C. When I check netdb.h, the possible values for h_errno are: 1: HOST_NOT_FOUND 2: TRY_AGAIN 3: NO_RECOVERY 4: NO_ADDRESS or NO_DATA. So i guess you could check "$?" after your call and print an errormessage yourself when its value is 1
ldx
That will probably tons of messy codes....http://www.perlmonks.org/index.pl?node_id=773351 So I need to do a loop for the error checking?
JavaNoob