开发者

How do I output error for DNS resolve in Perl?

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 ($userinpu开发者_开发百科t2);

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

Can someone please give advice on the codes? Thanks!


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.


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


Please check the following to resolve the above "dns" issues in perl script.

As DNS server is not running, perl will not resolve the address. so it returns an empty string and inet_ntoa will throw error for that empty string.

If you are using a linux system please verify the following:

a) Check the internet address in the file /etc/resolv.conf

nameserver 172.19.1.11  (IP address of your internet or survice provider)

b) Add "dns" in the /etc/nsswitch.conf file as follows:

hosts:      files dns
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