bash 使用 Netcat 将二进制文件(逐行)发送到套接字服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19265300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send a binary file (line by line) to a socket server with Netcat
提问by DHS
As a spin off to this Stack Overflow questionI want to archieve the same except for a couple of tweaks.
作为这个 Stack Overflow 问题的衍生,除了一些调整之外,我想归档相同的内容。
I want to connect to a host, send a binary file, line by line, and have a couple of seconds of delay between each line... and it must be in the same connection. The strings I want to send are mostly textbased but there are a couple of non-printable chars in the line.
我想连接到主机,逐行发送一个二进制文件,并且每行之间有几秒钟的延迟......而且它必须在同一个连接中。我想发送的字符串主要是基于文本的,但该行中有几个不可打印的字符。
This is an example of a string/line I want to send (thousands of lines per file):
这是我要发送的字符串/行的示例(每个文件数千行):
<null>e<null><null>00326513,6598,no,8,,2z<null>
<null>e<null><null>00326513,6598,no,8,,2z<null>
The <null>
value is the HEX value 0x00
.
该<null>
值为十六进制值0x00
。
I have rewitten my question from its first entry but @mzetanswered me on my original question as my question was text-only then but I later on discovered that my string had these 0x00
chars inside which I could not see at first. I want to use his answer as a base as I think it almost works but it may need a tweak so this is his contribution, not mine.
我从它的第一个条目中重新编写了我的问题,但@mzet回答了我原来的问题,因为我的问题是纯文本的,但后来我发现我的字符串0x00
里面有这些字符,我一开始看不到。我想用他的回答作为基础,因为我认为它几乎有效,但它可能需要调整,所以这是他的贡献,而不是我的贡献。
If I have a file with 3 lines:
如果我有一个包含 3 行的文件:
<null>e<null><null>00370513,6598,no,8,,2z<null>
<null>f<null><null>00891548,6598,yes,8,,3z<null>
<null>f<null><null>00129525,6598,yes,8,,2z<null>
I then thought I could tweak @mzetanswer and replace the <null>
values inside my file with \x00
and set -e
on the echo
command:
然后我想我可以调整@mzet答案并将<null>
我的文件中的值替换为\x00
并-e
在echo
命令上设置:
[root@srv]# mkfifo /tmp/fifoIn; cat /tmp/fifoIn | nc localhost 2222 &
[root@srv]# cat myfile | while read line; do echo -ne $line; sleep 2; done > /tmp/fifoIn
When I do that, I can see the x00
on the serverside:
当我这样做时,我可以x00
在服务器端看到:
[root@srv]# nc -l 2222
x00ex00x0000370513,6598,no,8,,2zx00
x00fx0000891548,6598,yes,8,,3zx00
x00fx00x0000129525,6598,yes,8,,2zx00
Is it possible to send a binary (text?) file as I want? And if it it not possible to send a file, line by line, is it then possible to send one string several thousands of times? Strictly necessary they don't need to be unique or in a file as I could manage with the same string repeating itself.
是否可以根据需要发送二进制(文本?)文件?如果无法逐行发送文件,那么是否可以将一个字符串发送数千次?绝对必要的是,它们不需要是唯一的,也不需要在文件中,因为我可以使用重复的相同字符串进行管理。
EDIT #1
编辑#1
Why is my script only sending one line to the server (I would expect 4 lines) afterwards it is just pausing(?) forever. The client (nor the server) closes its connection but it doesn't come with any errors:
为什么我的脚本只向服务器发送一行(我希望有 4 行)之后它只是永远暂停(?)。客户端(也不是服务器)关闭了它的连接,但它没有出现任何错误:
rm -f /tmp/fifofile
mkfifo /tmp/fifofile
cat /tmp/fifofile | nc 192.168.20.6 5000 &
sleep 1
i="0"
while [ $i -lt 4 ]
do
echo "$i"
echo -ne "\x00e\x00\x00001212dsfdsfdsfsdfsdfsdfdsf\x00" | tee /tmp/fifofile
sleep 1
i=$[$i+1]
done
回答by mzet
You can achieve it in two steps:
您可以通过两个步骤来实现:
1) You need to start nc with a named pipe (fifo) as its input:
1) 您需要使用命名管道 (fifo) 作为输入来启动 nc:
mkfifo /tmp/fifoIn; cat /tmp/fifoIn | nc localhost 2222 &
2) Send your data from file input.txt, line by line with 2 sec delay:
2) 从文件 input.txt 中逐行发送数据,延迟 2 秒:
cat input.txt | while read line; do echo $line; sleep 2; done > /tmp/fifoIn
I've tested it with this "server" (I'm using openbsd netcat syntax):
我已经用这个“服务器”测试过它(我使用的是 openbsd netcat 语法):
nc -l localhost 2222
回答by DHS
After a lot of trying and pulling my hair I finally figured out that I could use NCatinstead of Netcat as NCat can execute a command.
经过大量的尝试和拉扯,我终于发现我可以使用NCat而不是 Netcat,因为 NCat 可以执行命令。
Start a connection with NCat to my socket server on port 5000
and execute the script ./sendlines.sh
:
使用 NCat 开始连接到端口上的套接字服务器5000
并执行脚本./sendlines.sh
:
ncat --exec "./sendlines.sh" 192.168.1.10 5000
ncat --exec "./sendlines.sh" 192.168.1.10 5000
./sendlines.sh
will send 4 lines with a delay of two seconds between each line:
./sendlines.sh
将发送 4 行,每行之间延迟两秒:
#!/bin/bash
#
# sendlines.sh, v1.00, initial release
#
i="0"
while [ $i -lt 4 ]
do
echo -ne "\x00e\x00\x0000370513,6598,no,8,,2z\x00"
sleep 2
i=$[$i+1]
done
I have not figured out how to send a binary file, line by line, but this is not strictly necessary as I can manage by sending the same string many times.
我还没有弄清楚如何逐行发送二进制文件,但这并不是绝对必要的,因为我可以通过多次发送相同的字符串来进行管理。
If you know a way to send a binary file, line by line, it would be great as it would be the best solution for me.
如果您知道一种逐行发送二进制文件的方法,那就太好了,因为这对我来说是最好的解决方案。