[GH-ISSUE #64] Option to get only the IP as result #44

Closed
opened 2026-03-14 01:08:47 +03:00 by kerem · 9 comments
Owner

Originally created by @kelvinkad on GitHub (Feb 12, 2024).
Original GitHub issue: https://github.com/ameshkov/dnslookup/issues/64

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.

Originally created by @kelvinkad on GitHub (Feb 12, 2024). Original GitHub issue: https://github.com/ameshkov/dnslookup/issues/64 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.
kerem 2026-03-14 01:08:47 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@mohshami commented on GitHub (Feb 12, 2024):

You can do it like this

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

Hope that helps

<!-- gh-comment-id:1939372507 --> @mohshami commented on GitHub (Feb 12, 2024): You can do it like this ``` JSON=1 dnslookup google.com tls://dns.adguard.com | jq -r '.Answer[].A' ``` Hope that helps
Author
Owner

@kelvinkad commented on GitHub (Feb 12, 2024):

Yeah! It helped a lot! Thanks

<!-- gh-comment-id:1939377858 --> @kelvinkad commented on GitHub (Feb 12, 2024): Yeah! It helped a lot! Thanks
Author
Owner

@mohshami commented on GitHub (Feb 12, 2024):

You are most welcome :)

<!-- gh-comment-id:1939378793 --> @mohshami commented on GitHub (Feb 12, 2024): You are most welcome :)
Author
Owner

@kelvinkad commented on GitHub (May 1, 2024):

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?

<!-- gh-comment-id:2089025075 --> @kelvinkad commented on GitHub (May 1, 2024): 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 <stdin>:41): Cannot iterate over null (null) Do you know how can I solve this?
Author
Owner

@mohshami commented on GitHub (May 3, 2024):

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

<!-- gh-comment-id:2092074482 --> @mohshami commented on GitHub (May 3, 2024): This normally happens when the reply doesn't contain the required field, what is the output without jq part?
Author
Owner

@kelvinkad commented on GitHub (May 5, 2024):

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?

<!-- gh-comment-id:2094850971 --> @kelvinkad commented on GitHub (May 5, 2024): 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?
Author
Owner

@mohshami commented on GitHub (May 5, 2024):

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"

<!-- gh-comment-id:2094879744 --> @mohshami commented on GitHub (May 5, 2024): 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"
Author
Owner

@kelvinkad commented on GitHub (May 6, 2024):

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.

<!-- gh-comment-id:2095025881 --> @kelvinkad commented on GitHub (May 6, 2024): 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.
Author
Owner

@mohshami commented on GitHub (May 6, 2024):

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

<!-- gh-comment-id:2095031159 --> @mohshami commented on GitHub (May 6, 2024): Well I only have a few lines in dnslookup, I'm just here to help :)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/dnslookup#44
No description provided.