如何配置“检查错误”使用Perl进行DNS查找? [英] How can I configure "Checking for Errors" for DNS Lookup using Perl?

查看:273
本文介绍了如何配置“检查错误”使用Perl进行DNS查找?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



尽管一切正常,但是我有一个脚本可以让我在输入要转发到DNS服务器的IP地址之后查找一个主机名。如果找不到DNS,该程序似乎无法打印出我想要的示例。



代码:

 #!/ usr / bin / perl 

用IO :: Socket;
使用警告;
使用strict;
使用术语:: ANSIColor;
使用Socket;
使用Sys :: Hostname;

打印\你现在显示DNS IP地址!\\\
\\\
;

print************* \\\
;
print| DNS地址| \\\
;
print************* \\\
;

打印\\\
请输入您要查看的主机名\\\\
;
打印\\\
\\\
输入选择此处的主机名:;
my $ userchoice =<> ;;
chomp($ userchoice);

我的$ host = hostname();

我的$ hostname = $ userchoice;

我的$ packed_ip = gethostbyname($ hostname);

我的$ ip_address = inet_ntoa($ packed_ip)或系统(clear),系统(/ root / Desktop
/showdns.pl),die(Can not resolve $ hostname:$!\\\
,再试一次);

我的$ colorText =彩色($ name,'bold underline blue');
打印\\\
\\\
主机名IP地址为:$ coloredText\\\
\\\
;

打印按回车键返回主菜单\\\
;
my $ userinput2 =& lt;& gt ;;
chomp($ userinput2);

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

有人可以给代码一些建议吗?

解决方案

不要滥用 | 运算符来执行一系列操作。这不是你想要的,虽然你想要的不清楚我。什么时候应该调用两个系统调用?成功或失败?



如果要在die()被调用时完成,可以执行以下操作:

 我的$ i_addr =标量(gethostbyname($ hostname ||'localhost'))
或系统(清除),系统(/ root / /showdns.pl),die(Can not resolve $ hostname:$!\\\
,try again);
我的$ name = inet_ntoa($ i_addr);

我的$ i_addr =标量(gethostbyname($ hostname ||'localhost'));
if($ i_addr){
system(clear);
system(/ root / Desktop / showdns.pl);
die(Can not resolve $ hostname:$!\\\
,try again);
}
我的$ name = inet_ntoa($ i_addr);

(修复了inet_ntoa的滥用情况;您需要验证gethostbyname的成功,然后才能调用) / p>

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/simpleip.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.)

这篇关于如何配置“检查错误”使用Perl进行DNS查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