I'm using the following bash script to start listening to connections on port 8585. When connected by telnet and sending the text "shutdown" I would like the host computer to shut down. As of now I'm using my mac.
#!/bin/bash
echo "Start listening on port 8585..."
while read line
do
if [ "$line" == 'shutdown' ]; then
# Execute shutdown now on the computer.
break
else
echo "$line"
fi
done < <((echo "Welcome.") | nc -k -l 8585)
echo "Good bye"
When I try to connect to it through telnet I just get this as response:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Welcome.
Connection closed by foreign host.
Do any of you guys know why the connection gets closed straight away? Do you have any ideas?
I got the example code from here: https://askubuntu.com/questions/873788/bash-read-lines-from-netcat-connection#873794
Remove the (echo "Welcome.") |
:
#!/bin/bash
echo "Start listening on port 8585..."
while read line
do
if [ "$line" == 'shutdown' ]; then
# Execute shutdown now on the computer.
break
else
echo "$line"
fi
done < <(nc -k -l 8585)
echo "Good bye"
Keep in mind that will only break the loop with the word shutdown
but nc
will continue listening