• 判断端口通不通的几种方法


    以下内容转载出处:https://my.oschina.net/yongqing/blog/305715
     
    原文如下:
     
    摘要: 通常使用"telnet ip port"判断端口通不通. 有其它方法吗?先看下面的几种方法

    通常使用"telnet ip port"判断端口通不通. 有其它方法吗?先看下面的几种方法

    准备环境

    启动一个web服务器,提供端口.

    用其它web服务器提供端口也一样,由于python比较方便,这里就用它

    1 [wyq@localhost ~]$ python -m SimpleHTTPServer 8080
    2 Serving HTTP on 0.0.0.0 port 8080 ...
     

    使用telnet判断

    telnet是windows标准服务,可以直接用;如果是linux机器,需要安装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.能访问指定端口. 遵循这两点可以找到很多工具.

    一般在windows下使用telnet比较方便,linux下个人就比较喜欢用wget.

  • 相关阅读:
    Flink CEP实例及基础应用
    seata 分布式事务
    skywalking 分布式链路追踪
    datax 离线数据同步工具
    rocketmq 消息队列
    Nacos 服务注册
    跨站(cross-site)、跨域(cross-origin)、SameSite与XMLHttpRequest.withCredentials
    读写二进制文件与文本文件
    WPF中通过双击编辑DataGrid中Cell(附源码)
    记一次XML文件读取优化
  • 原文地址:https://www.cnblogs.com/Reclouds-shangri-la/p/8715891.html
Copyright © 2020-2023  润新知