可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
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.
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.
回答1:
You seem to be looking for a port scanner such as nmap or netcat, both of which are available for Windows, Linux, and Mac OS X.
For example, check for telnet on a known ip:
nmap -A 192.168.0.5/32 -p 23
For example, look for open ports from 20 to 30 on host.example.com:
nc -z host.example.com 20-30
回答2:
In Command Prompt, you can use the command telnet..
For Example, to connect to IP 192.168.10.1 with port 80,
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:
dism /online /Enable-Feature /FeatureName:TelnetClient
回答3:
For scripting purposes, I've found that curl
command can do it, for example:
$ 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 -sm5 localhost:8080 >/dev/null; echo $?)" != 7 ] && echo OK || echo FAIL
Note: Added -m5
to set maximum connect timeout of 5 seconds.
If you would like to check also whether host is valid, you need to check for 6
exit code as well:
$ 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 localhost:80
curl: (7) Failed to connect to localhost port 80: Connection refused
See: man curl
for full list of exit codes.
回答4:
Press Windows + R type cmd
and 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)
回答5:
Use nc command,
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 <ip/host> port
回答6:
nc or 'netcat' also has a scan mode which may be of use.
回答7:
I think you're looking for Hping (http://www.hping.org/), which has a Windows version.
"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..."
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.
回答8:
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
.
Here is simple example to test whether Memcached is running:
</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
.
Related: Test if a port on a remote system is reachable (without telnet) at SuperUser.
For more examples, see: How to open a TCP/UDP socket in a bash shell (article).