Come posso configurare “Controllo degli errori” per la ricerca DNS utilizzando Perl?

StackOverflow https://stackoverflow.com/questions/3801705

  •  25-09-2019
  •  | 
  •  

Domanda

Ho uno script che mi permette di ricerca per un nome host dopo aver inserito un indirizzo IP che sarà inoltrata a un server DNS.

Tuttavia, anche se tutto funziona bene, il programma può non sembrare di stampare gli errori che voglio esempio, se il DNS non può essere trovato.

I Codici:

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

Qualcuno può dare qualche consiglio sui codici?

È stato utile?

Soluzione

Do non abusare l'operatore | per eseguire una sequenza di azioni. Non è fare quello che vuoi, anche se ciò che si vuole, non mi è chiaro. Quando vengono le due chiamate di sistema dovrebbero essere invocato? In caso di successo o il fallimento?

Se si suppone di essere fatto quando die () sta per essere chiamato, si può fare:

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

(abusi fisse di inet_ntoa, è necessario verificare il successo di gethostbyname prima si può chiamare).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top