ameshkov / dnslookup

Simple command line utility to make DNS lookups to the specified server
MIT License
816 stars 72 forks source link

Option to get only the IP as result #64

Closed kelvinkad closed 6 months ago

kelvinkad commented 6 months ago

How can I get only the IP as result of the command?

For example, when I use "dnslookup google.com tls://dns.adguard.com" I`d like to get only 142.250.219.174 as result.

mohshami commented 6 months ago

You can do it like this

JSON=1 dnslookup google.com tls://dns.adguard.com | jq -r '.Answer[].A'

Hope that helps

kelvinkad commented 6 months ago

Yeah! It helped a lot! Thanks

mohshami commented 6 months ago

You are most welcome :)

kelvinkad commented 4 months ago

Hello, I faced a problem with this command we talked about. Maybe you can help me. When I run, for example: JSON=1 dnslookup google.com tls://dns.adguard.com | jq -r '.Answer[].A', like you suggested me to get only the IP, I get one, two, three dns addresses Is there a way to grab only one? And sometimes the first and second addresses are NULL, so I can`t select this, just the valid address, that is maybe the third value.

otherwise, I get this error: jq: error (at :41): Cannot iterate over null (null)

Do you know how can I solve this?

mohshami commented 4 months ago

This normally happens when the reply doesn't contain the required field, what is the output without jq part?

kelvinkad commented 4 months ago

Let me give you one full example The following command: JSON=1 dnslookup cfs10.blog.daum.net tls://dns.adguard-dns.com | jq -r '.Answer[].A' returns this: null null 121.53.201.236

If I repeat the same command withou jq the answer is this:

{ "Id": 42595, "Response": true, "Opcode": 0, "Authoritative": false, "Truncated": false, "RecursionDesired": true, "RecursionAvailable": true, "Zero": false, "AuthenticatedData": false, "CheckingDisabled": false, "Rcode": 0, "Question": [ { "Name": "cfs10.blog.daum.net.", "Qtype": 1, "Qclass": 1 } ], "Answer": [ { "Hdr": { "Name": "cfs10.blog.daum.net.", "Rrtype": 5, "Class": 1, "Ttl": 600, "Rdlength": 17 }, "Target": "t1.int.daumcdn.net." }, { "Hdr": { "Name": "t1.int.daumcdn.net.", "Rrtype": 5, "Class": 1, "Ttl": 600, "Rdlength": 27 }, "Target": "t1-int-jzdtavpl.kgslb.com." }, { "Hdr": { "Name": "t1-int-jzdtavpl.kgslb.com.", "Rrtype": 1, "Class": 1, "Ttl": 60, "Rdlength": 4 }, "A": "121.53.202.238" } ], "Ns": null, "Extra": null, "elapsed": 1067907345 }

See that using jq, the answer comes with null, null and then the answer I want, the A record? How Do I exclude the null results from the answer? And sometimes the answer comes with 2 or 3 ip`s, like this: null null 94.154.114.212 94.154.114.214 94.154.114.213 94.154.114.215 I would like to exclude this null results AND grab only ONE address, so the answer for the command I want would be like: 94.154.114.212 Only one ip, only one line, only one result.

You think this is possible?

mohshami commented 4 months ago

That's more of a jq thing at this point. You can do something like this (with the json you provided)

jq '.Answer[] | select (.A) | .A'

and you'll get

"121.53.202.238"

kelvinkad commented 4 months ago

That's great. The null answer was excluded. Now I just need to figured out how to grab only one answer when there are more than 2 dns addresses, but I'll see if I can. Thank you very much for your help even though the subject is not about your tool.

mohshami commented 4 months ago

Well I only have a few lines in dnslookup, I'm just here to help :)