Strange behaviour of netcat with UDP

前端 未结 4 588
天命终不由人
天命终不由人 2020-12-13 13:36

I noticed a strange behaviour working with netcat and UDP. I start an instance (instance 1) of netcat that listens on a UDP port:

nc -lu -p 10000
         


        
4条回答
  •  一整个雨季
    2020-12-13 13:48

    As the accepted answer explains, ncat appears not to support --keep-open with the UDP protocol. However, the error message which it prints hints at a workaround:

    Ncat: UDP mode does not support the -k or --keep-open options, except with --exec or --sh-exec. QUITTING.
    

    Simply adding --exec /bin/cat allows --keep-open to be used. Both input and output will be connected to /bin/cat, with the effect of turning it an "echo server" because whatever the client sends will be copied back to it.

    To do something more useful with the input, we can use the shell's redirection operators (thus requiring --sh-exec instead of --exec). To see the data on the terminal, this works:

    ncat -k -l -u -p 12345 --sh-exec "cat > /proc/$$/fd/1"
    

    Caveat: the above example sends data to the stdout of ncat's parent shell, which could be confusing if combined with additional redirections. To simply append all output to a file is more straightforward:

    ncat -k -l -u -p 12345 --sh-exec "cat >> ncat.out"
    

提交回复
热议问题