Home > Linux, 工具介绍 > 巧用Netcat方便网络程序开发

巧用Netcat方便网络程序开发

December 5th, 2011

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: 巧用Netcat方便网络程序开发

首先介绍下NC,这个号称网络瑞士军刀的工具。

What is Netcat?

Netcat is a featured networking utility which reads and writes data across network connections, using the TCP/IP protocol.
It is designed to be a reliable “back-end” tool that can be used directly or easily driven by other programs and scripts. At the same time, it is a feature-rich network debugging and exploration tool, since it can create almost any kind of connection you would need and has several interesting built-in capabilities.

It provides access to the following main features:

* Outbound and inbound connections, TCP or UDP, to or from any ports.
* Featured tunneling mode which allows also special tunneling such as UDP to TCP, with the possibility of specifying all network parameters (source port/interface, listening port/interface, and the remote host allowed to connect to the tunnel.
* Built-in port-scanning capabilities, with randomizer.
* Advanced usage options, such as buffered send-mode (one line every N seconds), and hexdump (to stderr or to a specified file) of trasmitted and received data.
* Optional RFC854 telnet codes parser and responder.

项目主页:http://netcat.sourceforge.net/
Wiki: http://en.wikipedia.org/wiki/Netcat

再看下我们如何使用NC:
在RHEL服务器里面NC是标配,无需用户自己安装,手册也很全 man nc就好了。

之前在开发服务端程序的时候经常用nc来发报文,模拟客服端的行为, 省的自己写个客户端的麻烦,比如:

$ echo -n "GET / HTTP/1.0\r\n\r\n" | nc host.example.com 80
$ nc [-C] localhost 25 << EOF
HELO host.example.com
MAIL FROM: <user@host.example.com>
RCPT TO: <user2@host.example.com>
DATA
Body of email.
.
QUIT
EOF

这二天从鸣嵩那里学了招模拟服务端的行为, 在调查自己的客户端的时候很方便,来看下:

#模拟服务端
$ nc -l 1234
hello
#模拟客户端
$ nc 127.0.0.1 1234
hello

NC还有其他的功能,读者自己来挖掘!

祝玩得开心!

Post Footer automatically generated by wp-posturl plugin for wordpress.

Categories: Linux, 工具介绍 Tags:
  1. Argan
    December 5th, 2011 at 14:40 | #1

    nc的我喜欢这个,http://engineering.tumblr.com/post/7658008285/efficiently-copying-files-to-multiple-destinations

    绝对是UNIX哲学的典范啊

    Yu Feng Reply:

    很有意思,多谢补充!

  2. ike
    December 5th, 2011 at 18:11 | #2

    期待霸哥的NUMA文章

    Yu Feng Reply:

    这个要慢点,容我多想会儿

Comments are closed.