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


    通常使用"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

  • 相关阅读:
    [转]史上最最佳软件开发实践指导
    附微信表情代码关键字
    TFS2010配置篇 — 万事具备只欠外网访问
    ASP.NET在IE10中Form表单身份验证失效问题解决方法
    TFS2010 更改已经登录的用户
    TFS命令tf:undo 强制签入别人签出的文件
    ASP.NET Core部署后报错: Could not load file or assembly 'System.Private.ServiceModel, Version=4.1.2.4, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
    SQL 增删改查语句
    存储过程拼接sql
    sql 求时间差
  • 原文地址:https://www.cnblogs.com/weiok/p/4681720.html
Copyright © 2020-2023  润新知