Send a binary file (line by line) to a socket server with Netcat

假装没事ソ 提交于 2019-12-01 12:00:44

You can achieve it in two steps:

1) You need to start nc with a named pipe (fifo) as its input:

mkfifo /tmp/fifoIn; cat /tmp/fifoIn | nc localhost 2222 &

2) Send your data from file input.txt, line by line with 2 sec delay:

cat input.txt | while read line; do echo $line; sleep 2; done > /tmp/fifoIn

I've tested it with this "server" (I'm using openbsd netcat syntax):

nc -l localhost 2222

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:

#!/bin/bash
#
# sendlines.sh, v1.00, initial release
#
i="0"
while [ $i -lt 4 ]
do
  echo -ne "\x00e\x00\x0000370513,6598,no,8,,2z\x00"
  sleep 2
  i=$[$i+1]
done

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!