0

So my bash scripting is not the best, but the following script takes IP addresses from a text file and feeds the output to a shell and shows only the dns host names that resolve.

#!/bin/bash
while read line
do
dig @1.2.3.4 -x  "$line" +short
done

and the output would look like:

one.foo.local
two.foo.local
three.foo.local

How can I also print the ip address for the address that resolve to a dns name on the same line so it would appear like:

one.foo.local 2.3.4.5
two.foo.local 6.7.8.9
three.foo.local 7.5.3.1

Thanks

1 Answer 1

2

You can try something like:

#!/bin/bash
while read line
do
nme=$(dig @1.2.3.4 -x  "$line" +short)
if [ "$(echo "$nme"|wc -c)" -gt 1 ]
   then
    echo "$nme $line"
fi 
done<input_file
9
  • That prints out all of the ip addresses. I only wish to print the address that are resolved to a name. One stop closer. Thanks. Sep 21, 2021 at 16:22
  • @SilverSurfer can you clarify what do you mean by "only wish to print the address that are resolved to a name". Please provide in question a example Sep 21, 2021 at 16:23
  • 1
    @roaima, i get the point, let me edit the code :) Sep 21, 2021 at 16:25
  • 1
    Exactly Romeo. unresolved not on the list Sep 21, 2021 at 16:28
  • 1
    Yes, Thank you very much. what does the -gt do? Sep 21, 2021 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.