Skip to main content
Fixed 0 byte output
Source Link
JayCravens
  • 829
  • 4
  • 10

For the netcat server, I tested the followingstarted with:

while true; do (echo -ne "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" && cat | tee $(date +"%Y%m%d%H%M%S").png) | nc -l -p 1881; done

I used this curl command to test it and was successful:

curl -X POST --data-binary @your_image.png http://localhost:1881

These commands should enable youAt first glance, it appeared to succeed, but then I noticed the file was 0 bytes.

I tried and tried to get nc to output to a file and it just wouldn't do it. I now understand your frustrations.

#!/bin/bash

while true; do
  filename="$(date +"%Y%m%d%H%M%S").png"

  # Listen (-l), close idle @ 100ms (-i .1), wait avoids truncation (-w 2)
  nc -l -i .1 -w 2 -p 1881 | {

  echo -ne "HTTP/1.1 200 OK\r\nContent-Type: image/png\r\n\r\n"

  # Strip curl's response headers from output
  sed -e '1,7d' > ./"$filename"
  }
done

This script, with the scripts operationalsame curl command, should do the trick.

For the netcat server, I tested the following:

while true; do (echo -ne "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" && cat | tee $(date +"%Y%m%d%H%M%S").png) | nc -l -p 1881; done

I used this curl command to test it and was successful:

curl -X POST --data-binary @your_image.png http://localhost:1881

These commands should enable you to get the scripts operational.

For the netcat server, I started with:

while true; do (echo -ne "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" && cat | tee $(date +"%Y%m%d%H%M%S").png) | nc -l -p 1881; done

I used this curl command to test:

curl -X POST --data-binary @your_image.png http://localhost:1881

At first glance, it appeared to succeed, but then I noticed the file was 0 bytes.

I tried and tried to get nc to output to a file and it just wouldn't do it. I now understand your frustrations.

#!/bin/bash

while true; do
  filename="$(date +"%Y%m%d%H%M%S").png"

  # Listen (-l), close idle @ 100ms (-i .1), wait avoids truncation (-w 2)
  nc -l -i .1 -w 2 -p 1881 | {

  echo -ne "HTTP/1.1 200 OK\r\nContent-Type: image/png\r\n\r\n"

  # Strip curl's response headers from output
  sed -e '1,7d' > ./"$filename"
  }
done

This script, with the same curl command, should do the trick.

Source Link
JayCravens
  • 829
  • 4
  • 10

For the netcat server, I tested the following:

while true; do (echo -ne "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" && cat | tee $(date +"%Y%m%d%H%M%S").png) | nc -l -p 1881; done

I used this curl command to test it and was successful:

curl -X POST --data-binary @your_image.png http://localhost:1881

These commands should enable you to get the scripts operational.