I have a script which allows me to search for a hostname after inputting an IP address which is a DNS server will be forwarded.
However, although everything works fine, the program can not print errors, which I would like to see if the DNS can not be found.
Code:
#! Use the / usr / bin / perl IO :: socket; Use warnings; Strict use; Use the term: ANSIColor; Use socket; Sys: Hostname; Print "\ nYou can now show the DNS IP address! \ N \ n"; Print "************* \ n"; Print "| DNS address | \ n"; Print "************* \ n"; Print \ nPlease enter a hostname that you want to see \ n \ n "Print" \ n \ nEnter the hostname of the option here: "; my $ userchoice = & lt; & gt ;; Chomp ( $ Userchoice; my $ host = hostname (); my $ hostname = $ user's choice; my $ packed_ip = gethostbyname ("$ hostname"); My $ ip_address = inet_ntoa ($ packed_ip) or system ("clear"), System ("/ root / desktop /showdns.pl"), die ("$ hostname can not be resolved: $! \ N, try again"); $ my color = text color = $ "Bold Underline Blue"); Print "\ n \ nThe hostname is IP address: $ coloredText \ n \ nPress" Print "to return to the main menu \ n \ n"; $ my userinput2 = & Amp; lt; & gt; chomp ($ userinput2); system ("clear"); system ("/ root / desktop / simpleip.pl");
Can someone please give some advice on the code?
To sequence the action | Do not misuse the operator. This is not what you want, though However, what he wants is not clear to me. When are two system calls called? On success or failure?
If it happens that when a person is being called (), then you can do this:
My $ i_addr = scalar (gethostbyname ($ Hostname || 'localhost')) or system ("clear"), system ("/ root / desktop / showdns.pl"), die ("can not 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"); Dead ("$ hostname can not be solved: $! \ N, try again"); } $ Name = inet_ntoa ($ i_addr);
(Fixed duplication of inet_ntoa; You need to verify the success of gethostbyname before calling it.)
Comments
Post a Comment