As a spin off to this Stack Overflow question I want to archieve the same except for a couple of tweaks.
I want to connect to a host, send a binary file, line by line, and have a couple of seconds of delay between each line... and it must be in the same connection. The strings I want to send are mostly textbased but there are a couple of non-printable chars in the line.
This is an example of a string/line I want to send (thousands of lines per file):
<null>e<null><null>00326513,6598,no,8,,2z<null>
The <null>
value is the HEX value 0x00
.
I have rewitten my question from its first entry but @mzet answered me on my original question as my question was text-only then but I later on discovered that my string had these 0x00
chars inside which I could not see at first. I want to use his answer as a base as I think it almost works but it may need a tweak so this is his contribution, not mine.
If I have a file with 3 lines:
<null>e<null><null>00370513,6598,no,8,,2z<null>
<null>f<null><null>00891548,6598,yes,8,,3z<null>
<null>f<null><null>00129525,6598,yes,8,,2z<null>
I then thought I could tweak @mzet answer and replace the <null>
values inside my file with \x00
and set -e
on the echo
command:
[root@srv]# mkfifo /tmp/fifoIn; cat /tmp/fifoIn | nc localhost 2222 &
[root@srv]# cat myfile | while read line; do echo -ne $line; sleep 2; done > /tmp/fifoIn
When I do that, I can see the x00
on the serverside:
[root@srv]# nc -l 2222
x00ex00x0000370513,6598,no,8,,2zx00
x00fx0000891548,6598,yes,8,,3zx00
x00fx00x0000129525,6598,yes,8,,2zx00
Is it possible to send a binary (text?) file as I want? And if it it not possible to send a file, line by line, is it then possible to send one string several thousands of times? Strictly necessary they don't need to be unique or in a file as I could manage with the same string repeating itself.
EDIT #1
Why is my script only sending one line to the server (I would expect 4 lines) afterwards it is just pausing(?) forever. The client (nor the server) closes its connection but it doesn't come with any errors:
rm -f /tmp/fifofile
mkfifo /tmp/fifofile
cat /tmp/fifofile | nc 192.168.20.6 5000 &
sleep 1
i="0"
while [ $i -lt 4 ]
do
echo "$i"
echo -ne "\x00e\x00\x00001212dsfdsfdsfsdfsdfsdfdsf\x00" | tee /tmp/fifofile
sleep 1
i=$[$i+1]
done