Netcat TCP Programming with Bash

后端 未结 1 875
离开以前
离开以前 2021-01-18 06:20

I\'m attempting to do some basic TCP client communication by using strictly bash scripting. I have netcat at my disposal and so I\'ve written this loop so far:



        
相关标签:
1条回答
  • 2021-01-18 07:13

    With Bash≥4 you can use coproc:

    #!/bin/bash
    
    coproc nc { nc 10.0.0.104 4646; }
    
    while [[ $nc_PID ]] && IFS= read -r -u${nc[0]} line; do
        case $line in
            ('{"cmd": 1}')
                printf >&${nc[1]} '%s\n' '{"error": 0}'
                ;;
            (*)
                printf >&2 '%s\n' "Received line:" "$line"
                ;;
        esac
    done
    

    This avoids using temporary fifos. Without coproc, I guess the only option left is to use fifos explicitly. Here's an example:

    #!/bin/bash
    
    mkfifo fifo_in
    
    while IFS= read -r line; do
        case $line in
            ('{"cmd": 1}')
                printf '%s\n' '{"error": 0}'
                ;;
            (*)
                printf >&2 '%s\n' "Received line:" "$line"
                ;;
        esac
    done < <(nc 10.0.0.104 4646 < fifo_in) > fifo_in
    

    For this, you'll have to manage the creation and deletion of the fifo: you'll need to create a temporary directory with mktemp, in there create the fifo, then trap your script so that on exit everything is cleaned.


    /dev/tcp

    If your Bash has been compiled with net redirections support, you can get rid of nc and of the fifos and coprocesses altogether:

    #!/bin/bash
    
    # open TCP connection, available on file descriptor 3
    exec 3<> /dev/tcp/10.0.0.104/4646 || exit
    
    while IFS= read -r -u3 line; do
        case $line in
            ('{"cmd": 1}')
                printf >&3 '%s\n' '{"error": 0}'
                ;;
            (*)
                printf >&2 '%s\n' "Received line:" "$line"
                ;;
        esac
    done
    

    This is very likely the sweetest solution!

    0 讨论(0)
提交回复
热议问题