I played around with bash(4.0.33) network support for learning purposes and tried to create a port scanner in bash. For TCP I opened a TCP/IP socket with
exec 3<>/dev/tcp/192.0.2.1/80
and appropriate action was taken if the connection was refused or connect system call timed out. However, with UDP I'm easily able to send a packet with
echo > /dev/udp/192.0.2.1/53
but how to read returning packets from correct socket? I mean UDP datagram sent to 192.0.2.1 has source port from ephemeral port range and thus I do not know which socket in /dev/udp/192.0.2.1/
directory should I read. Or isn't this doable without external utilities like tcpdump?
Bash's UDP support isn't great, and is compiled out on many distros (Debian/Ubuntu and derivatives especially). The recommended tool is netcat:
nc -u 192.0.2.1 53
Note: Use coproc or named pipes to read and write from the same netcat process. Don't send a packet first and try to catch the reply netcat.
Bash is really the wrong language for this though. Consider using Python, which handles both UDP and binary data way better, with just a couple of more lines of code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With