天天看點

管道的一個應用: 将一個tcp端口的輸出轉移到另外一個tcp端口而輸出

  案例: 有一個應用程式需要把資料輸出給遠端tcp端口44441, 為了安全, 資料會再次轉發到端口44442,進而使得連接配接到端口44442的應用可以實時接收到資料。

   為此,在遠端tcp主機編寫了shell腳本如下:

#!/bin/bash -l

pipe=/tmp/testpipe

ps -ef | grep -e '44441' -e '44442' | grep -v 'grep' | awk '{print $2;}' | xargs kill -9 > /dev/null 2>&1
trap "rm -f $pipe" EXIT

if [[ ! -p $pipe ]]; then
    mkfifo $pipe
fi

ncat -lk -p 44441 0<"$pipe" | ncat -lk -p 44442 | tee $pipe &