简体   繁体   English

使用Netcat或Rails通过TCP / IP发送数据

[英]Send data over TCP/IP with Netcat or Rails

I have an IP of the server and a port on which I'm able to connect via nc on Ubuntu 14.04. 我有服务器的IP和端口,可以在Ubuntu 14.04上通过nc连接到该端口。

> nc x.x.x.x PORT

In order to communicate with the server, the first step is to send a WAKEUP call and get acknowledgment. 为了与服务器进行通信,第一步是发送WAKEUP呼叫并获得确认。 The server expects a 3 byte ID in the wakeup call. 服务器在唤醒调用中期望3字节的ID。 An example is provided in the documentation that shows the success scenario of sending the ID and receiving the ack using a software. 文档中提供了一个示例,该示例显示了使用软件发送ID和接收确认的成功方案。 ie

The client sends: 客户端发送:

<sy><sy><eq>111<et>

And the server responds with: 服务器响应:

<sy><ak>A<et><cr>

Here is some detail of <sy> 这是<sy>一些细节

Within <> brackets is a non-printable ASCII character ( <sy> = ASCII 22 or Hex 0x16) 在<>括号内是不可打印的ASCII字符( <sy> = ASCII 22或十六进制0x16)

I tried to replicate the exact same scenario but failed to do so. 我试图复制完全相同的方案,但没有这样做。 The server doesn't respond to the data I send, although the data is received there. 服务器没有响应我发送的数据,尽管在那里收到了数据。 I'm not sure about these tags <sy><sy><eq> etc. How to send the ID(111) along with these tags <sy> correctly? 我不确定这些标签<sy><sy><eq>等。如何正确发送ID(111)和这些标签<sy>

Also tried to send this data using Rails framework and Bindata ruby gem but don't know how to represent the above format. 还尝试使用Rails框架和Bindata ruby gem发送此数据,但不知道如何表示以上格式。

netcat is probably the wrong tool for this. netcat可能是错误的工具。 Or at least you will want to use some other program to feed it input. 或者至少您将要使用其他程序来提供输入。

If I were doing this, I would code up something in python or C that would both connect to the server and feed it whatever data I needed to send it (and receive/interpret the responses) leaving out nc altogether. 如果这样做的话,我会用python或C语言编写一些代码,这些代码既可以连接到服务器,也可以向我提供发送(以及接收/解释响应)所需的任何数据,而完全不使用nc There are many examples on the web. 网上有很多例子。

You can encode the control characters in a byte string in python with the syntax b'\\x16' for your <sy> character. 您可以使用<sy>字符的语法b'\\x16'将控制字符编码为python中的字节字符串。 Most other languages have an equivalent capability. 大多数其他语言都具有等效的功能。

I can't be sure exactly what those characters are. 我不确定这些字符到底是什么。 It seems likely they are standard ASCII control characters, but they aren't using the standard abbreviations (see http://www.theasciicode.com.ar/ for example). 似乎它们是标准的ASCII控制字符,但它们没有使用标准的缩写(例如,请参见http://www.theasciicode.com.ar/ )。 So presumably the documentation you are looking at has a list of the corresponding values. 因此,大概您正在查看的文档中有相应值的列表。 Assuming for the sake of example that <eq> corresponds to the ASCII ENQ character and <et> to the ASCII EOT (and given you already know that <sy> is equivalent to ASCII SYN), your desired string <sy><sy><eq>111<et> can be encoded in a python byte string: b'\\x16\\x16\\x05111\\x04' 举例来说,假设<eq>对应于ASCII ENQ字符,而<et>对应于ASCII EOT(并且已经知道<sy>等同于ASCII SYN),则需要的字符串<sy><sy><eq>111<et>可以用python字节字符串编码: b'\\x16\\x16\\x05111\\x04'

(or equivalently b'\\x16\\x16\\x05\\x31\\x31\\x31\\x04' if you like regularity: the 1 characters are simply ASCII digits, so you can replace each 1 with its binary equivalent b'\\x31' ) (或者,如果喜欢规律性,则等效地为b'\\x16\\x16\\x05\\x31\\x31\\x31\\x04'1字符只是ASCII数字,因此您可以将每个1替换为其等效的二进制b'\\x31'

To return to nc , trying to type in the control characters to the nc input from a terminal window is, while possible in most cases, very difficult and error-prone. 要返回nc ,在大多数情况下尝试在终端窗口的nc输入中键入控制字符是非常困难且容易出错的。 You will need to know the equivalent control character mapping (for example, 0x16 is "Ctrl-V") and will need to know how to get the terminal to accept that literal character (coincidentally, in linux, you have to precede most control characters with a Ctrl-V in order to enter them as input and avoid having them interpreted in the usual way: Ctrl-D == EOF, Ctrl-C == Interrupt, Ctrl-W == Delete-Previous-Word, etc). 您将需要知道等效的控制字符映射(例如0x16为“ Ctrl-V”),并且需要知道如何使终端接受该文字字符(巧合的是,在Linux中,您必须在大多数控制字符之前)使用Ctrl-V,以便将它们作为输入输入,并避免以通常的方式解释它们:Ctrl-D == EOF,Ctrl-C ==中断,Ctrl-W == Delete-Previous-Word等)。

So if you wanted to enter the data above into nc 's input from the command line, you would need to type these characters: 因此,如果您想从命令行将上述数据输入到nc的输入中,则需要输入以下字符:

Ctrl-V Ctrl-V              <sy> / SYN
Ctrl-V Ctrl-V              <sy> / SYN
Ctrl-V Ctrl-E              <eq> / ENQ
1
1
1
Ctrl-V Ctrl-D              <et> / EOT

But also important to note is that ordinarily nc will not actually send anything until you enter a newline (ie press the Return key). 但要注意的另一个重要事项是,通常在输入换行符之前(即按Return键), nc实际上不会发送任何内容。 Then that newline character will also get sent to the server which might not be what you want. 然后,该换行符也将发送到服务器,而这可能不是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM