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 msOr 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:80Is it possible ping something like address:port? If it is possible, why doesn't what I tried work?
1511 Answers
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 4 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.orgYou 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).
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 7 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 msNote that it needs root privileges (or SELinux capabilities) to create raw IP packets, just like ping (which is most likely suid on your system).
4You 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 $?
1Additionally, 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 8000;date
Fri Jun 20 15:55:26 PDT 2014
Fri Jun 20 15:55:27 PDT 2014
$ date;nc -z -w 4 8000;date
Fri Jun 20 15:55:33 PDT 2014
Fri Jun 20 15:55:37 PDT 2014
$ nc -h
[v1.10-40] 9 You could also use nping (part of nmap):
$ nping -p 80 localhost
Starting Nping 0.6.00 ( ) 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 2 You can do this in the shell with Python as a not so short one liner:
$ portping() { python <<<"import socket; socket.setdefaulttimeout(1); socket.socket().connect(('$1', $2))" 2> /dev/null && echo OPEN || echo CLOSED; }
$ portping 8.8.8.8 54
CLOSED
$ portping 8.8.8.8 53
OPEN 0 Just for reference, wanted to share a post by Vivek Gite:
He lists various ways, some of which are already posted here. But the most surprising for me was nothing more but bash:
(echo >/dev/tcp/{host}/{port}) &>/dev/null && echo "opened" || echo "closed"
(echo >/dev/udp/{host}/{port}) &>/dev/null && echo "opened" || echo "closed"
(echo >/dev/tcp/) &>/dev/null && echo "Opened 22" || echo "Closed 22"
(echo >/dev/tcp/) &>/dev/null && echo "Opened 443" || echo "Closed 443"Or a super simple version: just looking at the output of the following command pattern:
echo >/dev/{tcp|udp}/{host}/{port}Useful when working with random docker containers.
3It is simple with nmap
examples:
#sintaxis
nmap -p [port] hostName
#first is command, after scan ports, type port - port or range ports, and ip or name of website...
## Scan port 80
nmap -p 80 onofri.org
## Scan TCP port 80
nmap -p T:80 onofri.org
## Scan UDP port 53
nmap -p U:53 onofri.org
## Scan two ports ##
nmap -p 80,443 onofri.org
## Scan port ranges ##
nmap -p 80-200 onofri.org
## Combine all options ##
nmap -p U:53,111,137,T:21-25,80,139,8080 onofri.org
nmap -p U:53,111,137,T:21-25,80,139,8080 server1.cyberciti.biz
nmap -v -sU -sT -p U:53,111,137,T:21-25,80,139,8080 onofri.org
## Scan all ports with * wildcard ##
nmap -p "*" 192.168.1.1
## Scan top ports i.e. scan $number most common ports ##
nmap --top-ports 5 onofri.org
nmap --top-ports 10 onofri.orgFor more information see this:
type in command line this: man nmap
I add watch tool here:
watch nmap -p22,80 google.com
Every 2,0s: nmap -p22,80 google.com Mon Jun 15 16:46:33 2015
Starting Nmap 6.40 ( ) at 2015-06-15 16:46 NOVT
Nmap scan report for google.com (127.0.0.1)
Host is up (0.0012s latency).
rDNS record for 127.0.0.1: google.com
PORT STATE SERVICE
22/tcp open ssh
80/tcp closed http
Nmap done: 1 IP address (1 host up) scanned in 0.18 seconds 1 Are you trying to test communication or get a response from port 80 on that node? PING will try to establish communication to a specific host through ICMP which has nothing to do with ports.
Instead try to check port info and test communication:
nmap -v -p 80 onofri.org 1