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

前端 未结 2 1669
悲哀的现实
悲哀的现实 2021-01-16 04:34

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 lin

相关标签:
2条回答
  • 2021-01-16 05:02

    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
    
    0 讨论(0)
  • 2021-01-16 05:11

    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.

    0 讨论(0)
提交回复
热议问题