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
After a lot of trying and pulling my hair I finally figured out that I could use NCat instead of Netcat as NCat can execute a command.
Start a connection with NCat to my socket server on port
5000
and execute the script./sendlines.sh
:ncat --exec "./sendlines.sh" 192.168.1.10 5000
./sendlines.sh
will send 4 lines with a delay of two seconds between each line:I have not figured out how to send a binary file, line by line, but this is not strictly necessary as I can manage by sending the same string many times.
If you know a way to send a binary file, line by line, it would be great as it would be the best solution for me.
You can achieve it in two steps:
1) You need to start nc with a named pipe (fifo) as its input:
2) Send your data from file input.txt, line by line with 2 sec delay:
I've tested it with this "server" (I'm using openbsd netcat syntax):