8个实用的Linux netcat命令
来源:互联网 发布:如何开一个淘宝店铺 编辑:程序博客网 时间:2024/05/18 02:10
Netcat or nc is a networking utility for debugging and investigating the network.
This utility can be used for creating TCP/UDP connections and investigating them. The biggest use of this utility is in the scripts where we need to deal with TCP/UDP sockets.
In this article we will learn about the netcat command by some practical examples.
1. Netcat in a Server-Client Architecture
The netcat utility can be run in the server mode on a specified port listening for incoming connections.
$ nc -l 2389
Also, it can be used in client mode trying to connect on the port(2389) just opened
$ nc localhost 2389
Now, if we write some text at the client side, it reaches the server side. Here is the proof :
$ nc localhost 2389HI, server
On the terminal where server is running :
$ nc -l 2389HI, server
So we see that netcat utility can be used in the client server socket communication.
2. Use Netcat to Transfer Files
The netcat utility can also be used to transfer files. At the client side, suppose we have a file named ‘testfile’ containing :
$ cat testfilehello test
and at the server side we have an empty file ‘test’
Now, we run the server as :
$ nc -l 2389 > test
and run the client as :
cat testfile | nc localhost 2389
Now, when we see the ‘test’ file at the server end, we see :
$ cat testhello test
So we see that the file data was transfered from client to server.
3. Netcat Supports Timeouts
There are cases when we do not want a connection to remain open forever. In that case, through ‘-w’ switch we can specify the timeout in a connection. So after the seconds specified along with -w flag, the connection between the client and server is terminated.
Server :
nc -l 2389
Client :
$ nc -w 10 localhost 2389
The connection above would be terminated after 10 seconds.
NOTE : Do not use the -w flag with -l flag at the server side as in that case -w flag causes no effect and hence the connection remains open forever.
4. Netcat Supports IPV6 Connectivity
The flag -4 or -6 specifies that netcat utility should use which type of addresses. -4 forces nc to use IPV4 address while -6 forces nc to use IPV6 address.
Server :
$ nc -4 -l 2389
Client :
$ nc -4 localhost 2389
Now, if we run the netstat command, we see :
$ netstat | grep 2389tcp 0 0 localhost:2389 localhost:50851 ESTABLISHEDtcp 0 0 localhost:50851 localhost:2389 ESTABLISHED
The first field in the above output would contain a postfix ’6′ in case the IPV6 addresses are being used. Since in this case it is not, so a connection between server and client is established using IPV4 addresses.
Now, If we force nc to use IPV6 addresses
Server :
$ nc -6 -l 2389
Client :
$ nc -6 localhost 2389
Now, if we run the netstat command, we see :
$ netstat | grep 2389tcp6 0 0 localhost:2389 localhost:33234 ESTABLISHEDtcp6 0 0 localhost:33234 localhost:2389 ESTABLISHED
So now a postfix ’6′ with ‘tcp’ shows that nc is now using IPV6 addresses.
5. Disable Reading from STDIN in Netcat
This functionality can be achieved by using the flag -d. In the following example, we used this flag at the client side.
Server :
$ nc -l 2389
Client :
$ nc -d localhost 2389Hi
The text ‘Hi’ will not be sent to the server end as using -d option the read from stdin has been disabled.
6. Force Netcat Server to Stay Up
If the netcat client is connected to the server and then after sometime the client is disconnected then normally netcat server also terminates.
Server :
$ nc -l 2389
Client :
$ nc localhost 2389
Server :
$ nc -l 2389$
So, in the above example we see that as soon as the client got disconnected the server was also terminated.
This behavior can be controlled by using the -k flag at the server side to force the server to stay up even after the client has disconnected.
Server :
$ nc -k -l 2389
Client :
$ nc localhost 2389^C
Server :
$ nc -k -l 2389
So we see that by using the -k option the server remains up even if the client got disconnected.
7. Configure Netcat Client to Stay Up after EOF
Netcat client can be configured to stay up after EOF is received. In a normal scenario, if the nc client receives an EOF character then it terminates immediately but this behavior can also be controlled if the -q flag is used. This flag expects a number which depicts number of seconds to wait before client terminates (after receiving EOF)
Client should be started like :
nc -q 5 localhost 2389
Now if the client ever receives an EOF then it will wait for 5 seconds before terminating.
8. Use Netcat with UDP Protocol
By default all the sockets that nc utility creates are TCP protocols but this utility also works with UDP protocol. To enable UDP protocol the -u flag is used.
Server :
$ nc -4 -u -l 2389
Client :
$ nc -4 -u localhost 2389
Now, both the server and client are configured to use UDP protocol. This can be confirmed by the following netstat command. So we see that this connection is now using the UDP protocol.
$ netstat | grep 2389udp 0 0 localhost:42634 localhost:2389 ESTABLISHED中文翻译:http://www.admin10000.com/document/1169.html
- 8个实用的Linux netcat命令
- 8 个实用的 Linux netcat 命令示例
- 8 个实用的 Linux netcat 命令示例
- 8 个实用的 Linux netcat 命令示例
- 8个实用的Linux netcat命令示例
- 8个实用的Linux netcat命令示例
- 8个实用的Linux netcat命令示例
- 8 个实用的 Linux netcat 命令示例
- 8 个实用的 Linux netcat 命令示例
- 8 个实用的 Linux netcat 命令示例
- 8个实用的netcat命令实例
- 8个实用的netcat命令实例
- Linux新手必备:8个实用的Linux终端命令
- 【Linux命令】netcat 网络工具的瑞士军刀
- linux nc命令 <netcat>
- Linux Netcat命令
- Linux Netcat 命令
- linux nc命令 <netcat>
- 检测应用第一次启动
- Java ftp 上传文件
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- 如何利用社会化媒体营销
- 关于搭建Oracle数据库遇到的问题(Oracle10g)及解决办法
- 8个实用的Linux netcat命令
- dwz+thinkphp环境配置
- IAR中的Zstack工程产生的.hex通过SmartRF Flash Programmer烧录到CC2530
- MAVEN nexus,在服务器端通过web界面添加上传第三方jar包
- WaitForSingleObject
- 解析 Linux 中的 VFS 文件系统机制
- 使用 inotify 监控文件系统的活动
- System V 了解
- 如何在wince 控制面板上增加应用程序?