0

I'm trying to take data I receive over a netcat connection to a script that will send each line to another server via a curl post command.

Heres where I am.

This works: nc -lk 9272 > test.log

Each line received is in the log as expected

This doesn't work:

nc -lk 9272 | ./senddata.sh

Expect it to send the lines to this script:

#! /bin/bash
echo "Received Line!"

line=$1

cart=${line:0:7}
type=${line:7:4}
title=${line:14:28}
curl -d "cart=$cart&type=$type&title=$title" -X POST http://server/update

Nor does this ./senddata.sh 9272

senddata.sh

#!/bin/bash

echo "Started listening on port $1 ..."

while read line
do
    if [ "$line" == 'exit' ]; then
        echo "Received 'exit'"
        break
    else
        echo "Received Line!"
        cart=${line:0:7}
        type=${line:7:4}
        title=${line:14:28}
        curl -d "cart=$cart&type=$type&title=$title" -X POST http://server/update
    fi
done < <((echo "Welcome.") | nc -kl $1)
echo "Good bye"

The end goal is to receive the data and then send it to my app via a post.

ahackney
  • 137
  • 7

1 Answers1

2

Try using your second script with the first option (but remove the redirection at the end of the while loop).

Here is a sample I have just tested:

root@kube-01-01:~# cat test.sh 
#!/bin/bash

while read line
do
        echo "Received Line!"
        echo $line
done
echo "Good bye"

root@kube-01-01:~# nc -l 8090 | ./test.sh
Received Line!
test
Received Line!
hello there
Good bye

In your first script, you were receiving data on standard input (stdin), via a pipe. However, you were trying to read it using $1 (which refers to the first command-line argument passed to the script).

Bogd
  • 291
  • 1
  • 3
  • 7
  • So this works once. When I first run the script the first line of data is recieved in to the script and then is sent via curl to my app. The following lines do not though. The script does run and then the post is made to the app but its all blank as though the $line is blank. – ahackney Nov 11 '18 at 16:27
  • It did work for me - as you can see, I did send multiple lines over the netcat connection. There are many things that could change here - netcat version (there are several of them around), the remote side closing the connection, etc. Without knowing the details about how you actually send that data, I do not know how to provide additional help... – Bogd Nov 11 '18 at 17:57
  • I think the problem is with the client side sending data, its an old system and its not very flexible but I can put a LF character in front of the line that it sends and it seems to fix the send but then it sends two lines cause it sends one after as well. Its generating two posts each time a line is sent. One blank then one with the data I need. I'm looking now to see if there is a way to take the line sent and add a return to the line in the script to force it through each time. Might have to do it before I pipe it through to the script though. – ahackney Nov 11 '18 at 18:18
  • To avoid duplicate calls to curl, you could check to see if the line that bash reads is blank - something like `if [ -z $line ]; then`... – Bogd Nov 11 '18 at 20:13
  • Nice. I'll try that – ahackney Nov 11 '18 at 20:19