zoukankan      html  css  js  c++  java
  • nc

    nc 功能说明:多功能网络工具
    nc是一个简单、可靠、强大的网络工具,它可以建立TCP连接,发送UDP数据包,监听任意的TCP和UDP端口,进行端口扫描,处理IPv4和IPv6数据包。
    如果系统没有nc命令,安装命令为 yum -y install nc。     
    
    参数选项
    -l    指定监听端口,然后一直等待网络连接
    -z    表示zero,表示扫描时不发送任何数据
    -v    显示详细输出
    -w    设置超时时间,对-l选项失效
    -p    指定nc命令使用的端口,不能和-l选项一起使用,可能引起错误
    -u    使用UDP连接,默认是TCP连接
    -s    指定发送数据的源IP地址,适用于多网卡主机
    
    
    
    由于后面的测试网络相关,因此需要准备好环境:需要关闭防火墙和selinux
    [root@cs6 ~]# /etc/init.d/iptables status
    表格:filter
    Chain INPUT (policy ACCEPT)
    num  target     prot opt source               destination         
    1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
    2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
    3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
    4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
    5    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited
     
    Chain FORWARD (policy ACCEPT)
    num  target     prot opt source               destination         
    1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited
     
    Chain OUTPUT (policy ACCEPT)
    num  target     prot opt source               destination         
     
    [root@cs6 ~]# /etc/init.d/iptables stop
    iptables:将链设置为政策 ACCEPT:filter                    [确定]
    iptables:清除防火墙规则:                                 [确定]
    iptables:正在卸载模块:                                   [确定]
    [root@cs6 ~]# setenforce 0
    [root@cs6 ~]# getenforce
    Permissive
     
    
    范例:模拟 TCP 连接并传输文本内容(远程复制文件)
    [root@cs6 ~]# nc -l 12345 > lewen.nc  #<=监听12345端口,将数据写入oldboy.nc
    #<=执行完上面的命令后,当前窗口挂起。
    #<=新开一个窗口执行命令。
    [root@cs6 ~]# cat lewen.nc  #<=首先查看12345端口。
    [root@cs6 ~]# netstat -lntup|grep 12345
    tcp        0      0 0.0.0.0:12345               0.0.0.0:*                   LISTEN      2612/nc             
    [root@cs6 ~]# cat lewen.log #<=待用的文件。
    6.8.0  #<=文件中的内容
    [root@cs6 ~]# nc 10.0.0.100 12345 < lewen.log  #<=使用nc命令向10.0.0.100 主机12345 端口传输lewen.log文件
    [root@cs6 ~]# netstat -lntup|grep 12345  #<==nc命令传输完数据后自动终止。
    [root@cs6 ~]# cat lewen.nc  #检查结果
    6.8.0 
    
    
    
    范例:用Shell 模拟一个简单的Web 服务器效果案例
    [root@cs6 ~]# echo "I love linux www.wenyule.top" >test.txt
    [root@cs6 ~]# vim web.sh
    #!/bin/bash
    while true
      do
        nc -l 80 < test.txt # 一直监听80端口,test.txt是发送给用户的内容。
    done
     
    [root@cs6 ~]# sh web.sh &>/dev/null &
    [1] 2685
    [root@cs6 ~]#
     
    [root@cs6 ~]# netstat -lntup|grep 80
    tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      2686/nc             
    [root@cs6 ~]# curl 127.0.0.1
    I love linux www.wenyule.top 
    
    
    
    范例:手动与HTTP服务器建立连接的例子。
    [root@cs6 ~]# nc blog.oldboyedu.com 80
    GET /about-us/ HTTP/1.1  <=粘贴这两行语句,速度要快,如果慢了程序就会超时自动退出。
    host:blog.oldboyedu.com   <==HTTP/1.1的要求必须写明hosto
    #<==敲两次回车确认发送请求报文,下面就是响应报文的内容。
    HTTP/1.1 301 Moved Permanently
    Server: nginx
    Date: Tue, 07 May 2019 13:40:50 GMT
    Content-Type: text/html
    Content-Length: 178
    Connection: keep-alive
    Location: https://blog.oldboyedu.com/about-us/
     
    <html>
    <head><title>301 Moved Permanently</title></head>
    <body bgcolor="white">
    <center><h1>301 Moved Permanently</h1></center>
    <hr><center>nginx</center>
    </body>
    </html>
    
    
    
    范例:利用nc 进行端口扫描
    [root@cs6 ~]# nc -z 10.0.0.100 20-30
    Connection to 10.0.0.100 22 port [tcp/ssh] succeeded!
    [root@cs6 ~]# nc -z 10.0.0.100 22
    Connection to 10.0.0.100 22 port [tcp/ssh] succeeded!
    [root@cs6 ~]# nc -zv 10.0.0.100 20-30  #<=使用-v选项详细显示扫描过程
    nc: connect to 10.0.0.100 port 20 (tcp) failed: Connection refused
    nc: connect to 10.0.0.100 port 21 (tcp) failed: Connection refused
    Connection to 10.0.0.100 22 port [tcp/ssh] succeeded!
    nc: connect to 10.0.0.100 port 23 (tcp) failed: Connection refused
    nc: connect to 10.0.0.100 port 24 (tcp) failed: Connection refused
    nc: connect to 10.0.0.100 port 25 (tcp) failed: Connection refused
    nc: connect to 10.0.0.100 port 26 (tcp) failed: Connection refused
    nc: connect to 10.0.0.100 port 27 (tcp) failed: Connection refused
    nc: connect to 10.0.0.100 port 28 (tcp) failed: Connection refused
    nc: connect to 10.0.0.100 port 29 (tcp) failed: Connection refused
    nc: connect to 10.0.0.100 port 30 (tcp) failed: Connection refused 
    
    
    
    范例:使用nc命令,模拟QQ聊天工具聊天
    
    打开两个命令行窗口,模拟两个人聊天的场景。
    首先在第一个窗口执行如下命令,执行完会hang住等待输入状态:
    
    [root@cs6 ~]# nc -l 12345
    
    然后在第2个窗口执行如下命令,执行完也会hang住等待输入状态:
    
    [root@cs6 ~]# nc 127.0.0.1 12345
    
    此时两个窗口都等待输入内容。我们先新建第3个窗口,查看他们建立的网络连接:
    
    [root@cs6 ~]# netstat -ntp|grep nc #<=12345端口是nc指定开放的,52978端口是系统为了和12345就口通信随机开放的,当然也可以使用-p选项指定开放端口。
    tcp        0      0 127.0.0.1:12345             127.0.0.1:37060             ESTABLISHED 2748/nc             
    tcp        0      0 127.0.0.1:37060             127.0.0.1:12345             ESTABLISHED 2749/nc   
    
    怎么聊天呢?很简单,你在第一个窗口中输入想要说的话,然后敲回车键,悄悄话就会自动发送到对方(第二个窗口),和QQ的效果一样:
     
    [root@cs6 ~]# nc -l 12345
    hi,I am lewen
    
    对方(第二个窗口)也只需要输入回复的话然后敲回车键,消息就能发送给你:
    [root@cs6 ~]# nc 127.0.0.1 12345
    hi,I am lewen
    hello,lewen
    
     
     
     
  • 相关阅读:
    Working with macro signatures
    Reset and Clear Recent Items and Frequent Places in Windows 10
    git分支演示
    The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.1.
    Build website project by roslyn through devenv.com
    Configure environment variables for different tools in jenkins
    NUnit Console Command Line
    Code Coverage and Unit Test in SonarQube
    头脑王者 物理化学生物
    头脑王者 常识,饮食
  • 原文地址:https://www.cnblogs.com/l10n/p/14185848.html
Copyright © 2011-2022 走看看