Tcp
用於在 sh 腳本中呼叫函式的 netcat 守護程序
我是 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”,就好像我按下輸入鍵一樣。
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 ‘已經開始遊戲’”之類的輸入,並讓它在伺服器上實際執行該功能。
我能否讓它執行,幾乎看起來它必須在後台?我一直在使用nc(1)作為參考,一些網站說可以嘗試
-d
,但也沒有用。
我想通了。我確實需要一個while語句。
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