nerdexam
CompTIA

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.

Scripting, Containers and Automation

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)
  • A
    5% (2)
  • B
    82% (31)
  • C
    3% (1)
  • D
    11% (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.

AMYIP =$(dig pc21.local +short

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.

BMYIP =$(dig pc21.local +norecurse +nocomments)Correct

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.

CMYIP =$(dig pc21.local + answer + mostatus

+mostatus is not a valid dig option, making this command invalid and unable to execute successfully.

DMYIP =$(dig pc21.local +authority)

+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

#dig#DNS lookup#shell scripting#IP resolution

Community Discussion

No community discussion yet for this question.

Full XK0-004 Practice