2014-06-16 18:17:25 +0000 2014-06-16 18:17:25 +0000
217
217
Advertisement

是否可以ping一个地址:端口?

Advertisement

我对网络不感兴趣,有以下问题与Linux的ping命令有关。例如:

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

或者我也可以ping一个地址:端口,例如。onofri.org:80*?

如果我试了这个,就不行了:

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

有没有可能ping到像address:port这样的地址?如果可以的话,为什么我试过的东西不能用?

Advertisement
Advertisement

答案 (11)

311
311
311
2014-06-16 18:19:26 +0000

端口是UDPTCP的一个概念。Ping消息在技术上被称为ICMP Echo RequestICMP Echo Reply,它们是ICMP的一部分。ICMP、TCP和UDP是 “兄弟姐妹",它们不是基于彼此的,而是三个独立的协议,运行在IP之上。

nmap -p 80 onofri.org

你也可以像其他答案中建议的那样使用 ping (如果端口被关闭或被过滤,它将会给出一个错误)。

86
86
86
2014-06-16 18:24:11 +0000

我使用的是Telnet,因为它内置在很多平台上,不需要额外下载。

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

如果你知道你要连接的服务的命令顺序,你可以键入一个命令 HTTP/FTP GET ),然后在终端中观察响应和输出。这对于测试服务本身是非常有用的,因为它可以显示发送给客户端的错误信息,比如HTTP 500错误。

如果收到拒绝连接的消息,则表示该端口被关闭。

49
Advertisement
49
49
2014-06-16 21:15:05 +0000
Advertisement

你可以使用netcat连接到特定的端口,查看是否有连接。-v 标志会增加verbosity来显示端口是打开还是关闭。一旦有了连接, -z 标志会使 netcat 退出。然后你可以通过 $? 退出代码来查看连接是否建立。

$ 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

此外, 你可以使用 mtr 和 -T 标志来指定 tcp, -P 标志来指定端口。这样做的效果类似于通过 TCP 进行跟踪路由,而不仅仅是 ICMP。

sigh因为我们不能把代码放在注释中,所以我不得不编辑添加这一点。Knoppix可能在netcat的版本上做了一些不同的事情,但这是我从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]
48
48
48
2014-06-17 15:35:04 +0000

是的,使用HPing来做:

$ 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

&001

注意,它需要root权限(或SELinux功能)来创建原始IP包,就像ping一样(很可能在你的系统上有suid)。

21
Advertisement
21
21
2015-01-21 08:02:45 +0000
Advertisement

你可以使用Paping,一个跨平台的TCP端口测试,模拟ping(端口ping)的功能

(另见Github,因为code.google.com已经贬值了)

paping -p 80 google.com
19
19
19
2014-06-23 09:57:58 +0000

你也可以使用npingnmap的一部分):

$ 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
8
Advertisement
8
8
2015-12-18 22:11:18 +0000
Advertisement

你可以在shell中用Python做一个不算短的单行本:

$ 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
7
7
7
2017-06-06 13:07:40 +0000

仅供参考,想分享Vivek Gite的一个帖子。https://www.cyberciti.biz/faq/ping-test-a-specific-port-of-machine-ip-address-using-linux-unix/#comment-920398

他列举了各种方法,其中一些已经在这里发布了。但最让我惊讶的是,除了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/www.cyberciti.biz/22) &>/dev/null && echo "Opened 22" || echo "Closed 22"
(echo >/dev/tcp/www.cyberciti.biz/443) &>/dev/null && echo "Opened 443" || echo "Closed 443"
echo >/dev/{tcp|udp}/{host}/{port}

之外,最让我惊讶的是&007:

&001

或者一个超级简单的版本:只看下面的命令模式输出:

&001

在使用随机docker容器时很有用。

3
Advertisement
3
3
2015-06-15 10:48:37 +0000
Advertisement

我在这里添加watch工具:

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 ( http://nmap.org ) 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
3
3
3
2016-07-25 18:26:01 +0000

很简单,以nmap

为例:

#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.org

更多信息请参见:

在命令行中输入: man nmap http://www.cyberciti.biz/networking/nmap-command-examples-tutorials/ http://www.tecmint.com/nmap-command-examples/

1
1
1
2014-06-19 15:58:08 +0000

你是想测试通信还是从该节点上的80端口获得响应? PING会尝试通过ICMP建立与特定主机的通信,而ICMP与端口无关。

,请尝试http://nmap.org/检查端口信息并测试通信:

nmap -v -p 80 onofri.org
Advertisement

相关问题

6
10
3
5
8
Advertisement