Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am not into networking, and I have the following question related to the Linux ping command.

Can I only ping an address? For example:

miner@raspberrypi ~ $ ping onofri.org
PING onofri.org (67.222.36.105) 56(84) bytes of data.
64 bytes from onofri.org (67.222.36.105): icmp_req=1 ttl=47 time=202 ms
64 bytes from onofri.org (67.222.36.105): icmp_req=2 ttl=47 time=206 ms
64 bytes from onofri.org (67.222.36.105): icmp_req=3 ttl=47 time=215 ms

Or can I also ping an address:port, for example: onofri.org:80?

If I try this one it doesn't work:

miner@raspberrypi ~ $ ping onofri.org:80
ping: unknown host onofri.org:80

Is it possible ping something like address:port? If it is possible, why doesn't what I tried work?

share|improve this question
8  
What are you trying to accomplish? Verifying correct service operation requires you to actually query the service and evaluate the response. A web server, could, for example, accept your connection, but return an error because of misconfiguration. –  Daniel B Jun 16 at 18:36
5  
Thats part of what I like about Telnet. you can connect to a webserver, and type GET /index.html HTTP\1.1 and see the 200 response (or error code) along with the resultant markup. –  Frank Thomas Jun 16 at 18:45
6  
@FrankThomas HTTP\1.1? Really? –  glglgl Jun 17 at 9:08
5  
@Navin Maybe HTTP/1.1... –  glglgl Jun 17 at 11:40
2  
@glglgl; what? you never put a hack where a slash should go? but you are correct, it should be a slash. –  Frank Thomas Jun 17 at 11:45

7 Answers 7

up vote 166 down vote accepted

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).

share|improve this answer
15  
+1 you understand it right. ICMP is built on top of IP, which has a concept of IP addresses but not ports. TCP and UDP are also on top of IP, and it is those protocols that add "ports". ICMP, TCP, and UDP are all at the same "level" in terms of the protocol they are carried by. –  Jason C Jun 16 at 18:42
8  
+1. Many, many people think that if they can't ping something they can't connect to it... but as you say, ICMP is different than TCP and UDP : if you intend to serve, say, a webpage on TCP 80, than you only need to open TCP 80 on the firewall, nothing else (so Ping to the same IP could (... should!) be blocked, for example) –  Olivier Dulac Jun 17 at 11:45
4  
Just to keep information accurate, ICMP is not a transport layer protocol like TCP or UDP. Like a number of protocols, it doesn't entirely fit in the network conceptual models, but it is generally considered a network/internetwork layer protocol as is IP. It has also sometimes been referred to as a L3.5 protocol. –  YLearn Jun 17 at 18:32
1  
@BenjiWiebe, agreed, but again, for accuracy the statement should be made which is why I added it. All that is said so far is that ICMP, TCP, and UDP are related. The highest voted comment even goes so far as to say they are all on the same "level." While this makes no difference to the average user, there are plenty of non-average users that visit this site. –  YLearn Jun 17 at 19:17
4  
@OlivierDulac I don't know if you should block it. Most webservers I know leave it open. ICMP makes the internet go around. Why would you want people thinking they can't reach you if they can? –  Cruncher Jun 19 at 16:12

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
share|improve this answer
4  
Also if the screen goes completely black it's a sign you're connected too. –  Tensigh Jun 17 at 6:30
    
Excellent suggestion. I used to use this to test if a web server was up when a web browser was not an easy option. –  Brandon Jun 17 at 13:17
    
@Brandon like when sshing into dev machines ^.^ –  Cruncher Jun 19 at 20:00
    
Developer tools in browsers is a lot easier, because they'll still send all the proper HTTP request headers but you can inspect the full HTTP response. Then again if you want to tailor the request too then telnet is the way to go. –  Lightness Races in Orbit Jun 22 at 11:30
    
