通过telnet连接到netcat并关闭计算机

时间:2018-07-31 07:19:55

标签: bash telnet netcat

我正在使用以下bash脚本开始侦听端口8585上的连接。通过telnet连接并发送文本“ shutdown”时,我希望主机关闭。截至目前,我正在使用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"

当我尝试通过telnet连接到它时,我得到的只是响应:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Welcome.
Connection closed by foreign host.

你们当中有人知道为什么连接会立即关闭吗?你有什么想法吗?

我从这里获得了示例代码:https://askubuntu.com/questions/873788/bash-read-lines-from-netcat-connection#873794

1 个答案:

答案 0 :(得分:1)

删除(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"

请记住,只会打破单词shutdown的循环,而nc会继续收听