Howto create a permanent client connection with netcat?

12,267

Solution 1

Two separate processes: One that copies result.txt to netcat. Result.txt is fed via another process.

echo -n >result.txt
tail -f result.txt | nc ip port &
while true
do
    read_folder()
    process_data() > result.txt
    wait 10 sec
done

Solution 2

Just put the redirection outside the loop.

while true; do
    read_folder
    process_data
    sleep 10
done | netcat $ip $port

If you need to output to the loop's standard output from inside the loop, divert it through another file descriptor.

{
  while true; do
      read_folder
      echo tick >&3
      process_data
      sleep 10
  done | netcat $ip $port
} 3>&1
Share:
12,267

Related videos on Youtube

jmbouffard
Author by

jmbouffard

Updated on September 18, 2022

Comments

  • jmbouffard
    jmbouffard almost 2 years

    I'm writing a bash script that constantly read a folder in a loop to send data to a server at a defined time interval. I'm using netcat as the tool to connect to the server and send the data. My pseudo code would look like:

    while true
    do
        read_folder()
        process_data() > result.txt
        cat result.txt > netcat ip port
        wait 10 sec
    done
    

    My only problem is that in the scenario the client connects and disconnects the TCP/IP connection to the server every time. I would prefer to establish the connection at the start of the script and close it at the end.

    Is there any way this could be done with command line tools in a bash script?

    • bbaja42
      bbaja42 almost 13 years
      Actually, I prefer your current setup. In case of long connection, there is bigger chance of connection loss. This way, in case of connection loss, there is automatic retry after 10 seconds
    • BrettRobi
      BrettRobi almost 13 years
      Take a look at socat, it's a netcat replacement that has a built-in functionality to respawn pipes after they've finished.
  • jmbouffard
    jmbouffard almost 13 years
    I thought about something like that but the first process will return when it reaches the end of the file, it will not wait for the other process to write to it.
  • mm2001
    mm2001 almost 13 years
    @jmbouffard: fixed. tail -f will continue to read the file even after it reaches EOF.
  • jmbouffard
    jmbouffard almost 13 years
    Thanks it works amazingly well with tail -f. I'm even able to use a fifo pipe instead of a "growing" result.txt file for the transfer. Answer accepted.
  • tcoolspy
    tcoolspy almost 13 years
    @Kevim: The edit @Gilles made using : >result.txt was fine, it does not leave a newline in the file. Test it yourself.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 13 years
    echo -n is fine too, as long as your shell supports it (some shells print -n) — so : is preferable (it works everywhere). And you do need tail -c +1; without it, if tail is slow to start, it will omit all but 10 lines of what's already in the file.