通过终端持续监听 tcp 端口

Continuously listen to tcp port via terminal

是否可以连续监听一个端口?

我使用以下命令监听传入的 tcp 通知

sudo nc -l -p 999

但是一旦收到通知,我就必须使用相同的命令重新开始监听。是否可以在通知到达时无需重新启动命令即可监听端口,直到用户决定中止监听?

通过简单的 bash 脚本解决

#!/bin/bash

#Make Sure Script Is Ran As Root
if [ $(id -u) != 0 ]; then
    echo; echo -e "\e[1;31mScript must be run as sudo. Please Type \"sudo\" To Run As Root \e[0m"; echo    
exit 1
fi

echo "Enter port to listen"
read portL

while true;
do
    nc -l -p $portL
done
exit 0

感谢 dreamlax 的提示!

有点过时的问题,但在我的 Google 搜索中排在第一位。

为了netcat不在接收到第一个连接后立即关闭,您可以添加-k选项。

来自男人:

-k Forces nc to stay listening for another connection after its current connection is completed. It is an error to use this option without the -l option.

来源:https://superuser.com/a/708133/410908