XK0-004 · Question #357
A lab virtual image is always named pc21.local and in variety of testing situation. The administrator wants to use the IP address pc21.local in a script but is concerned it may change from day to day,
The correct answer is B. MYIP =$(dig pc21.local +norecurse +nocomments). Using dig with +norecurse and +nocomments in a shell script reliably resolves a hostname to its current IP address for use as a variable.
Question
A lab virtual image is always named pc21.local and in variety of testing situation. The administrator wants to use the IP address pc21.local in a script but is concerned it may change from day to day, as the image is rebuilt fairly often. Which of the following should the administrator add to a script to ensure the IP address is always accurate?
Options
- AMYIP =$(dig pc21.local +short
- BMYIP =$(dig pc21.local +norecurse +nocomments)
- CMYIP =$(dig pc21.local + answer + mostatus
- DMYIP =$(dig pc21.local +authority)
How the community answered
(38 responses)- A5% (2)
- B82% (31)
- C3% (1)
- D11% (4)
Why each option
Using dig with +norecurse and +nocomments in a shell script reliably resolves a hostname to its current IP address for use as a variable.
Option A is missing the closing parenthesis in the command substitution - MYIP=$(dig pc21.local +short is syntactically invalid and would cause a shell script parse error.
The +norecurse flag directs the dig query to the DNS server without requesting recursive resolution, and +nocomments strips semicolon-prefixed comment lines from the output, making it easier to parse the IP address in a script. This approach ensures a direct DNS query for pc21.local each time the script runs, guaranteeing the IP variable reflects the most recent address assigned to the frequently rebuilt VM.
+mostatus is not a valid dig option, making this command invalid and unable to execute successfully.
+authority returns the authority section of the DNS response, which contains nameserver records rather than the A record IP address needed for the variable.
Concept tested: DNS hostname-to-IP resolution using dig in shell scripts
Source: https://linux.die.net/man/1/dig
Topics
Community Discussion
No community discussion yet for this question.