如何配置“检查错误”使用 Perl 进行 DNS 查找?
我有一个脚本,允许我在输入将转发到 DNS 服务器的 IP 地址后查找主机名。
然而,即使一切正常,如果找不到 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 = <>;
chomp ($userinput2);
system("clear");
system("/root/Desktop/simpleip.pl");
有人可以就代码提供一些建议吗?
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 = <>;
chomp ($userinput2);
system("clear");
system("/root/Desktop/simpleip.pl");
Can someone please give some advice on the codes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要滥用
|
运算符来执行一系列操作。它没有做你想做的事,尽管我不清楚你想要什么。这两个系统调用应该在什么时候被调用?论成功还是失败?如果它应该在要调用 die() 时完成,您可以执行以下操作:(
修复了 inet_ntoa 的误用;您需要先验证 gethostbyname 是否成功,然后才能调用它。)
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:
(Fixed misuses of inet_ntoa; you need to verify success of gethostbyname before you can call it.)