首页 > 其他分享 >nc command

nc command

时间:2023-10-22 23:24:30浏览次数:53  
标签:nc connection command device Netcat port

copy from https://phoenixnap.com/kb/nc-command#:~:text=The%20Netcat%20(%20nc%20)%20command%20is,using%20either%20TCP%20or%20UDP.

 

Introduction

The Netcat (nc) command is a command-line utility for reading and writing data between two computer networks. The communication happens using either TCP or UDP. The command differs depending on the system (netcatncncat, and others).

Netcat is a crucial tool to master for network and system administrators due to the rich connection troubleshooting features and scripting usability.

Prerequisites

  • Two machines in the same network. The examples use two Ubuntu 18.04 virtual machines.
  • Access to the command line/terminal on both devices.
  • The IP address of each device.
  • Access to a browser or the curl command.

nc Command Syntax

The basic syntax for the nc command is:

nc [<options>] <host> <port>

The command consists of the following elements:

  • On Ubuntu, the commands nc and netcat both work as symbolic links for the OpenBSD version of Netcat. On CentOS, Debian, and RHEL, the command is ncat.
  • The <host> is either a numeric IP address or a symbolic hostname. 
  • The <port> is either a numeric port or service name.

Netcat has two working modes:

  • Connect mode. In connect mode, Netcat works as a client. The utility requires the <host> and <port>parameters.
  • Listen mode. In listen mode, Netcat works as a server. When <host> is omitted, Netcat listens on all available addresses for the specified port.

The command attempts to start a TCP connection at the provided host and port without any options.

Netcat (nc) Command Options

The table below outlines common nc command options:

OptionTypeDescription
-4 Protocol Use IPv4 only.
-6 Protocol Use IPv6 only.
-U
--unixsock
Protocol Use Unix domain sockets.
-u
--udp
Protocol Use UDP connection.
-g <hop1, hop2,...> Connect mode Set hops for loose source routing in IPv4. Hops are IP addresses or hostnames.
-p <port>
--source-port <port>
Connect mode Binds the Netcat source port to <port>.
-s <host>
--source <host>
Connect mode Binds the Netcat host to <host>.
-l
--listen
Listen mode Listens for connections instead of using connect mode.
-k
--keep-open
Listen mode Keeps the connection open for multiple simultaneous connections.
-v
--verbose
Output Sets verbosity level. Use multiple times to increase verbosity.
-z Output Report connection status without establishing a connection.

The list is not comprehensive. Check the manual page for a complete list of options using the man command:

man netcat

Use arrow keys to navigate and press q to exit.

nc Command Examples

The following nc command examples assume two devices with unique IP addresses. The two devices in the examples are: 

1. phoenixNAP_1 (device 1) with IP 10.0.2.4.

Device 1 IP address terminal output

2. phoenixNAP_2 (device 2) with IP 10.0.2.5.

Device 2 IP address terminal output

Both devices run as virtual machines with Ubuntu 18.04, but other setups are possible. Note that the commands vary for different operating systems.

Client/Server Connection

A simple client/server connection is between two devices. One device acts as a server (listens) while the other acts as a client (connects).

1. On device 1, run the nc command in listen mode and provide a port:

nc -lv 1234
Device 1 nc -lv 1234 listening terminal output

The -l option activates listen mode, making device 1 the server. The output shows the device listening for connections due to the -v option.

2. On device 2, run the nc command with the IP address of device 1 and the port:

nc -v 10.0.2.4 1234
Device 2 nc -v 10.0.2.4 1234 connection terminal output

The output shows the connection is successful. Device 1 confirms the link and prints the IP address of device 2.

Device 1 connection received terminal output

The client/server connection establishes successfully.

3. Send a message from either device, and the same message shows up on the other device. The client and server behave the same after the connection establishes.

To end the connection, press CTRL+C on either machine.

Ping Specific Port on Website

Use Netcat as an alternative to the ping command to test a specific port to a website. For example:

nc -zv google.com 443
nc -zv google.com 443 ping terminal output

If the ping succeeds, the output shows the successful connection message. The -z option ensures the connection does not persist. 

Netcat does not give any specific information, and there are other methods to ping a specific port.

Scanning Ports

Use the nc command to scan for open ports. 

1. Run nc on device 2 to listen on port 1234:

nc -lkv 1234
nc -lkv 1234 terminal output

The -k option ensures the connection stays open after a disconnect.

