I'm new to shell programming and I have created a script that opens a connection to a server of mine. I want to have this script listen for an input from a client node and use that to run a function.
This is my process. Run script > opens listener > on second computer use netcat to connect > run a function in the script on the server called nodefunction
I have server_port coded to '4444'
File name: run_hangman
nc -l -k -v -p 4444 | bash hangman
File name: 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?
I run the run_hangman file and then I connect to it via my node computer. I enter the following line and echo "1 2 3" because that is what I need. I also can't enter "1 2 3" directly into the window running "run_hangman" as if I press enter it goes to a new line.
echo "1 2 3" >/dev/tcp/xxx.xxx.xxx.xxx/4444
The server shows that it connected
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
Now once it gets to openSocket it will allow me to send one more echo and then it closes on the server. I need to get what I presume is a while statement and have it listen for an input like "playermsg 'has started a game'" and have it actually run that function on the server.
Will I be able to get this to run, almost seems like it has to be in the background? I've been using nc(1) for reference and some websites said to try -d and that didn't work either.