如何在 Perl 中输出 DNS 解析错误?
我目前有一个 DNS 反向查找脚本,可以运行,但是该脚本存在一个能够输出 DNS 系统错误的小问题。
问题是这样的:
- 用户输入错误/错误的互联网地址名称等。“www.whyisthednsnothappening.com”
- 然后脚本将使用系统(清除)清除屏幕
- 然后脚本将打印“无法解析 DNS。错误”原因是:各种系统错误”
- 脚本将用户重定向回同一菜单/脚本以再次输入名称地址。
所以现在的主要问题是步骤 3,该脚本仅向我显示“无法解析 DNS。错误是由于:BLANK”,其中 BLANK 应该显示错误例如“Socket::inet_ntoa 的参数长度错误,长度为 0,在 ./showdns.pl 第 28 行,<> 第 1 行中应该为 4。” 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 $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 ($userinput2);
system("clear");
system("/root/Desktop/simpleip.pl");
有人可以就代码提供建议吗?谢谢!
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:
- User keys in false/wrong internet address name etc. "www.whyisthednsnothappening.com"
- The script would then clear the screen using system(clear)
- The script would then print "can't resolve DNS. The error is due to: various System error"
- 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 ($userinput2);
system("clear");
system("/root/Desktop/simpleip.pl");
Can someone please give advice on the codes? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
啊,我明白你的意思了。系统(“clear”)调用正在清除$!在您有机会打印 gethostbyname 的错误之前。
尽管据我所知, gethostbyname 返回的特定错误并不是很有意义。
您可能想考虑在脚本中放置一个循环,而不是使用 system() 重新开始。如果出现故障,您当然不想继续访问 inet_ntoa。请注意,inet_ntoa 与 DNS 查找没有任何关系;它与 DNS 查找无关。这是由 gethostbyname 完成的。 inet_ntoa 只是将 4 字节字符串更改为正常的 123.123.123.123
IP 地址的可打印形式。
sprintf("%vd", $i_addr)
做同样的事情。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.
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.另外两个问题:
如果您删除对
system('clear')
是否出现错误从 gethostbyname 获取显示
那么?
你为什么使用
系统('/root/Desktop/showdns.pl')
递归调用同一个脚本?
使用
exec
不是更好吗而不是
系统
?exec
终止当前进程。而
系统
整个新流程的分支以及
等待该进程退出。所以
例如,如果您的用户输入 20
无效的主机名,你最终会
有20个进程正在等待
最近的那个
创建。
格,
LDX
Two additional questions:
If you remove the call to
system('clear')
Does the errorfrom gethostbyname get displayed
then?
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
terminatesthe 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
请检查以下内容以解决 perl 脚本中的上述“dns”问题。
由于 DNS 服务器未运行,perl 将无法解析该地址。因此它返回一个空字符串,并且 inet_ntoa 将为该空字符串抛出错误。
如果您使用的是 Linux 系统,请验证以下内容:
a) 检查文件
/etc/resolv.conf
中的互联网地址b) 在 /etc/nsswitch.conf 文件中添加“dns”:如下:
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
b) Add "dns" in the /etc/nsswitch.conf file as follows: