linux nc (NetCat) 命令详解

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

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

引用

[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版本进行简单说明。
假设两服务器信息:linux

引用

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

引用

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

引用

[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
二、克隆硬盘或分区
操做与上面的拷贝是雷同的,只须要由dd得到硬盘或分区的数据,而后传输便可。
克隆硬盘或分区的操做,不该在已经mount的的系统上进行。因此,须要使用安装光盘引导后,进入拯救模式(或使用Knoppix工 具光盘)启动系统后,在server2上进行相似的监听动做:服务器

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

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

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

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

三、端口扫描
能够执行:post

引用

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

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

五、模拟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内容。

六、聊天
nc还能够做为简单的字符下聊天工具使用,一样的,server2上须要启动监听:

[root@hatest2 tmp]# nc -lp 1234

server1上传输:

[root@hatest1 ~]# nc 192.168.228.222 1234

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

七、传输目录
从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
八、参数简介
这仅是一个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式的指定范围。
3、版本差别
不用系统上提供的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

4、用在脚本中
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

相关文章
相关标签/搜索