Question

I'm having an issue and have had difficulties finding a solution online. My code tails a log file and connects with an external server using netcat when the file is written to.

Here's a quick example of my working code, I'll explain the issue afterwards.

# function that watches input for '$servertag' variable
serverwatch() {
  while read data
  do
    if [ `strindex "$data" "$servertag"` -ge 0 ]; then
      ....
      nc <server_ip> 1234
      ....
    fi
  done
}

# Tail the log file and pipe to functions
tail -f messages | serverwatch

The issue is that though the connection is successfully established, the data from the 'messages' file is being piped through to netcat. I do some basic parsing on the data locally, but I don't know how to stop the data from being sent through netcat.

I should quickly note that I am looking to have this work on most DD-WRT builds, so I am trying to not install any new packages. Also, bash seems to be broken/minimal on the build I have, so I'm trying to avoid bash.

Please let me know if you have any questions, solutions, or references to other questions that will help with this issue.

Thanks,

James

Était-ce utile?

La solution

netcat inherits the stdin descriptor. Redirect it from somewhere else instead, such as:

nc <server_ip> 1234 < /dev/null

netcat unfortunately closes the connection when it reaches end-of-input, so if you want the connection to remain open, you could use the -d flag.

nc -d <server_ip> 1234 < /dev/null
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top