有没有办法从 Windows 命令行检查特定端口的状态?我知道我可以使用 netstat 检查所有端口,但 netstat 很慢并且查看特定端口可能不会。
这是端口查找的简单解决方案...
在cmd中:
netstat -na | find "8080"
在bash中:
netstat -na | grep "8080"
在 PowerShell 中:
netstat -na | Select-String "8080"
您可以将
netstat
与 -np
标志和管道一起使用到 find
或 findstr
命令。
基本用法如下:
netstat -np <protocol> | find "port #"
例如,要检查 TCP 上的端口 80,您可以这样做:
netstat -np TCP | find "80"
最终给出以下类型的输出:
TCP 192.168.0.105:50466 64.34.119.101:80 ESTABLISHED
TCP 192.168.0.105:50496 64.34.119.101:80 ESTABLISHED
如您所见,这仅显示 TCP 协议端口 80 上的连接。
我用:
netstat –aon | find "<port number>"
此处
-o
选项用于显示 进程标识符 (PID)。
现在您可以使用此进程 ID 执行任何您需要的操作。例如,要终止进程,请使用:
taskkill /F /pid <process ID>
当我遇到 WAMP apache 问题时,我使用此代码来查找哪个程序正在使用端口 80。
netstat -o -n -a | findstr 0.0:80
3068
是PID,所以我可以从任务管理器找到它并停止该进程。
如其他地方所述:使用 netstat 和适当的开关,然后使用 find[str] 过滤结果
最基本的:
netstat -an | find ":N"
或
netstat -a -n | find ":N"
要查找您可以使用的外国端口:
netstat -an | findstr ":N[^:]*$"
要查找您可能使用的本地端口:
netstat -an | findstr ":N.*:[^:]*$"
其中 N 是您感兴趣的端口号。
-n
确保所有端口均为数字,即不会以翻译为服务名称的形式返回。
-a
将确保您搜索所有连接(TCP、UDP、侦听...)
在
find
字符串中,您必须包含冒号作为端口限定符,否则该数字可能与本地或外部地址匹配。
您可以根据需要使用其他 netstat 开关进一步缩小搜索范围...
进一步阅读(^0^)
netstat /?
find /?
findstr /?
netstat -a -n | find /c "10.240.199.9:8080"
它将为您提供特定 IP 和端口(服务器端口号)上活动的套接字数量
它将为您提供特定 IP 上的所有活动套接字:
netstat -an | find "172.20.1.166"
改进@EndUzr的回复:
要查找外部端口(IPv4 或 IPv6),您可以使用:
netstat -an | findstr /r /c:":N [^:]*$"
要查找本地端口(IPv4 或 IPv6),您可以使用:
netstat -an | findstr /r /c:":N *[^ ]*:[^ ]* "
其中 N 是您感兴趣的端口号。“/r”开关告诉它将其作为正则表达式处理。 “/c”开关允许 findstr 在搜索字符串中包含空格,而不是将空格视为搜索字符串分隔符。这个增加的空间可以防止较长的端口被滥用 - 例如,“:80”与“:8080”以及其他端口修改问题。
列出本地 RDP 服务器的远程连接,例如:
netstat -an | findstr /r /c:":3389 *[^ ]*:[^ ]*"
或者查看谁正在接触您的 DNS:
netstat -an | findstr /r /c:":53 *[^ ]*:[^ ]*"
如果您想排除仅限本地的端口,您可以使用一系列带有“/v”的例外并使用反斜杠转义字符:
netstat -an | findstr /v "0.0.0.0 127.0.0.1 \[::\] \[::1\] \*\:\*" | findstr /r /c:":80 *[^ ]*:[^ ]*"
这会对你有帮助
netstat -atn | grep <port no> # For tcp
netstat -aun | grep <port no> # For udp
netstat -atun | grep <port no> # For both
对于 Windows 8 用户:打开命令提示符,键入 netstat -an |找到“您的端口号”,输入。
如果回复类似于LISTENING,则该端口正在使用中,否则它是免费的。
对于端口 80,命令为:netstat -an |找到“80” 对于端口 n,命令为:netstat -an |找到“n”
这里,netstat是对你机器的指令
-a :显示所有连接和监听端口 -n :以数字格式显示所有地址和指令(这是必需的,因为 -a 的输出可以包含机器名称)
然后,使用 find 命令来“模式匹配”前一个命令的输出。
在 RHEL 7 中,我使用此命令来过滤处于 LISTEN 状态的几个端口:
sudo netstat -tulpn | grep LISTEN | egrep '(8080 |8082 |8083 | etc )'
如果您需要检查多个端口 - 最简单的方法是使用 findstr 和多个字符串进行搜索:
findstr /C:":80 " /C:":443 " /C:":8080"
端口号后面的空格很重要,没有空格findstr将选择以“例如”开头的所有内容。来自“:80”。 在我的例子中,完整的命令如下所示:
netstat -an | findstr /C:":80 " /C:":443 " /C:":8080"
对我来说,以下命令用于检查特定端口状态
netstat -an |找到“:8000”
netstat -aon |查找str“:8000”
在Linux下: 要查找外国端口,您可以使用:
netstat -anp |grep port|awk '{ print $5 }' |grep port
要查找您可能使用的本地端口:
netstat -anp |grep port|awk '{ print $4 }' |grep port
对于精确匹配[Windows命令提示符]
netstat -aon | findstr "\<5000\>"
使用lsof命令“lsof -i tcp:port #”,这里是一个例子。
$ lsof -i tcp:1555
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 27330 john 121u IPv4 36028819 0t0 TCP 10.10.10.1:58615->10.10.10.10:livelan (ESTABLISHED)
java 27330 john 201u IPv4 36018833 0t0 TCP 10.10.10.1:58586->10.10.10.10:livelan (ESTABLISHED)
java 27330 john 264u IPv4 36020018 0t0 TCP 10.10.10.1:58598->10.10.10.10:livelan (ESTABLISHED)
java 27330 john 312u IPv4 36058194 0t0 TCP 10.10.10.1:58826->10.10.10.10:livelan (ESTABLISHED)
此命令将显示所有端口及其目标地址:
netstat -f