2. Run the following command on device 2 to check whether port 1234 is open:

nc -zv 10.0.2.4 1234
nc -zv 10.0.2.4 1234 open port terminal output

If the port is open, the output shows a successful connection message. 

3. Alternatively, scan multiple ports on device 2 by adding a port range. For example:

nc -zv 10.0.2.4 1230-1235
nc -zv 10.0.3.4 scan port range terminal output

The output shows whether the connection is successful or not for each port. 

4. When scanning for port ranges, filter the results using grep:

nc -zv 10.0.2.4 1230-1235 2>&1 | grep 'succeeded'
nc scan open port grep succeeded terminal output

For example, grepping for the word succeeded only shows open ports in the output.

Transfer Files

Netcat allows transferring files through established connections. To see how file transfers work, do the following:

1. Create a sample file on device 1 using the touch command:

touch file.txt

The command creates an empty text file.

2. Create a listening connection on device 1 and redirect the file to the nc command:

nc -lv 1234 < file.txt
nc send file terminal output

3. On device 2, connect to device 1 and redirect the file:

nc -zv 10.0.2.4 1234 > file.txt
nc -zv file transfer complete

Confirm the file transfer is complete using the ls command.

The output shows the file name, indicating the transfer was succesful.

Transfer Directory

Netcat does not allow transferring directories in the same way as files. Use the tar command to send multiple files or directories and pipe the command to Netcat.

1. Create a directory on either device and add multiple files:

mkdir files; touch files/file{1..5}.txt

The command creates a files directory with five text files.

2. Navigate to the directory using the cd command:

cd files

3. On the other device, create and enter the destination directory:

mkdir files_destination && cd files_destination

4. Create a listening connection on port 1234 and pipe the tar command:

nc -lv 1234 | tar xfv -
nc listen for tar transfer terminal output

The listening connection expects a file that tar extracts.

5. On the other device, send the directory with:

tar -cf - . | nc -v 10.0.2.5 1234
nc send tar directory terminal output

The connection establishes and sends the tar file. 

nc transferred files destination terminal output

The receiving end extracts the files immediately, and the transfer is complete.

Create Web Server

To create a web server with Netcat, do the following:

1. Run the web server on device 1 and listen for connections on port 1234:

nc -lv 10.0.2.4 1234
nc -lv 10.0.2.4 1234 terminal output

Omitting the address runs the web server on localhost.

2. On device 2, run the address and port in a browser. Alternatively, use the curl command:

curl 10.0.2.4:1234

The page does not display anything for now.

3. On device 1 where the web server is listening, the request sent by the browser or curl is visible.

nc web server connection curl terminal output

The message shows the request information, such as the request type, host, and user agent.

4. To send a response to the client (device 2), paste the following code on device 1:

HTTP/1.1 200 Everything OK
Server: netcat
Content-Type: text/html; charset=UTF-8

<!DOCTYPE html>
<html>

<head>
<title>
Netcat
</title>
</head>
<body>
<h1>A webpage served with nc</h1>
</body>
</html>
http page response

The response updates the information immediately. 

html page received terminal

If accessing the web server from a browser, the browser page fetches the updates live.

html page received browser

The web server is running successfully. Shut off the server with CTRL+C.

Simple Chat Server

Take advantage of the Netcat communication functionality to create a simple chat server.

1. On device 1, run the following command:

awk -W interactive '$0="Bob: "$0' | nc -lv 1234
awk nc chat listening connection terminal

The awk command helps add Bob's username to the messages sent through the server. 

2. On device 2, add a different username and connect to the chat server:

awk -W interactive '$0="Alice: "$0' | nc 10.0.2.4 1234

Send messages back and forth to test the chat. Bob (device 1) sees messages sent from Alice (device 2) with prefixed names and vice versa. 

nc chat exchanged messages terminal outputs

Their own usernames do not show up in their chat windows.

Send HTTP Request

Use Netcat with printf to send a HTTP request to a website. For example, to send a request to google.com on port 80 (for TCP/IP connections), run the following:

printf "GET / HTTP/1.0\r\n\r\n" | nc -v google.com 80
google nc http request response terminal output

The output prints the page header and contents. Most pages disable a TCP connection and fetch the 404 error page.

Conclusion

After following the examples from this tutorial, you know how to use the nc command. Netcat is a powerful tool for network administrators. 

For more networking tools, check out the netstat command.

