tags:

views:

81

answers:

1

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?

+1  A: 

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

ysth
Just to clarify, `|` is the bitwise-OR operator, and `||` is the logical-OR operator. In other words you should only ever see `|` when you are performing math.
PP
Its only activated when on failure which "or die" is suppose to be activated.
JavaNoob
@JavaNoob: updated my answer
ysth
Still does not work.... You are missing a bracket at "can't resolve..." The error shown is "Bad arg length for Socket::inet_ntoa, length is 9, should be 4 at /root/Desktop/showdns.pl line 25, <> line 1.".
JavaNoob
@JavaNoob: sorry bout that
ysth
Still doesn't work..... same error...
JavaNoob
@JavaNoob: oh, that would be because you are misusing inet_ntoa. What are you trying to accomplish with it?
ysth
As mentioned above im trying to allow the user to type in etc "www.google.com" and the script would then translate to output the ip address etc "168.*.*.*". That part of the script is able to work fine. However the problem arises when the user types in etc "hi" which the script should be able to output "cannot resolve name" instead the system outputs the system error and quit from the script.
JavaNoob
@JavaNoob: that's because you can't call inet_ntoa unless gethostbyname succeeded and gave a packed ip address for inet_ntoa to work with.
ysth
After executing your additional script by placing a wrong address etc www.whatisthissupposetodohuh.com the errors of "Use of uninitialized value in subroutine entry at ./showdns.pl line 31, <> line 1.Bad arg length for Socket::inet_ntoa, length is 0, should be 4 at ./showdns.pl line 31, <> line 1." appears.
JavaNoob
Changed the original codes abit. I think it might help you to understand better.
JavaNoob
@JavaNoob: you are still doing it wrong. First see if gethostbyname succeeded, and if not, die. Only if it succeeded can you call inet_ntoa.
ysth
Yea But I can't seem to get your script working. I can't even reverse dns resolve after I place your script into my original script. Same error. Did you test out your codes on terminal?
JavaNoob
@JavaNoob: Yes. If you are getting that error, you are calling inet_ntoa after gethostbyname failed. My code doesn't do that. Stop doing that.
ysth
After loads of testing the first code seems to work however when I enter an invalid address name, the script just refreshes and goes back to the same page without printing the "die("Can't resolve $hostname: $!\n ,try again");". And only prints that error message after I quit the script by force/normally.
JavaNoob
@JavaNoob: wait, is /root/Desktop/showdns.pl this same script? why are you having it start itself via system?
ysth
Erm its linked like this simpleip --> showdns.pl and I start the script via the terminal with the root permission. It just seems that placing "die" somehow does not print the "can't resolve..." when the error occurs but only after when the script ended.
JavaNoob
@JavaNoob: that's because when you get an error, you are restarting the script with a system() call, then giving the error when that script has finished and the system() call returns. I don't know why you are doing that, but it doesn't sound like what you want.
ysth
Wow! Your last comment really helped! I think this sequence suits my script "stem("clear"), print("Can't resolve $hostname: $! ,try again"), system("/root/Desktop/showdns.pl");". By the way I can't seem to print the system error after removing "die" so is there any other way that I can print the system error without putting in "die"? Thanks mate!
JavaNoob
@JavaNoob: what do you mean by "print the system error"? maybe you mean `print(STDERR "Can't resolve $hostname: $!, try again")` ?
ysth
the error I am referring is the errors such as "net_ntoa, length is 0, should be 4 at ./showdns.pl line 31, <> line 1." which is also the "$!" in "die". After trying out the STDERR the errors are still not shown. Sorry about this I know its tough and tiring answering my questions but I hope you can help. Thanks.
JavaNoob
@JavaNoob: I don't understand. I think you'd better post a new question showing your current code, what it's doing, and what you want it to be doing.
ysth
Hmmm... Should I post it here or open a new question?
JavaNoob
@JavaNoob: I'd do a new question.
ysth