Is it possible to ping an address:port?

Solution 1:

You can use Paping, a cross-platform TCP port testing, emulating the functionality of ping (port ping)

(see also Github as code.google.com has been depreciated)

paping -p 80 google.com

Solution 2:

Ports are a concept of UDP and TCP. Ping messages are technically referred to as ICMP Echo Request and ICMP Echo Reply which are part of ICMP. ICMP, TCP, and UDP are "siblings"; they are not based on each other, but are three separate protocols that run on top of IP.

Therefore you can not ping a port. What you can do, is use a port scanner like nmap.

nmap -p 80 onofri.org

You can also use telnet onofri.org 80, as suggested in one of the other answers (It will give an error if the port is closed or filtered).

Solution 3:

I use Telnet, since its built into lots of platforms with no additional downloads.

Just use the telnet command to connect to the port you want to test. If you get the message below, or a message from the service itself, then the port is alive.

Minty16 ~ $ telnet localhost 139
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

If you know the command sequence for the service you are connecting to, you can type a command (HTTP/FTP GET for instance) and observe the response and output in the terminal. This is very useful for testing the service itself, as it will show you error information sent to the client, like HTTP 500 errors.

If you get a message that the connection was refused, the port is closed.

Minty16 ~ $ telnet localhost 5000
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

Solution 4:

Yes, use HPing to do that:

$ sudo hping -S -p 80 google.com
HPING google.com (p5p1 77.237.27.37): S set, 40 headers + 0 data bytes
len=46 ip=77.237.27.37 ttl=58 id=25706 sport=80 flags=SA seq=0 win=29200 rtt=7.5 ms
len=46 ip=77.237.27.37 ttl=58 id=25707 sport=80 flags=SA seq=1 win=29200 rtt=7.4 ms
len=46 ip=77.237.27.37 ttl=58 id=25708 sport=80 flags=SA seq=2 win=29200 rtt=8.5 ms
len=46 ip=77.237.27.37 ttl=58 id=25709 sport=80 flags=SA seq=3 win=29200 rtt=7.8 ms
^C
--- google.com hping statistic ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 7.4/7.8/8.5 ms

Note that it needs root privileges (or SELinux capabilities) to create raw IP packets, just like ping (which is most likely suid on your system).

Solution 5:

You can use netcat to connect to a specific port to see if you get a connection. The -v flag will increase the verbosity to show whether the port is open or closed. The -z flag will cause netcat to quit once it has a connection. You can then use the exit codes through $? to see whether or not the connection was established or not.

$ nc -zv localhost 22
localhost [127.0.0.1] 22 (ssh) open
$ echo $?
0
$ nc -zv localhost 23
localhost [127.0.0.1] 23 (telnet) : Connection refused
$ echo $?
1

Additionally, you can use mtr with the -T flag for tcp and the -P flag to specify a port. This will do something similar to a traceroute over TCP instead of just ICMP. This may be overkill, however.

sigh I have to edit to add this bit, since we cannot put code in comments. Knoppix may being doing something different with its version of netcat, but this is what I get off of Linux Mint

$ date;nc -z -w 1 www.google.com 8000;date
Fri Jun 20 15:55:26 PDT 2014
Fri Jun 20 15:55:27 PDT 2014
$ date;nc -z -w 4 www.google.com 8000;date
Fri Jun 20 15:55:33 PDT 2014
Fri Jun 20 15:55:37 PDT 2014

$ nc -h
[v1.10-40]