2

I have script like this

CHARSET=ASCII dig domain.com AXFR > domain.com.zone

if [ "$?" = "0" ]; then
    echo "OK"
else
    echo "Something went wrong"
    
fi

However, I saw there is non complete file with line at the end

;; communications error: end of file

Is there some bug in my script or how I can "catch" this error?

Nick
  • 826
  • 2
  • 15
  • 42
  • You may already know this, but be aware that zone transfers are much less common now than when DNS was young. Any standard that refers to zone transfers, as the default way of moving DNS data in bulk, is ancient and should be distrusted. (Ignore me if you're just doing this between machines under your own control, or if up to date documentation has instructed you to use a zone transfer.) – Glenn Willen Jun 12 '23 at 21:26
  • 1
    I am aware. This is third party zone. – Nick Jun 13 '23 at 19:29

1 Answers1

2

The exit status ($?) of the dig command does not reflect whether the zone transfer was successful or not.

We could check the output file for the error message like that:

CHARSET=ASCII dig domain.com AXFR > domain.com.zone

if grep -q "communications error: end of file" domain.com.zone; then
    echo "Something went wrong"
else
    echo "OK"
    gzip -9 domain.com.zone
fi
Saxtheowl
  • 1,112
  • 5
  • 8