I think all the answers here miss the most crucial piece of information. Technically, wouldn't it be possible to "ping" a port as follows? You can establish a TCP/UDP connection, and you count how many milliseconds it took to establish that connection. –  David Jun 23 at 8:06

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]
share|improve this answer
    
problem is if you try nc -z www.google.com 8000 it can take a long time- 5 seconds plus. If I do -w 1 then it takes 5 seconds. If I do -w 3 then it takes like 3 times longer.. 15 flashes of the cursor, 15 seconds. -w 1 is meant to be one second but tested on cygwin.,. 5 seconds. It's super fast when there is a server on the port but a bit slow when there isn't. –  barlop Jun 19 at 5:20
    
curse no newlines in comments... That seems to be a bug with cygwin. Testing with linux Mint shows a delay of however many seconds specified. ran this to test: date;nc-zw5 www.google.com 8000;date –  Falsenames Jun 19 at 19:38
    
I also upvoted @BenjiWiebe with the nmap requests. nc is easier to put into a script, but it's MUCH easier to use nmap visually. –  Falsenames Jun 19 at 19:45
    
Trying it in knoppix. nc -zv -w 1 www.google.com How do you cause it to just try once? When I try it yes the -w 1 works but every second it retries until I do Ctrl-C. –  barlop Jun 19 at 20:00
    
Not sure what the Knoppix version of 'nc -zv -w 1 www.google.com 80' is doing. That works fine on my system, querying once and dropping just like -z is supposed to do. Also, without the port number specified, mine fails out saying that there is no port specified. –  Falsenames Jun 20 at 21:36

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).

share|improve this answer

What you want to acomplish is test comunication ? or what respond to port 80 on that node?

Due to PING will try to acomplish comunication to a especific host trought the ICMP, nothing to do with ports.

probablity you need http://nmap.org/ to test port info and communications.

nmap -v -p 80 onofri.org
share|improve this answer
1  
Not sure which version of nmap you're using. On my system, it requires -v and -p to be separated like 'nmap -v -p 80 onofri.org'. This is because -v and -vv mean different things, -v taking more v's as an argument. –  Falsenames Jun 20 at 0:31
    
thanks.. sorry for not testing it =( –  Quijote Shin Jun 20 at 14:03

Alternatively, You could use Paping

Usage :

paping www.google.com -p 80 -c 4
share|improve this answer

You could also use nping (part of nmap):

$ nping -p 80 localhost

Starting Nping 0.6.00 ( http://nmap.org/nping ) at 2014-06-23 11:57 CEST
SENT (0.0015s) Starting TCP Handshake > localhost:80 (127.0.0.1:80)
RECV (0.0016s) Handshake with localhost:80 (127.0.0.1:80) completed
SENT (1.0027s) Starting TCP Handshake > localhost:80 (127.0.0.1:80)
RECV (1.0027s) Handshake with localhost:80 (127.0.0.1:80) completed
SENT (2.0038s) Starting TCP Handshake > localhost:80 (127.0.0.1:80)
RECV (2.0039s) Handshake with localhost:80 (127.0.0.1:80) completed
SENT (3.0050s) Starting TCP Handshake > localhost:80 (127.0.0.1:80)
RECV (3.0050s) Handshake with localhost:80 (127.0.0.1:80) completed
SENT (4.0061s) Starting TCP Handshake > localhost:80 (127.0.0.1:80)
RECV (4.0062s) Handshake with localhost:80 (127.0.0.1:80) completed

Max rtt: 0.032ms | Min rtt: 0.008ms | Avg rtt: 0.012ms
TCP connection attempts: 5 | Successful connections: 5 | Failed: 0 (0.00%)
Tx time: 4.00575s | Tx bytes/s: 99.86 | Tx pkts/s: 1.25
Rx time: 4.00575s | Rx bytes/s: 49.93 | Rx pkts/s: 1.25
Nping done: 1 IP address pinged in 4.01 seconds
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.