Linux ping command with port

Is it possible to ping an address:port?

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?

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.

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.

@glglgl; what? you never put a hack where a slash should go? but you are correct, it should be a slash.

11 Answers 11

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)

for anyone who finds this answer, like I did, only to find paping is not in any repositories and is ancient code that won’t even compile on ARM — scroll down further to the ‘hping’ answer. It’s a perfect drop-in for ping when you need to test a port and is easily accessible in various repositories (ie. Ubuntu, Arch) including ARM.

How can I install paping ? I have visited the repo but there are no releases. Ah, nevermind.. google code was not accessible for me at the time.

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 .

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

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

Читайте также:  Linux command to add cron job

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

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.

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

@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?

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 

Excellent suggestion. I used to use this to test if a web server was up when a web browser was not an easy option.

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.

Читайте также:  Linux установить графическую среду

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.

$ 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 sport=80 flags=SA seq=0 win=29200 rtt=7.5 ms len=46 ip=77.237.27.37 ttl=58 sport=80 flags=SA seq=1 win=29200 rtt=7.4 ms len=46 ip=77.237.27.37 ttl=58 sport=80 flags=SA seq=2 win=29200 rtt=8.5 ms len=46 ip=77.237.27.37 ttl=58 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).

I wish this were the «accepted answer». hping was easily findable in multiple repos, unlike paping which is ancient crufty code that doesn’t compile on ARM. Also unlike NMAP, haping returns a status code that is easily testible from a shell script, so it’s a perfect substitute for where you would otherwise use ping. Thanks for this answer, a shame I didn’t scroll down far enough to see it and had to find it on another web site instead.

I can not install hping in ubuntu.. Package not found.. But I have found hping3 .Is it the same as hping3 ? @d33tah

Ok I tried hping3 and it pings all ports.. Doesn’t matter closed or open.. How can I tell if a port is closed or open?

Using the -S option you will send a SYN packet, if the reply has flags=RA, you received a reset response, so the port is closed, but if you receive flags=SA, the port is open

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.

Читайте также:  Zipping directory in linux

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] 

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.

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

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.

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.

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.

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 

Источник

Оцените статью
Adblock
detector