zoukankan      html  css  js  c++  java
  • linux nc (NetCat) 命令详解

    原文:http://www.huanxiangwu.com/477/linux-nc-netcat

    一、版本
    通常的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.huanxiangwu.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/irc
    /usr/share/doc/nc-1.10/scripts/iscan
    /usr/share/doc/nc-1.10/scripts/ncp
    /usr/share/doc/nc-1.10/scripts/probe
    /usr/share/doc/nc-1.10/scripts/web
    /usr/share/doc/nc-1.10/scripts/webproxy
    /usr/share/doc/nc-1.10/scripts/webrelay
    /usr/share/doc/nc-1.10/scripts/websearch
    /usr/share/man/man1/nc.1.gz

  • 相关阅读:
    《WF编程》系列之 承载工作流:跟踪服务 Tracking Service
    C#中隐藏(new)和方法重写(override)和重载(overload)的区别
    一套完整自定义工作流的实现
    工作流规范
    新宇面试题
    c# 多态性
    SQL Join连接详解
    Minix安装及配置指南(转载)
    游戏开发的学习资料汇总
    error LNK2019: 无法解析的外部符号 __vsnprintf 问题的解决方法
  • 原文地址:https://www.cnblogs.com/wangkangluo1/p/3616051.html
Copyright © 2011-2022 走看看