15

I've got some binary commands (which I'm representing as hex) that I need to send to a remote device (it's an embedded data collection device) and observe the reply.

It's easy to connect to it with netcat

nc -v 192.168.200.34 19000

and it sits there happy as a clam.

The hex string I need to type in terminal and then send is something like:

02:45:31:38:03:34:43:0d:0a

Where 02 is STX, 03 is ETX and so on.

But when I type this into my netcat window (with or without spaces , with or without the colons) netcat transmits it as ascii.

All the literature is happy to tell me how to capture a hexdump from the remote device, but not how to transmit binary/hex data to the remote device.

Is this an easy thing to do, or am I missing something?

fl4r3
  • 161
  • 1
  • 2
  • 7

2 Answers2

29

You can do it with command: echo -n -e "\x02\x45\x31\x38\x03\x34\x43\x0d\x0a" | nc [-u] <-ip-addr-> <-port-no->

  • The -n supresses outputting the trailing newline.
  • The -e enables the interpretation of backslash escapes -> allowing us to send hex codes.
  • 'x' before each character specifies that the byte is in hexadecimal form.
  • -u switch to UDP traffic (optional)
laplasz
  • 3,359
  • 1
  • 29
  • 40
rahul1205
  • 794
  • 1
  • 6
  • 14
  • 2
    Welcome to Stack Overflow! While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Ram Koti Dec 06 '17 at 07:10
  • 1
    Thank you for your inputs @RamKoti. I added as much explanation as I could. – rahul1205 Dec 06 '17 at 12:42
  • You can also use `printf` which might make it easier to go over things in a loop. https://stackoverflow.com/a/16914660/3794873 – dragon788 Aug 24 '20 at 16:31
1

You can try this !

echo '024531380334430d0a' | xxd -r -p | nc 192.168.200.34 19000
attaboyabhipro
  • 1,450
  • 1
  • 17
  • 33