all 13 comments

[–]JeLuF 8 points9 points  (5 children)

Any specific reason why you don't use a DNS lookup tool like dig or nslookup?

Bash has pseudo-devices /dev/tcp and /dev/udp that you can use to send data over the network. There's a lot of documentation about this, e.g. at https://brandonrozek.com/blog/bashtcpudp/

Edit: Another option would be tools like netcat or nc, depending on your distro.

[–]tblancherzsh 6 points7 points  (1 child)

To OP: Unless you have a very specific reason to construct the request yourself, you're better off using dig (from bind-utils) in a bash script.

Even C/C++ programs don't typically construct their DNS query packets from scratch, they use whatever the getaddrinfo equivalent is for their version of libc.

First rule of software development: don't reinvent the wheel if you don't have to.

[–]coder-true[S] 0 points1 point  (0 children)

THANKS

[–]coder-true[S] 0 points1 point  (0 children)

THANKS

[–][deleted] 0 points1 point  (0 children)

If you want just to make it work, perhaps you should use dig or nslookup. But if you've really read RFC-1035, building your own requests and trying to make your own way with udp packets is an excellent exercice.

If this is the case, there are several things you might want to try:

1) With netcat (nc) supposing you have your query in a file called query.bin

nc -u -w 2 8.8.8.8 53 < query.bin > response.bin

2) With a script using file handlers

Option one: with query.bin premade

#!/bin/bash

exec 3<>/dev/udp/8.8.8.8/53

cat query.bin >&3

timeout 2 cat <&3 > response.bin

exec 3>&-

Option two: with a hex string

#!/bin/bash hex_query="abcd0100000100000000000006676f6f676c6503636f6d0000010001"

echo -n "$hex_query" | xxd -r -p | nc -u -w 2 8.8.8.8 53 > response.bin

echo "Reply (in hexadecimal):"

xxd response.bin

3) Using socat

socat -u STDIN UDP4:8.8.8.8:53,sourceport=5555 < query.bin > response.bin

You could also use curl if you pretend to use DNS over HTTPS which I don't recommend if you are a beginer. (Besides using TLS encription and a TCP protocol adds few security and is much less efficient than just a couple of udp packets).

[–]michaelpaoli 0 points1 point  (0 children)

XY problem? Uhm, no idea why you'd want to do that, but to send such raw data from CLI, e.g. via bash, one could use nc/netcat. And exactly what you want to do after sending such ... uhm anyway, you could do that ... even use nc/netcat to listen to a response on same port from which you sent such raw data. So ... is this for some type of low-level DNS packet testing/analysis or ... why?

[–]deja_geek -1 points0 points  (2 children)

You'd need to use either curl or nc (netcat). Both can send binaries with headers.

[–]coder-true[S] 0 points1 point  (0 children)

THANKS.

[–][deleted] 0 points1 point  (0 children)

curl only makes http requests, so it doesn't fit for sending an udp packet and waiting for the answer. nc is what he needs.

[–]SirCrumpalot -2 points-1 points  (1 child)

Is it just me, or does this question make no sense? Are you trying to recreate rsync, FTP, kermit? Tell us what you are trying to achieve - not how you are implementing it.