用于在sh脚本

时间:2016-04-27 19:00:56

标签: shell sockets tcp netcat

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

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

的服务器上的脚本中运行一个函数

我将server_port编码为'4444'

文件名:run_hangman

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

文件名: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”,就好像我按下输入它转到一个新行。

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,它将允许我再发送一个echo,然后在服务器上关闭它。我需要得到我认为是一个while语句并让它听取像“playermsg'已经启动游戏'这样的输入并让它实际上在服务器上运行该函数。

我是否可以让它运行,几乎看起来它必须在后台?我一直在使用nc(1)作为参考,有些网站试图尝试使用-d,但也无效。

1 个答案:

答案 0 :(得分:0)

我明白了。我确实需要一段时间的陈述。

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