开发者

How can I configure "Checking for Errors" for DNS Lookup using Perl?

I have a script which allows me to lookup for a hostname after inputting an IP address which would be forwarded to a DNS server.

However even though everything works fine, the program can't seem to print out the errors that I want example if the DNS cannot be found.

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 $host = hostname();

my $hostname = $userchoice;

my $packed_ip = gethostbyname("$hostname");

my $ip_address = inet_ntoa($packed_ip) or system("clear"), system("/root/Desktop 
/showdns.pl"), die("Can't resolve $hostname: $!\n ,try again");

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 = &lt;&gt;;
chomp ($userinput2);

system("clear");
system("/root/Desktop/simplei开发者_如何学运维p.pl");

Can someone please give some advice on the codes?


Don't misuse the | operator to perform a sequence of actions. It's not doing what you want, though what you want isn't clear to me. When are the two system calls supposed to be invoked? On success or failure?

If it's supposed to be done when die() is going to be called, you can do:

my $i_addr = scalar(gethostbyname($hostname || 'localhost'))
    or system("clear"), system("/root/Desktop/showdns.pl"), die("Can't resolve $hostname: $!\n ,try again");
my $name = inet_ntoa($i_addr);

my $i_addr = scalar(gethostbyname($hostname || 'localhost'));
if ( $i_addr ) {
    system("clear");
    system("/root/Desktop/showdns.pl");
    die("Can't resolve $hostname: $!\n ,try again");
}
my $name = inet_ntoa($i_addr);

(Fixed misuses of inet_ntoa; you need to verify success of gethostbyname before you can call it.)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