I am trying to use socat to listen on some UDP ports, and then write the data coming in through UDP to a file.
For example, I want to listen on port 53:
socat UDP-L:53,reuseaddr,pktinfo,fork EXEC:"./dns.sh"
My dns.sh is like:
while read line; do
thedate=`date +"%Y-%m-%d %H:%M:%S"`
printf "%s %s %s %s\n" "$thedate" "$SOCAT_PEERADDR" "$SOCAT_PEERPORT" "$line" >> dns.txt
done
However, it didn't work. It seems the root cause is, the input UDP data(a dns query, e.g. "abc.com") has no an new line character at the end.
Anyone could give me any help? How to make the script to read it successfully when the UDP data don't have a new line charactor at the end?
Or there are any alternative way I could achieve the same goal(output the data in the same format I required)?
Thanks.
======update 1, tried socat -b as arto suggested======= I tried "socat -b", but seems it didn't work.
I run the command:
socat -b 15 UDP-L:53,reuseaddr,pktinfo,fork EXEC:"./dns.sh" &
The script dns.sh was:
root@log-server:~# cat dns.sh
#!/bin/bash
firstdate=`date +"%Y-%m-%d %H:%M:%S"`
printf "%s %s %s %s\n" "$firstdate" "$SOCAT_PEERADDR" "$SOCAT_PEERPORT" "UDP incoming... " >> dns.txt
while read -t 3 line; do
thedate=`date +"%Y-%m-%d %H:%M:%S"`
printf "%s %s %s %s\n" "$thedate" "$SOCAT_PEERADDR" "$SOCAT_PEERPORT" "$line" >> dns.txt
done
but when I tried to ping a domain name either > 15 bytes or < 15 bytes, i only got:
2013-08-02 21:12:55 192.168.0.142 49899 UDP incoming...
2013-08-02 21:12:59 192.168.0.142 49899 UDP incoming...
2013-08-02 21:13:03 192.168.0.142 49899 UDP incoming...
Use -b option:
Sets the data transfer block [size_t]. At most bytes are transferred per step. Default is 8192 bytes.