1

I have a file on my server that has HL7 messages spread out by a space. Example below. I am wanting to send one of these messages at a time in a continual loop until stopping the script with ctrl+c. I am pretty new to netcat but when I try to netcat directly using nc -l 10004 I get:

ncat: bind to :::10004: Address already in use. QUITTING.

There is a Mirth application on the appliance listening on this port already. I have no problem running the command on another appliance altogether sending it to this one if that is the only way but I need assistance sending to the IP using netcat and sending one message at a time with a timed gap between them.

MSH....
.......
.......

MSH....
.......

MSH....
.......
.......
.......

The file I am pulling from has multiple messages with more than one line but they are separated by a line break. (space)

  • You probably can batch these instead of sending one at a time if you agreed on a low level protocol to wrap the messages. – Peter Turner Jun 10 '15 at 20:12
  • I'm wanting to send them through one at a time that way I can see how they are processing within our system. By keeping them flowing through one at a time say every 3 seconds I can make changes and see how they affect processing. – Jeff Jun 10 '15 at 21:01
0

If you have the Mirth application already listening on port 10004 then you can't really have netcat also listening on that same port (the -l flag denotes listen). Perhaps you intended to write to the port rather than listen on it:

# Connect once and write many messages
( while :; do echo MSH....; sleep 3; done ) | nc -vvv localhost 10004

or

# Connect for each message
( while :; do echo MSH.... | nc -vvv localhost 10004; sleep 3; done )

Be aware that there are several different implementations of netcat. Every implementation seems to handle a different subset of flags...


Based on the new information in your question, here's another possible solution:

MESG=
while IFS= read -r LINE || test -n "$MESG"
do
    if test -n "$LINE"
    then
        # Build the message block
        test -z "$MESG" && MESG="$LINE" || MESG=$(printf "%s\n%s" "$MESG" "$LINE")
        continue
    fi

    # Send the message block to the service
    echo "$MESG" | nc -vvv localhost 10004
    MESG=
    sleep 3
done < /path/to/your/messagefile.txt
  • I tried while :; do echo HL7.txt | nc -vvv localhost 10004; sleep 3; done because I have the different HL7 messages in this file but it sends HL7.txt not what's inside. To verify this I sent it to another port and listened on this port. Each HL7 message is different but is separated by a space so I am hoping to send each individually and in a loop. – Jeff Jun 11 '15 at 11:30
  • @Jeff you have a file containing several lines, one line per HL7 message? Or several files each containing one message? Or something different? You didn't mention this in your Question so please update it, and then I can update my Answer to support your new information. – roaima Jun 11 '15 at 12:06
  • I updated the question thanks for any assistance! – Jeff Jun 11 '15 at 12:42
  • @Jeff answer updated to incorporate the new information – roaima Jun 11 '15 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.