netcat 守护进程,用于调用 sh 脚本中的函数

netcat 守护进程,用于调用 sh 脚本中的函数

我是 shell 编程新手,我创建了一个脚本来打开与我的服务器的连接。我想让这个脚本监听来自客户端节点的输入并使用它来运行一个函数。

这是我的过程。运行脚本>打开监听器>在第二台计算机上使用netcat连接>在服务器上运行脚本中名为nodefunction的函数

我将 server_port 编码为“4444”

文件名: run_hangman

nc -l -k -v -p 4444 | bash hangman

文件名称:刽子手

#!/bin/bash

msg_timeout=0
host_ows=1
server_port=4444
dubOws=xxx.xxx.xxx.xxx

initServer() {
    hostIP=`ip -o addr show dev "eth0" | awk '$3 == "inet" {print $4}' | sed -r 's!/.*!!; s!.*\.!!'`
    hostOws=`echo $hostIP | cut -d . -f 4`
} 
servermsg(){ #message    //if = --n, echos on same line
    if [ "$1" != "--n" ]
    then
        echo `date +"%T"` "[SERVER] "$1
    else
        echo -n `date +"%T"` "[SERVER] "
    fi
}
owsmsg(){    #message    //if = --n, echos on same line
    if [ "$1" != "--n" ]
    then
        echo `date +"%T"` "[OWS]    "$1
    else
        echo -n `date +"%T"` "[OWS]    "
    fi
}
playermsg() {
    if [ "$1" != "--n" ]
    then
        echo `date +"%T"` "[PLAYER]    "$1
    else
        echo -n `date +"%T"` "[PLAYER]    "
    fi
}
question(){  #question, read, example
    servermsg "$1"
    if [ -n "$3" ]
    then
        servermsg "$3"
    fi
    read $2
    echo ""
}
owsArray(){   #
    for targetOws in $player_list
    do
        owsArray+=("OWS"$targetOws)
    done
    echo -n ${owsArray[*]}
    echo
}
openSocket() {
    servermsg "Starting the Game Listener"
    servermsg "Opening Listener on port "$server_port
    #nc -k -l $server_port |bash    
    #nc -kl -q 1 -p $server_port # This should create the listener & This is where everything stops.
    servermsg "Now listening on port "$server_port
}

initServer
owsmsg "Starting server on OWS"$hostOws"..."

question "Enter all the OWSs that will play:" player_list "Example: 1 9 14 23"
echo $player_list
question "Type a category hint:" game_cat "Example: Type of Animal"
question "Type your word:" game_word "Example: zebra"
question "How many guesses:" game_guesses "Example: 7"

servermsg "OWS"$host_ows "has created a Hangman session"
servermsg "Players are:"; servermsg --n; owsArray
servermsg "Your word is "${#game_word}" letters long and players have "$game_guesses" guesses"
question "If this is all correct press enter, or CTRL+C to cancel"

openSocket

# I think I need a While script here to read the RAW input and run the playermsg function with the input?

我运行 run_hangman 文件,然后通过我的节点计算机连接到它。我输入以下行并回显“1 2 3”,因为这就是我需要的。我也无法直接在运行“run_hangman”的窗口中输入“1 2 3”,就像按 Enter 键它会转到新行一样。

echo "1 2 3" >/dev/tcp/xxx.xxx.xxx.xxx/4444

服务器显示已连接

Listening on [0.0.0.0] (family 0, port 4444)
14:52:24 [OWS]    Starting server on OWS225...
14:52:24 [SERVER] Enter all the OWSs that will play:
14:52:24 [SERVER] Example: 1 9 14 23
Connection from [xxx.xxx.xxx.xxx] port 4444 [tcp/*] accepted (family 2, sport 41564)
Connection closed, listening again.

1 2 3

现在,一旦它到达 openSocket,它将允许我再发送一个回显,然后它在服务器上关闭。我需要得到我认为是一个 while 语句,并让它监听诸如“playermsg '已开始游戏'”之类的输入,并让它在服务器上实际运行该函数。

我能否让它运行,几乎看起来它必须在后台运行?我一直在使用数控(1)仅供参考,有些网站说可以尝试-d,但也不起作用。

答案1

我明白了。我确实需要一段时间声明。

openSocket
while read -r value; do
    val=${value:10}
    if [[ "$value" == playermsg* ]]; then 
        val=${value:10}
        playermsg $val
    elif [[ "$value" == servermsg* ]]; then 
        val=${value:10}
        servermsg $val
    else
        echo "Returned "$value
        echo "Value was "$val
    fi
done

所以现在在第二台计算机上我只需运行

echo "playermsg testing" >/dev/tcp/xxx.xxx.xxx.xxx/4444

然后服务器显示如下:

15:29:36 [SERVER] Starting the Game Listener
15:29:36 [SERVER] Opening Listener on port 4440
15:29:36 [SERVER] Now listening on port 4440
15:29:37 [PLAYER] testing

相关内容