zoukankan      html  css  js  c++  java
  • 一个常用的端口命令 nc (NetCat)

    mentor:提示

     aix  和 linux 都可使用:

    Linux : #nc -l 12345

    Aix  :    #netcat -l 12345

    #  nc  -l  12345   监听12345端口(占用12345端口)

    nc  即  netcat ;

    这个命令在一般情况下需要额外的安装。。。

    关于命令详细的使用可以man一下。。

    nc(NetCat),在网络工具中有”瑞士军刀”的美誉,它短小精悍,功能强大,下面分享一些我平时经常用到的功能,更多的功能请google之。

    1.基本参数
    想要连接到某处: nc [-options] hostname port[s] [ports] …
    绑定端口等待连接: nc -l -p port [-options] [hostname] [port]
    参数:
    -g gateway source-routing hop point[s], up to 8
    -G num source-routing pointer: 4, 8, 12, …
    -h 帮助信息
    -i secs 延时的间隔
    -l 监听模式,用于入站连接
    -n 指定数字的IP地址,不能用hostname
    -o file 记录16进制的传输
    -p port 本地端口号
    -r 任意指定本地及远程端口
    -s addr 本地源地址
    -u UDP模式
    -v 详细输出——用两个
    -v可得到更详细的内容
    -w secs timeout的时间
    -z 将输入输出关掉——用于扫描时,其中端口号可以指定一个或者用lo-hi式的指定范围。

    2.简单用法举例
    1)端口扫描
    # nc -v -w 2 192.168.2.34 -z 21-24
    nc: connect to 192.168.2.34 port 21 (tcp) failed: Connection refused
    Connection to 192.168.2.34 22 port [tcp/ssh] succeeded!
    nc: connect to 192.168.2.34 port 23 (tcp) failed: Connection refused
    nc: connect to 192.168.2.34 port 24 (tcp) failed: Connection refused

    2)从192.168.2.33拷贝文件到192.168.2.34
    在192.168.2.34上: nc -l 1234 > test.txt
    在192.168.2.33上: nc 192.168.2.34 < test.txt

    3)简单聊天工具
    在192.168.2.34上: nc -l 1234
    在192.168.2.33上: nc 192.168.2.34 1234
    这样,双方就可以相互交流了。使用ctrl+C(或D)退出。

    3.用nc命令操作memcached
    1)存储数据:printf “set key 0 10 6\r\nresult\r\n” |nc 192.168.2.34 11211
    2)获取数据:printf “get key\r\n” |nc 192.168.2.34 11211
    3)删除数据:printf “delete key\r\n” |nc 192.168.2.34 11211
    4)查看状态:printf “stats\r\n” |nc 192.168.2.34 11211
    5)模拟top命令查看状态:watch “echo stats” |nc 192.168.2.34 11211
    6)清空缓存:printf “flush_all\r\n” |nc 192.168.2.34 11211 (小心操作,清空了缓存就没了)


    4. 使用nc来传文件

     

    发送端:   cat a.txt  |  nc -l  3333

    接收端:   nc 192.168.0.3    3333 >   a.txt


    或者


    发送端:      cat a.txt     |   nc 192.168.0.3     9999 

    接收端:      nc -l 9999 > a.txt



    原文地址: 

    http://yunhaozou.org/linux/346.html   

     http://hi.baidu.com/xublog/blog/item/5b453cd3f2ff5c38960a162c.html

     

     

    扩展资料一:nc简单使用示例

    简单用法举例
    1)端口扫描
    # nc -v -w 2 192.168.2.34 -z 21-24
    nc: connect to 192.168.2.34 port 21 (tcp) failed: Connection refused
    Connection to 192.168.2.34 22 port [tcp/ssh] succeeded!
    nc: connect to 192.168.2.34 port 23 (tcp) failed: Connection refused
    nc: connect to 192.168.2.34 port 24 (tcp) failed: Connection refused

    2)从192.168.2.33拷贝文件到192.168.2.34
    在192.168.2.34上: nc -l 1234 > test.txt
    在192.168.2.33上: nc 192.168.2.34 < test.txt

    3)简单聊天工具
    在192.168.2.34上: nc -l 1234
    在192.168.2.33上: nc 192.168.2.34 1234
    这样,双方就可以相互交流了。使用ctrl+C(或D)退出。

    3.用nc命令操作memcached
    1)存储数据:printf “set key 0 10 6rnresultrn” |nc 192.168.2.34 11211
    2)获取数据:printf “get keyrn” |nc 192.168.2.34 11211
    3)删除数据:printf “delete keyrn” |nc 192.168.2.34 11211
    4)查看状态:printf “statsrn” |nc 192.168.2.34 11211
    5)模拟top命令查看状态:watch “echo stats” |nc 192.168.2.34 11211
    6)清空缓存:printf “flush_allrn” |nc 192.168.2.34 11211 (小心操作,清空了缓存就没了)

    nc -l 1234

    nc 127.0.0.1 1234
    在端口1234建立连接,互相发送输入

    nc -p 1234 -w 5 host.example.com 80
    建立从本地1234端口到host.example.com的80端口连接,5秒超时
    nc -u host.example.com 53
    u为UDP连接

    echo -n "GET / HTTP/1.0"r"n"r"n" | nc host.example.com 80
    连接到主机并执行

    nc -v -z host.example.com 70-80
    扫描端口(70到80),可指定范围。-v输出详细信息。

    扩展资料二:命令linux nc 命令传输文件

    nc到底干嘛用的我不多描述,今天主要讲下用nc传输文件。由于公司的设备sudo后没有ssh,scp等远程接入命令,或host.deny里面设置了 ssh的deny,不管怎样的原因。我今天跨过大家常用的scp,来说明下一个更有用的轻量级工具,nc的另一个强大的功---文件传输。


    范例如下:

    目的主机监听 
    nc -l 监听端口  > 要接收的文件名
    nc -l 4444 > cache.tar.gz

    源主机发起请求
    nc  目的主机ip    目的端口 
    nc  192.168.0.85  4444 

    netstat  如下
    [root@localhost jiangbao]# netstat -tpln

    tcp        0      0 0.0.0.0:4444                0.0.0.0:*                   LISTEN      18166/nc  

    英文描述如下 

    DATA TRANSFER

    Start by using nc to listen on a specific port, with output captured into a file:
               $ nc -l 1234 > filename.out
    Using a second machine, connect to the listening nc process, feeding it the file which is to be transferred:
               $ nc host.example.com 1234 

    扩展资料三:linux nc (NetCat) 命令详解,这篇文章对nc的介绍非常详细,和以上资料有部分重复.

    一、版本
    通常的Linux发行版中都带有NetCat(简称nc),甚至在拯救模式光盘中也由busybox提供了 简版的nc工具。但不同的版本,其参数的使用略有差异。
    NetCat 官方地址:
    http://netcat.sourceforge.net/

    引用

    [root@hatest1 ~]# cat /etc/asianux-release
    Asianux release 2.0 (Trinity SP2)
    [root@hatest1 ~]# cat /etc/redflag-release
    Red Flag DC Server release 5.0 (Trinity SP2)
    [root@hatest1 ~]# type -a nc
    nc is /usr/bin/nc
    [root@hatest1 ~]# rpm -q nc
    nc-1.10-22
    建议在使用前,先用man nc看看帮助。这里以红旗DC Server 5.0上的1.10版本进行简单说明。
    假设两服务器信息:

    引用

    server1: 192.168.228.221
    server2: 192.168.228.222
    二、常见使用
    1、远程拷贝文件
    从server1拷贝文件到server2上。需要先在server2上,用nc激活监听,server2上运行:

    [root@hatest2 tmp]# nc -lp 1234 > install.log
    server1上运行:

    引用

    [root@hatest1 ~]# ll install.log
    -rw-r–r–   1 root root 39693 12月 20   2007 install.log
    [root@hatest1 ~]# nc -w 1 192.168.228.222 1234 < install.log
    2、克隆硬盘或分区
    操作与上面的拷贝是雷同的,只需要由dd获得硬盘或分区的数据,然后传输即可。
    克隆硬盘或分区的操作,不应在已经mount的的系统上进行。所以,需要使用安装光盘引导后,进入拯救模式(或使用Knoppix工 具光盘)启动系统后,在server2上进行类似的监听动作:

    # nc -l -p 1234 | dd of=/dev/sda

    server1上执行传输,即可完成从server1克隆sda硬盘到server2的任务:

    # dd if=/dev/sda | nc 192.168.228.222 1234

    ※ 完成上述工作的前提,是需要落实光盘的拯救模式支持服务器上的网卡,并正确配置IP。

    3、端口扫描
    可以执行:

    引用

    # nc -v -w 1 192.168.228.222 -z 1-1000
    hatest2 [192.168.228.222] 22 (ssh) open
    4、保存Web页面

    # while true; do nc -l -p 80 -q 1 < somepage.html; done

    5、模拟HTTP Headers

    引用

    [root@hatest1 ~]# nc www.linuxso.com 80
    GET / HTTP/1.1
    Host: ispconfig.org
    Referrer: mypage.com
    User-Agent: my-browser


    HTTP/1.1 200 OK
    Date: Tue, 16 Dec 2008 07:23:24 GMT
    Server: Apache/2.2.6 (Unix) DAV/2 mod_mono/1.2.1 mod_python/3.2.8 Python/2.4.3 mod_perl/2.0.2 Perl/v5.8.8
    Set-Cookie: PHPSESSID=bbadorbvie1gn037iih6lrdg50; path=/
    Expires: 0
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    Cache-Control: private, post-check=0, pre-check=0, max-age=0
    Set-Cookie: oWn_sid=xRutAY; expires=Tue, 23-Dec-2008 07:23:24 GMT; path=/
    Vary: Accept-Encoding
    Transfer-Encoding: chunked
    Content-Type: text/html
    [......]
    在nc命令后,输入红色部分的内容,然后按两次回车,即可从对方获得HTTP Headers内容。

    6、聊天
    nc还可以作为简单的字符下聊天工具使用,同样的,server2上需要启动监听:

    [root@hatest2 tmp]# nc -lp 1234

    server1上传输:

    [root@hatest1 ~]# nc 192.168.228.222 1234

    这样,双方就可以相互交流了。使用Ctrl+D正常退出。

    7、传输目录
    从server1拷贝nginx-0.6.34目录内容到server2上。需要先在server2上,用nc激活监听,server2上运行:

    引用

    [root@hatest2 tmp]# nc -l 1234 |tar xzvf -
    server1上运行:

    引用

    [root@hatest1 ~]# ll -d nginx-0.6.34
    drwxr-xr-x 8 1000 1000 4096 12-23 17:25 nginx-0.6.34
    [root@hatest1 ~]# tar czvf – nginx-0.6.34|nc 192.168.228.222 1234
    8、参数简介
    这仅是一个1.10版本的简单说明,详细的参数使用还是需要看man:

    引用

    想要连接到某处: nc [-options] hostname port[s] [ports] …
    绑定端口等待连接: nc -l -p port [-options] [hostname] [port]
    参数:
    -g gateway source-routing hop point[s], up to 8
    -G num source-routing pointer: 4, 8, 12, …
    -h 帮助信息
    -i secs 延时的间隔
    -l 监听模式,用于入站连接
    -n 指定数字的IP地址,不能用hostname
    -o file 记录16进制的传输
    -p port 本地端口号
    -r 任意指定本地及远程端口
    -s addr 本地源地址
    -u UDP模式
    -v 详细输出——用两个-v可得到更详细的内容
    -w secs timeout的时间
    -z 将输入输出关掉——用于扫描时,其中端口号可以指定一个或者用lo-hi式的指定范围。
    三、版本差异
    不用系统上提供的nc版本会有说不同,其提供的参数使用方法也略有差异。
    例如,红旗Asianux 3.0 SP1拯救光盘上的版本是供使用的参数仅有一部分:

    引用

    # nc -h
    BusyBox v1.2.0 (2008.04.14-01:35+0000) multi-call binary

    Usage: nc [OPTIONS] [IP] [port]

    Netcat opens a pipe to IP:port

    Options:
             -l               listen mode, for inbound connects
             -p PORT         local port number
             -i SECS         delay interval for lines sent
             -e PROG         program to exec after connect (dangerous!)
             -w SECS         timeout for connects and final net reads
    而在Asianux 3.0 SP1系统中提供的nc版本则是1.84的,按上面的参数用法写会执行不了:

    引用

    [root@ftpserver ~]# rpm -q nc
    nc-1.84-10
    [root@ftpserver ~]# nc -lp 1234
    usage: nc [-46DdhklnrStUuvzC] [-i interval] [-p source_port]
               [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_version]
               [-x proxy_address[:port]] [hostname] [port[s]]
    讲查看man文档,可见在这个版本中,-l是不能与-s、-p、-z一起使用的,-w参数也会被忽略,所以,正确的用法是:

    [root@ftpserver tmp]# nc -l 1234

    四、用在脚本中
    nc每次启动监听后,都会在客户端连接完成并退出的同时,服务端一同退出。所以,如果需要不断的使用nc进行数据传输,需要在脚本中使用循环。利用nc实 现更多的功能,可参考其rpm提供的参考脚本:

    引用

    # rpm -qd nc
    /usr/share/doc/nc-1.10/Changelog
    /usr/share/doc/nc-1.10/README
    /usr/share/doc/nc-1.10/scripts/README
    /usr/share/doc/nc-1.10/scripts/alta
    /usr/share/doc/nc-1.10/scripts/bsh
    /usr/share/doc/nc-1.10/scripts/dist.sh
    /usr/share/doc/nc-1.10/scripts/irchttp://www.linuxso.com/command/nc.html

    下面是一段经典的对nc命令的介绍:

     

     

     

    http://www.heibai.net/article/info/info.php?infoid=32168

     

     

  • 相关阅读:
    迭代器生成器
    elasticsearch系列(五)score
    数据结构(五)串
    数据结构系列(四)栈与队列
    数据结构系列(三)线性表
    数据结构系列(二)算法
    数据结构系列(一)入门
    elasticsearch系列(四)部署
    SpringBoot系列(一)RestTemplate
    基于python的爬虫(一)
  • 原文地址:https://www.cnblogs.com/duanqingshe/p/2660044.html
Copyright © 2011-2022 走看看