标签:nc,connection,command,device,Netcat,port
From: https://www.cnblogs.com/saaspeter/p/17781360.html

相关文章

  • PAT 甲级【1007 Maximum Subsequence Sum】
    本题是考察动态规划与java的快速输入:max[i]表示第i个结尾的最大的连续子串和。bbegin[i]表示第[begin[i],i]为最大和的开始位置超时代码:importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;publicclassMain{@Suppres......
  • Temporally Grounding Natural Sentence in Video
    TemporallyGroundingNaturalSentenceinVideo摘要:我们引入了一种有效且高效的方法,可以在长的、未修剪的视频序列中建立(即本地化)自然句子。具体来说,提出了一种新颖的时间地面网络(TGN)来暂时捕获视频和句子之间不断变化的细粒度逐字交互。TGN基于所利用的逐字交互,对每帧结束......
  • [AHK2] 合并使用#include的脚本
    这个脚本用于将一个脚本中的#include语句包含的脚本添加到这条#include语句的位置。同时,它有其他功能,如:去除空行、注释(仅单行)、替换内置变量。因为脚本原理是读取单行并处理,所以只能处理单行注释,要做更多复杂功能就需要使用其他方法了,比如索引表……但脚本主要目的就是合并分部......
  • 小白学Python - 使用 Python 的 OpenCV 绘制矩形并提取对象
    使用Python的OpenCV绘制矩形并提取对象OpenCV是一个开源计算机视觉和机器学习软件库。可以在它的帮助下完成各种图像处理操作,例如操纵图像和应用大量滤镜。它广泛用于对象检测、人脸检测和其他图像处理任务。让我们看看如何使用OpenCV在图像上绘制矩形并提取对象。编写代码#......
  • Adobe InCopy CC2021 for Mac「Ic 支持M1芯片」汉化版下载附教程
    AdobeInCopy2021是一款由adobe公司最近推出的一款专业功能强大的编写和复印编辑软件,它主要是为开发者和专业的编辑提供的,通过它可以很容易地帮助用户创建一个独立的文档,然后你就可以根据自己的需要自由地设计文本样式,跟踪更改并对文档进行简单的布局修改,并且设计人员可以将共享......
  • ABB AC900F学习笔记327:WINCC7.5SP2作为OPC SERVER,freelance2019SP2作为OPCC LIENT练习
    这一篇博客我在新浪博客记录过,地址是 ABBAC900F学习笔记327:WINCC7.5SP2作为OPCSERVER,freelance2019SP2作为OPCCLIENT练习_来自金沙江的小鱼_新浪博客(sina.com.cn)为了避免丢失,我在这里再次记录一遍今天做一个练习,WINCC7.5SP2作为OPCSERVER,freelance2019SP2作为OPCCLIENT。......
  • Transformer-based Encoder-Decoder Models
    整理原链接内容方便阅读https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/Encoder_Decoder_Model.ipynbtitle:"Transformer-basedEncoder-DecoderModels"thumbnail:/blog/assets/05_encoder_decoder/thumbnail.pngauthors:user:p......
  • ABBAC900F学习笔记326:freelance2019SP1作为OPC DA SERVER,WINCC7.5SP2作为OPC DA CLIEN
    昨天练习了ABB的OPCDA通过寻,在同一台计算机上实验的。今天测试局域网上freelance2019SP1作为OPCDASERVER,WINCC7.5SP2作为OPCDACLIENT通讯。测试在昨天的ABB练习程序基础上进行。1.freelance2019SP1作为OPCDASERVER,配置DCOM,参考前面WINCC作为DASERVER的配置方法WINDO......
  • 论文阅读:SceneEncoder: Scene-Aware Semantic Segmentation of Point Clouds with A L
    SceneEncoder:Scene-AwareSemanticSegmentationofPointClouds withALearnableSceneDescriptorSceneEncoder:用可学习的场景描述符对点云进行场景感知的语义分割摘要除了局部特征,全局信息在语义分割中起着至关重要的作用,而最近的工作通常不能明确地提取有意义的全局信息......
  • Flutter开发Don't use 'BuildContext's across async gaps警告
    问题Flutter开发中遇到Don'tuse'BuildContext'sacrossasyncgaps警告有问题的源码if(awaitdatabaseHelper.isDataExist(task.title)){showDialog(context:context,builder:(BuildContextcontext){returnAlertDialog(......