2010-09-27 47 views
0

我有一個腳本,它允許我在輸入一個可以轉發到DNS服務器的IP地址後查找主機名。如何使用Perl爲DNS查找配置「檢查錯誤」?

但是,即使一切正常,程序似乎無法打印出我想要的示例如果DNS無法找到的錯誤。

的代碼:

#!/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/simpleip.pl"); 

可有人請給出代碼一些建議嗎?

回答

1

請勿濫用|操作員執行一系列操作。這不是你想做的,儘管你想要的東西對我來說不是很清楚。兩個系統調用何時應該被調用?成功還是失敗?

如果它應該當死()將被調用來完成,你可以這樣做:

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); 

(INET_NTOA的固定誤用;你需要驗證的gethostbyname成功之前,你可以調用它。 )

+0

爲了澄清,'|'是按位或運算符,'||'是邏輯或運算符。換句話說,當你正在進行數學運算時,你只能看到'|'。 – 2010-09-27 08:12:31

+0

它只有在失敗時激活,「或死」被認爲是激活的。 – JavaNoob 2010-09-27 08:21:00

+0

@JavaNoob:更新了我的答案 – ysth 2010-09-27 15:37:08