windows 检查远程主机上一个端口的状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1168317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check status of one port on remote host
提问by
I need a command line that can check the port status on a remote host. I tried ping xxx.xxx.xxx.xxx:161
but it doesn't recognize the "host". I thought it was a "good" answer until I did the same command against a host I know has that port open. This is for a batch file on Windows that will check the status of the remote port then run a command that uses that remote port for information, then the remote port check command again, then the command that uses that port on the next server for information, and so on. I've looked everywhere and thought the ping might do it, but there must be various versions of ping, I suppose as the server I am doing this on does not show that option.
我需要一个命令行来检查远程主机上的端口状态。我试过了,ping xxx.xxx.xxx.xxx:161
但它不能识别“主机”。我认为这是一个“好”的答案,直到我对我知道该端口打开的主机执行了相同的命令。这是用于 Windows 上的批处理文件,它将检查远程端口的状态,然后运行使用该远程端口获取信息的命令,然后再次运行远程端口检查命令,然后运行在下一个服务器上使用该端口获取信息的命令, 等等。我到处寻找并认为 ping 可能会这样做,但是必须有各种版本的 ping,我想因为我正在执行此操作的服务器没有显示该选项。
Just for chuckles, I tried a web-based remote port checker from a website - and the results were correct for both the "problem" server and the correct server. However, I can't use that in a batch run with 500+ server IPs in it.
只是为了笑一笑,我从网站上尝试了一个基于 Web 的远程端口检查器 - 结果对于“问题”服务器和正确的服务器都是正确的。但是,我不能在包含 500 多个服务器 IP 的批处理中使用它。
Is there something I can do that is simple? My Perl skills are extremely rusty (use it or lose it), don't know any other Windows based languages except batch. Unix is my skill, but this must be executed from Widows Server 2003.
有什么我可以做的很简单的事情吗?我的 Perl 技能非常生疏(使用它或失去它),除了批处理之外不知道任何其他基于 Windows 的语言。Unix 是我的技能,但这必须从 Widows Server 2003 执行。
回答by Glenn
You seem to be looking for a port scanner such as nmapor netcat, both of which are available for Windows, Linux, and Mac OS X.
您似乎正在寻找端口扫描器,例如nmap或netcat,它们都可用于 Windows、Linux 和 Mac OS X。
For example, check for telnet on a known ip:
例如,检查已知 IP 上的 telnet:
nmap -A 192.168.0.5/32 -p 23
For example, look for open ports from 20 to 30 on host.example.com:
例如,在 host.example.com 上查找从 20 到 30 的开放端口:
nc -z host.example.com 20-30
回答by Naveen Yedugani
In Command Prompt, you can use the command telnet.. For Example, to connect to IP 192.168.10.1 with port 80,
在命令提示符中,您可以使用命令 telnet.. 例如,使用端口 80 连接到 IP 192.168.10.1,
telnet 192.168.10.1 80
To enable telnet in Windows 7 and above click. From the linked article, enable telnet through control panel -> programs and features -> windows features -> telnet client, or just run this in an admin prompt:
要在 Windows 7 及更高版本中启用 telnet,请单击。从链接的文章中,通过控制面板 -> 程序和功能 -> Windows 功能 -> telnet 客户端启用 telnet,或仅在管理提示符下运行:
dism /online /Enable-Feature /FeatureName:TelnetClient
回答by kenorb
For scripting purposes, I've found that curl
command can do it, for example:
出于脚本目的,我发现curl
command 可以做到,例如:
$ curl -s localhost:80 >/dev/null && echo Connected. || echo Fail.
Connected.
$ curl -s localhost:123 >/dev/null && echo Connected. || echo Fail.
Fail.
Possibly it may not won't work for all services, as curl
can return different error codes in some cases (as per comment), so adding the following condition could work in reliable way:
可能它可能不适用于所有服务,因为curl
在某些情况下会返回不同的错误代码(根据评论),因此添加以下条件可以可靠地工作:
[ "$(curl -sm5 localhost:8080 >/dev/null; echo $?)" != 7 ] && echo OK || echo FAIL
Note: Added -m5
to set maximum connect timeout of 5 seconds.
注意:添加-m5
以将最大连接超时设置为 5 秒。
If you would like to check also whether host is valid, you need to check for 6
exit code as well:
如果您还想检查主机是否有效,您还需要检查6
退出代码:
$ curl -m5 foo:123; [ $? != 6 -a $? != 7 ] && echo OK || echo FAIL
curl: (6) Could not resolve host: foo
FAIL
To troubleshoot the returned error code, simply run: curl host:port
, e.g.:
要对返回的错误代码进行故障排除,只需运行:curl host:port
,例如:
$ curl localhost:80
curl: (7) Failed to connect to localhost port 80: Connection refused
See: man curl
for full list of exit codes.
请参阅:man curl
有关退出代码的完整列表。
回答by Leo
Press Windows+ Rtype cmd
and Enter
按Windows+R键入cmd
和Enter
In command prompt type
在命令提示符下键入
telnet "machine name/ip" "port number"
If port is not open, this message will display:
如果端口未打开,将显示此消息:
"Connecting To "machine name"...Could not open connection to the host, on port "port number":
Otherwise you will be take in to opened port (empty screen will display)
否则您将进入已打开的端口(将显示空白屏幕)
回答by minhas23
Use nc command,
使用 nc 命令,
nc -zv <hostname/ip> <port/port range>
For example,
nc -zv localhost 27017-27019
or
nc -zv localhost 27017
You can also use telnet command
您也可以使用 telnet 命令
telnet <ip/host> port
回答by caskey
nc or 'netcat' also has a scan mode which may be of use.
nc 或“netcat”也有可能有用的扫描模式。
回答by Trueblood
I think you're looking for Hping (http://www.hping.org/), which has a Windows version.
我认为您正在寻找具有 Windows 版本的Hping ( http://www.hping.org/)。
"The interface is inspired to the ping(8) unix command, but hping isn't only able to send ICMP echo requests. It supports TCP, UDP, ICMP..."
“该接口的灵感来自 ping(8) unix 命令,但 hping 不仅能够发送 ICMP 回显请求。它支持 TCP、UDP、ICMP ......”
It's also very useful if you want to see where along a route that a TCP port is being blocked (like by a firewall), where ICMP might not be.
如果您想查看 TCP 端口被阻止(例如被防火墙)阻塞的路由,而 ICMP 可能不会被阻止,这也非常有用。
回答by kenorb
In Bash, you can use pseudo-device files which can open a TCP connection to the associated socket. The syntax is /dev/$tcp_udp/$host_ip/$port
.
在 Bash 中,您可以使用伪设备文件,这些文件可以打开到相关套接字的 TCP 连接。语法是/dev/$tcp_udp/$host_ip/$port
.
Here is simple example to test whether Memcached is running:
以下是测试 Memcached 是否正在运行的简单示例:
</dev/tcp/localhost/11211 && echo Port open || echo Port closed
Here is another test to see if specific website is accessible:
这是另一个测试,看看特定网站是否可以访问:
$ echo "HEAD / HTTP/1.0" > /dev/tcp/example.com/80 && echo Connection successful.
Connection successful.
For more info, check: Advanced Bash-Scripting Guide: Chapter 29. /dev
and /proc
.
欲了解更多信息,请查看:高级Bash脚本编程指南:29章/dev
及/proc
。
Related: Test if a port on a remote system is reachable (without telnet)at SuperUser.
相关:在 SuperUser测试远程系统上的端口是否可访问(无需 telnet)。
For more examples, see: How to open a TCP/UDP socket in a bash shell(article).
有关更多示例,请参阅:如何在 bash shell 中打开 TCP/UDP 套接字(文章)。