Commands to Configure unix telnet to show and receive hex charactersWhat does the number in parentheses shown after Unix command names in manpages mean?Code golf - hex to (raw) binary conversionWhat is a unix command for deleting the first N characters of a line?Communicating with an SSH Server for file access using pure JavaTrack the time a command takes in UNIX/LINUX?how to Display Hex data instead of text?Getting file in hex format - my output vs xxd command outputCant receive Hex response from Imatic boardnull or Hex serial port encodingDoing Binary and/or Hexidecimal calculations in Excel
What is the command to reset a PC without deleting any files
Is there a familial term for apples and pears?
Do airline pilots ever risk not hearing communication directed to them specifically, from traffic controllers?
Why are only specific transaction types accepted into the mempool?
"which" command doesn't work / path of Safari?
Motorized valve interfering with button?
Japan - Plan around max visa duration
What typically incentivizes a professor to change jobs to a lower ranking university?
When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?
Can I interfere when another PC is about to be attacked?
A Journey Through Space and Time
How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?
A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?
Work Breakdown with Tikz
Why is "Reports" in sentence down without "The"
How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?
If Manufacturer spice model and Datasheet give different values which should I use?
Why don't electromagnetic waves interact with each other?
Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?
How long does it take to type this?
Example of a relative pronoun
How does one intimidate enemies without having the capacity for violence?
How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?
Why are 150k or 200k jobs considered good when there are 300k+ births a month?
Commands to Configure unix telnet to show and receive hex characters
What does the number in parentheses shown after Unix command names in manpages mean?Code golf - hex to (raw) binary conversionWhat is a unix command for deleting the first N characters of a line?Communicating with an SSH Server for file access using pure JavaTrack the time a command takes in UNIX/LINUX?how to Display Hex data instead of text?Getting file in hex format - my output vs xxd command outputCant receive Hex response from Imatic boardnull or Hex serial port encodingDoing Binary and/or Hexidecimal calculations in Excel
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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.
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.
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 "x11x22x33" | nc 127.0.0.1 port
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?
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?
It would be perfect if the realterm program currently works in linux without the need of wine.
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.
unix terminal binary hex hexdump
add a comment |
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.
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.
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 "x11x22x33" | nc 127.0.0.1 port
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?
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?
It would be perfect if the realterm program currently works in linux without the need of wine.
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.
unix terminal binary hex hexdump
add a comment |
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.
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.
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 "x11x22x33" | nc 127.0.0.1 port
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?
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?
It would be perfect if the realterm program currently works in linux without the need of wine.
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.
unix terminal binary hex hexdump
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.
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.
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 "x11x22x33" | nc 127.0.0.1 port
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?
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?
It would be perfect if the realterm program currently works in linux without the need of wine.
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.
unix terminal binary hex hexdump
unix terminal binary hex hexdump
asked Mar 9 at 3:45
MikeMike
1,356717
1,356717
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 ...
.
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.
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
.
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)
Is there something better? I mean I tried xxd now without piping and it does convert hex to binary output but the remote doesn't seem to recognize it when passed through nc. and in all cases of nc, I'm using the same port number as well as with the server.
– Mike
Mar 10 at 2:12
Ugh, sorry, the pipe causesxxd
to buffer its output. As an ugly workaround you can run( while : ; do xxd -r -p ; done ) | nc 127.0.0.1 port | od -tx1 -w30
and hit control-D whenever you've typed in a series of hex digits you want to have sent. To get away from that ugliness I'd probably write a Python script to do the whole thing in one program. I'll give that a try later, will post a comment when I have it working.
– ottomeister
Mar 10 at 3:46
Updated my answer with a Python program. It's Python 2 because I'm lazy, but it wouldn't be hard to get it working in Python 3 if needed.
– ottomeister
Mar 12 at 19:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073784%2fcommands-to-configure-unix-telnet-to-show-and-receive-hex-characters%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 ...
.
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.
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
.
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)
Is there something better? I mean I tried xxd now without piping and it does convert hex to binary output but the remote doesn't seem to recognize it when passed through nc. and in all cases of nc, I'm using the same port number as well as with the server.
– Mike
Mar 10 at 2:12
Ugh, sorry, the pipe causesxxd
to buffer its output. As an ugly workaround you can run( while : ; do xxd -r -p ; done ) | nc 127.0.0.1 port | od -tx1 -w30
and hit control-D whenever you've typed in a series of hex digits you want to have sent. To get away from that ugliness I'd probably write a Python script to do the whole thing in one program. I'll give that a try later, will post a comment when I have it working.
– ottomeister
Mar 10 at 3:46
Updated my answer with a Python program. It's Python 2 because I'm lazy, but it wouldn't be hard to get it working in Python 3 if needed.
– ottomeister
Mar 12 at 19:00
add a comment |
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 ...
.
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.
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
.
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)
Is there something better? I mean I tried xxd now without piping and it does convert hex to binary output but the remote doesn't seem to recognize it when passed through nc. and in all cases of nc, I'm using the same port number as well as with the server.
– Mike
Mar 10 at 2:12
Ugh, sorry, the pipe causesxxd
to buffer its output. As an ugly workaround you can run( while : ; do xxd -r -p ; done ) | nc 127.0.0.1 port | od -tx1 -w30
and hit control-D whenever you've typed in a series of hex digits you want to have sent. To get away from that ugliness I'd probably write a Python script to do the whole thing in one program. I'll give that a try later, will post a comment when I have it working.
– ottomeister
Mar 10 at 3:46
Updated my answer with a Python program. It's Python 2 because I'm lazy, but it wouldn't be hard to get it working in Python 3 if needed.
– ottomeister
Mar 12 at 19:00
add a comment |
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 ...
.
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.
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
.
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)
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 ...
.
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.
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
.
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)
edited Mar 12 at 18:55
answered Mar 10 at 1:25
ottomeisterottomeister
2,90721320
2,90721320
Is there something better? I mean I tried xxd now without piping and it does convert hex to binary output but the remote doesn't seem to recognize it when passed through nc. and in all cases of nc, I'm using the same port number as well as with the server.
– Mike
Mar 10 at 2:12
Ugh, sorry, the pipe causesxxd
to buffer its output. As an ugly workaround you can run( while : ; do xxd -r -p ; done ) | nc 127.0.0.1 port | od -tx1 -w30
and hit control-D whenever you've typed in a series of hex digits you want to have sent. To get away from that ugliness I'd probably write a Python script to do the whole thing in one program. I'll give that a try later, will post a comment when I have it working.
– ottomeister
Mar 10 at 3:46
Updated my answer with a Python program. It's Python 2 because I'm lazy, but it wouldn't be hard to get it working in Python 3 if needed.
– ottomeister
Mar 12 at 19:00
add a comment |
Is there something better? I mean I tried xxd now without piping and it does convert hex to binary output but the remote doesn't seem to recognize it when passed through nc. and in all cases of nc, I'm using the same port number as well as with the server.
– Mike
Mar 10 at 2:12
Ugh, sorry, the pipe causesxxd
to buffer its output. As an ugly workaround you can run( while : ; do xxd -r -p ; done ) | nc 127.0.0.1 port | od -tx1 -w30
and hit control-D whenever you've typed in a series of hex digits you want to have sent. To get away from that ugliness I'd probably write a Python script to do the whole thing in one program. I'll give that a try later, will post a comment when I have it working.
– ottomeister
Mar 10 at 3:46
Updated my answer with a Python program. It's Python 2 because I'm lazy, but it wouldn't be hard to get it working in Python 3 if needed.
– ottomeister
Mar 12 at 19:00
Is there something better? I mean I tried xxd now without piping and it does convert hex to binary output but the remote doesn't seem to recognize it when passed through nc. and in all cases of nc, I'm using the same port number as well as with the server.
– Mike
Mar 10 at 2:12
Is there something better? I mean I tried xxd now without piping and it does convert hex to binary output but the remote doesn't seem to recognize it when passed through nc. and in all cases of nc, I'm using the same port number as well as with the server.
– Mike
Mar 10 at 2:12
Ugh, sorry, the pipe causes
xxd
to buffer its output. As an ugly workaround you can run ( while : ; do xxd -r -p ; done ) | nc 127.0.0.1 port | od -tx1 -w30
and hit control-D whenever you've typed in a series of hex digits you want to have sent. To get away from that ugliness I'd probably write a Python script to do the whole thing in one program. I'll give that a try later, will post a comment when I have it working.– ottomeister
Mar 10 at 3:46
Ugh, sorry, the pipe causes
xxd
to buffer its output. As an ugly workaround you can run ( while : ; do xxd -r -p ; done ) | nc 127.0.0.1 port | od -tx1 -w30
and hit control-D whenever you've typed in a series of hex digits you want to have sent. To get away from that ugliness I'd probably write a Python script to do the whole thing in one program. I'll give that a try later, will post a comment when I have it working.– ottomeister
Mar 10 at 3:46
Updated my answer with a Python program. It's Python 2 because I'm lazy, but it wouldn't be hard to get it working in Python 3 if needed.
– ottomeister
Mar 12 at 19:00
Updated my answer with a Python program. It's Python 2 because I'm lazy, but it wouldn't be hard to get it working in Python 3 if needed.
– ottomeister
Mar 12 at 19:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073784%2fcommands-to-configure-unix-telnet-to-show-and-receive-hex-characters%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown