通常使用"telnet ip port"判斷端口通不通. 有其它方法嗎?先看下面的幾種方法
准備環境
啟動一個web服務器,提供端口.
[wyq@localhost ~]$ python -m SimpleHTTPServer 8080
Serving HTTP on 0.0.0.0 port 8080 ...
用其它web服務器提供端口也一樣,由於python比較方便,這里就用它
使用telnet判斷
telnet是windows標准服務,可以直接用;如果是linux機器,需要安裝telnet.
#yum install telnet // 安裝telnet
用法: telnet ip port
先用telnet連接不存在的端口
[wyq@localhost ~]$ telnet localhost 9000
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
直接提示連接被拒絕
再連接上面提供的端口
[wyq@localhost ~]$ telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
看到"Connected to localhost." 可以知道連接成功了.
后台服務器有什么反映?
[wyq@localhost monitor]$ python -m SimpleHTTPServer 8080
Serving HTTP on 0.0.0.0 port 8080 ...
沒有任何反映
此時telnet停住了,隨便輸入一個字符"a",然后回車
[wyq@localhost ~]$ telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
a
<head>
<title>Error response</title>
</head>
<body>
<h1>Error response</h1>
<p>Error code 400.
<p>Message: Bad request syntax ('a').
<p>Error code explanation: 400 = Bad request syntax or unsupported method.
</body>
Connection closed by foreign host.
再看服務器
[wyq@localhost monitor]$ python -m SimpleHTTPServer 8080
Serving HTTP on 0.0.0.0 port 8080 ...
127.0.0.1 - - [22/Aug/2014 07:15:16] code 400, message Bad request syntax ('a')
127.0.0.1 - - [22/Aug/2014 07:15:16] "a" 400 -
上面是linux環境下telnet連接一個web服務端口的情況.
使用ssh判斷
ssh是linux的標准配置並且最常用,可以用來判斷端口嗎?
用法: ssh -v -p port username@ip
-v 調試模式(會打印日志).
-p 指定端口
username可以隨意
連接不存在端口
[wyq@localhost ~]$ ssh -v -p 9000 wyq@localhost
OpenSSH_6.4, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /home/wyq/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 51: Applying options for *
debug1: Connecting to localhost [127.0.0.1] port 9000.
debug1: connect to address 127.0.0.1 port 9000: Connection refused
ssh: connect to host localhost port 9000: Connection refused
"Connection refused"表示端口不可用
連接存在的端口
[wyq@localhost ~]$ ssh -v -p 8080 wyq@localhost
OpenSSH_6.4, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /home/wyq/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 51: Applying options for *
debug1: Connecting to localhost [127.0.0.1] port 8080.
debug1: Connection established.
debug1: identity file /home/wyq/.ssh/id_rsa type 1
debug1: identity file /home/wyq/.ssh/id_rsa-cert type -1
debug1: identity file /home/wyq/.ssh/id_dsa type -1
debug1: identity file /home/wyq/.ssh/id_dsa-cert type -1
debug1: identity file /home/wyq/.ssh/id_ecdsa type -1
debug1: identity file /home/wyq/.ssh/id_ecdsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.4
debug1: ssh_exchange_identification: <head>
debug1: ssh_exchange_identification: <title>Error response</title>
debug1: ssh_exchange_identification: </head>
debug1: ssh_exchange_identification: <body>
debug1: ssh_exchange_identification: <h1>Error response</h1>
debug1: ssh_exchange_identification: <p>Error code 400.
debug1: ssh_exchange_identification: <p>Message: Bad request syntax ('SSH-2.0-OpenSSH_6.4').
debug1: ssh_exchange_identification: <p>Error code explanation: 400 = Bad request syntax or unsupported method.
debug1: ssh_exchange_identification: </body>
ssh_exchange_identification: Connection closed by remote host
"Connection established" 表示已經連上端口
服務器輸出
[wyq@localhost ~]$ python -m SimpleHTTPServer 8080
Serving HTTP on 0.0.0.0 port 8080 ...
127.0.0.1 - - [22/Aug/2014 13:32:55] code 400, message Bad request syntax ('SSH-2.0-OpenSSH_6.4')
127.0.0.1 - - [22/Aug/2014 13:32:55] "SSH-2.0-OpenSSH_6.4" 400 -
使用wget判斷
wget是linux下的下載工具,需要先安裝.
用法: wget ip:port
連接不存在的端口
[wyq@localhost ~]$ wget localhost:9000
--2014-08-22 13:36:42-- http://localhost:9000/
正在解析主機 localhost (localhost)... 127.0.0.1
正在連接 localhost (localhost)|127.0.0.1|:9000... 失敗:拒絕連接。
連接存在的端口
[wyq@localhost ~]$ wget localhost:8080
--2014-08-22 13:37:22-- http://localhost:8080/
正在解析主機 localhost (localhost)... 127.0.0.1
正在連接 localhost (localhost)|127.0.0.1|:8080... 已連接。
已發出 HTTP 請求,正在等待回應... 200 OK
長度:2770 (2.7K) [text/html]
正在保存至: “index.html”
100%[======================================>] 2,770 --.-K/s 用時 0s
2014-08-22 13:37:22 (105 MB/s) - 已保存 “index.html” [2770/2770])
總結
提供端口服務,則使用了tcp協議,上面是以web服務器為例。如果服務器是更簡單的tcp服務器,三個工具同樣適用.
三個工具的共同點是:1.以tcp協議為基礎;2.能訪問指定端口. 遵循這兩點可以找到很多工具
文章轉載:http://my.oschina.net/yongqing/blog/305715
