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.
Press Windows + R type
cmd
and EnterIn command prompt type
If port is not open, this message will display:
Otherwise you will be take in to opened port (empty screen will display)
nc or 'netcat' also has a scan mode which may be of use.
In Command Prompt, you can use the command telnet.. For Example, to connect to IP 192.168.10.1 with port 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:
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:
Here is another test to see if specific website is accessible:
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).
For scripting purposes, I've found that
curl
command can do it, for example: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: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:To troubleshoot the returned error code, simply run:
curl host:port
, e.g.:See:
man curl
for full list of exit codes.I think you're looking for Hping (http://www.hping.org/), which has a Windows version.
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.