日期:2014-05-16  浏览次数:20600 次

linux nc常用用法

nc(netcat),一般我们多用在局域网内传送文件(scp多用在跳板机存在的情况),可查询man nc查看它的说明,更多用法请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 seconds延时的间隔
-l 监听模式,用于入站连接
-n 指定数字的IP地址,不能用hostname
-o file 记录16进制的传输
-p port本地端口号
-r 任意指定本地及远程端口
-s addr本地源地址
-u UDP模式
-v 详细输出——用两个
-v可得到更详细的内容
-w seconds timeout的时间
-z 将输入输出关掉——用于扫描时,其中端口号可以指定一个或者用lo-hi式的指定范围。

2.用法举例
1)端口扫描 (多做远程端口监控)
nc -v -w 3 10.1.50.58 -z 21-25


结果如下:

nc: connect to 10.1.50.58 port 21 (tcp) failed: Connection refused
Connection to 10.1.50.58 22 port [tcp/ssh] succeeded!
nc: connect to 10.1.50.58 port 23 (tcp) failed: Connection refused
nc: connect to 10.1.50.58 port 24 (tcp) failed: Connection refused
nc: connect to 10.1.50.58 port 25 (tcp) failed: Connection refused

?
2)文件拷贝

a.启动监听: nc -l 9999 > tmp.txt

b.传送文件: nc 0.0.0.0 9999 < .bashrc

c.查看文件: cat tmp.txt


3)聊天工具--一般不怎么用
在10.1.50.58上: nc -l 1111
在mx9.mail.sohu.com上: nc 10.1.50.58 1111
这样,双方就可以相互交流了。使用ctrl+C(或D)退出。

3.用nc操作memcache及nosql等
1)存储数据:printf “set key 0 10 4\r\nkaka\r\n” |nc 10.1.50.58 11211
2)获取数据:printf “get key\r\n” |nc 10.1.50.58 11211
3)删除数据:printf “delete key\r\n” |nc 10.1.50.58 11211
4)查看状态:printf “stats\r\n” |nc 10.1.50.58 11211