zoukankan      html  css  js  c++  java
  • 10.13 nc:多功能网络工具

    nc命令

      是一个简单、可靠、强大的网络工具,它可以建立TCP连接,发送UDP数据包,监听任意的TCP和UDP端口,进行端口扫描,处理IPv4和IPv6数据包。
        如果系统没有nc命令,那么可以手动安装,安装命令为yum -y install nc。     

    nc命令的参数选项及说明

    -l    指定监听端口,然后一直等待网络连接
    -z    表示zero,表示扫描时不发送任何数据
    -v    显示详细输出
    -w    设置超时时间,对-l选项失效
    -p    指定nc命令使用的端口,不能和-l选项一起使用,可能引起错误
    -u    使用UDP连接,默认是TCP连接
    -s    指定发送数据的源IP地址,适用于多网卡主机
     
     
    [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
     
     
     
     
     
     
     
  • 相关阅读:
    Android SingleTask启动模式与Home键的问题
    Flutter Widget截图
    Flutter 以Dialog Activity形式展现
    Flutter Text或者RichText不换行的问题
    Adnroid提高效率之资源移动
    GO学习之 输入输出
    GO学习之 运算符
    GO学习之 值类型与引用类型
    GO学习之 指针
    GO学习之 变量与变量的基本数据类型
  • 原文地址:https://www.cnblogs.com/fadewalk/p/10840523.html
Copyright © 2011-2022 走看看