配置unix telnet以显示和接收十六进制字符的命令 [英] Commands to Configure unix telnet to show and receive hex characters
问题描述
我在本地服务器上有一个特殊的应用程序(来自ucsim的8051模拟器),该应用程序仅在一个端口上传输并期望不可打印的字符.本质上,我正在定义自己的数据格式.
I have a special application (8051 simulator from ucsim) on a local server that transmits and expects non-printable characters on only one port. Essentially, I'm defining my own data format.
如果我可以看到返回到30列的每个字符的十六进制代码,并且能够键入要输出的字符的十六进制代码,则Telnet将是完美的程序.问题是,Linux附带的telnet仅允许1行十六进制字符,并且在接收到某些字符时可能会改变行为.
Telnet would have been the perfect program if I can see the hex codes of each character returned back in 30 columns as well as being able to type hex codes of characters going out. Problem is, the telnet shipped with Linux only allows 1 row of hex characters and may alter behaviour when certain characters are received.
到目前为止,我最接近的解决方案是在wine中运行realterm程序并选择十六进制显示,但是它的问题是,如果我切换窗口,它会锁定.直到我停止服务器发送和接收字符.因此,我正在寻找针对所有这些的本机linux解决方案.
So far, the closest solution I have is to run the realterm program in wine and choose hex display, but the problem with it is that it locks up if I switch windows. until I stop the server from transmitting and receiving characters. So I'm looking for a native linux solution to all this.
对于接收数据,我可以轻松使用以下命令:
As for receiving data, I can easily get away with this command:
nc 127.0.0.1 port | od -tx1 -w30
但是,要与正在流出的数据在同一开放端口上发送数据时,请尝试以下命令:
But when it comes to sending data on the same open port as what data is flowing out on, I try this command:
echo -e -n"\ x11 \ x22 \ x33" | nc 127.0.0.1端口
echo -e -n "\x11\x22\x33" | nc 127.0.0.1 port
其中11、22、33是要发送回服务器的十六进制数字.问题是当我尝试执行此命令时,它停顿了,必须按CTRL + C退出.
where 11, 22, 33, are hexadecimal digits to send back out to the server. Problem is when I try this command, it just stalls and I have to press CTRL+C to exit.
我的逻辑在这里正确吗?
Is my logic correct here?
还是我可以在Unix中使用更好的命令来查看来自服务器的十六进制字符,该服务器传输二进制并且还传输在本地控制台键入的十六进制字符的二进制代码?
or are there better commands I can use in unix to see hexadecimal characters from a server that transmits binary and also transmit binary codes of the hexadecimal characters typed in at the local console?
如果realterm程序当前可以在Linux上运行而不需要Wine,那将是完美的.
It would be perfect if the realterm program currently works in linux without the need of wine.
P.S.很抱歉,如果这个论坛不是这个问题的理想之地,但是就我个人而言,如果要获得所要实现的功能,我不介意制作一个包含50个命令的Unix脚本,因为我正在寻找的应用程序对于Linux还没有.
P.S. I'm sorry if this forum isn't the perfect place for this question, but personally, I wouldn't mind making a unix script of 50 commands if that's what it takes to get what I achieve, because the application I'm looking for has yet to exist for linux.
推荐答案
nc
将从其标准输入中读取并通过网络连接发送该数据.因此,如果您只是希望能够在终端中键入十六进制数字并将相应的二进制文件发送到模拟器,则可以使用类似xxd -r -p
的方式将数据传递到nc
中. IE.将nc 127.0.0.1 ...
更改为xxd -r -p | nc 127.0.0.1 ...
.
nc
will read from its standard input and send that data over the network connection. So if you simply want to be able to type hex digits into the terminal and have the corresponding binary sent to the simulator, you could use something like xxd -r -p
to pipe data into nc
. I.e. change nc 127.0.0.1 ...
to xxd -r -p | nc 127.0.0.1 ...
.
然后您可以在xxd
命令中键入"41 42 43 44"(后跟return
,因为交互式输入是行缓冲的),并且应该将"ABCD"发送到模拟器. xxd
在其-r -p
模式下会将任何非十六进制数字都视为分隔符,因此如果需要,可以安全地在成对的十六进制数字之间放置空格以提高可读性.
You can then type "41 42 43 44" (followed by return
, because interactive input is line-buffered) into the xxd
command and that should deliver "ABCD" to the simulator. xxd
in its -r -p
mode treats any non-hex digits as separators, so it's safe to put spaces between pairs of hex digits for readability if you want.
如果您希望能够在开发会话过程中从各种来源(交互的,从文件接收的)发送不同类型的数据(十六进制,二进制),那么您可以建立第二个持久性nc
侦听器在另一个端口上,以收集该数据并将其馈入现有nc
的标准输入.
If you wanted to be able to send different types of data (hex, binary) from a variety of sources (interactive, cat'ed from files) during the course of a development session then you could probably rig up a second persistent nc
listener on a different port to collect that data and feed it into the stdin of the existing nc
.
使用Python程序进行更新,该程序将从标准输入中读取十六进制并通过网络连接发送相应的二进制文件,并将从连接中读取数据并将十六进制形式写入标准输出.将其另存为nethex.py
之类,并以python nethex.py <host> <port>
形式调用.
Update with a Python program that will read hex from stdin and send the corresponding binary over a network connection, and will read data from the connection and write is as hex to standard output. Save it as something like nethex.py
and invoke it as python nethex.py <host> <port>
.
#!/usr/bin/env python
import binascii
import re
import socket
import sys
import threading
def breakstring(string, maxchars):
return [ string[pos:(pos + maxchars)]
for pos in xrange(0, len(string), maxchars) ]
def to_hex(bytes):
lines = breakstring(binascii.hexlify(bytes), 32)
return ''.join([ (' '.join(breakstring(line, 2)) + '\n') for line in lines ])
def from_hex(s):
hexlist = re.sub('[^0-9A-Fa-f]', ' ', s).split()
pairs = [ '0' + hexstr if len(hexstr)%2 else hexstr for hexstr in hexlist ]
return binascii.unhexlify(''.join(pairs))
def connect(addr, port):
conn = None
infos = socket.getaddrinfo(addr, port, 0, socket.SOCK_STREAM)
for info in infos:
fam, typ, proto, fqdn, sa = info
try:
conn = socket.socket(fam, typ, proto)
except socket.error:
sys.stderr.write('socket: ' + str(se) + '\n')
continue
try:
conn.connect(sa)
except socket.error as se:
sys.stderr.write('connect: ' + str(se) + '\n')
conn.close()
conn = None
continue
break
return conn
class SockDrainer(threading.Thread):
def __init__(self, sock, sink):
super(SockDrainer, self).__init__()
self.sock = sock
self.sink = sink
def run(self):
drain(self.sock, self.sink)
def drain(sock, dest):
while True:
data = sock.recv(2048)
if not data:
break
outdata = to_hex(data)
dest.write(outdata)
dest.flush()
def fill(sock, source):
while True:
data = source.readline()
if not data:
break
try:
outdata = from_hex(data)
sock.send(outdata)
except socket.error:
break
try:
sock.shutdown(socket.SHUT_WR)
except socket.error:
pass
def nethex(addr, port):
conn = connect(addr, port)
if conn is not None:
drainer = SockDrainer(conn, sys.stdout)
drainer.daemon = True # thread will not block process termination
drainer.start()
fill(conn, sys.stdin)
drainer.join() # wait for rx'ed data to be handled
conn.close()
return conn is not None
if __name__ == '__main__':
result = nethex(sys.argv[1], sys.argv[2])
sys.exit(0 if result else 1)
这篇关于配置unix telnet以显示和接收十六进制字符的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!