Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
sending a hex string to a remote via netcat?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
afabco
Guru
Guru


Joined: 24 Feb 2004
Posts: 380

PostPosted: Wed Jul 30, 2008 11:29 pm    Post subject: sending a hex string to a remote via netcat? Reply with quote

Hey folks,

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

Code:
nc -v 192.168.200.34 19000


and it sits there happy as a clam.

The hex string I need to send is something like:

Quote:
05:64:10:D3:FD:00:00:00:E7:30:F5:C6:01:3C:02:07:1E:3C:03:07:1E:E7:59


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?

thanks!
_________________
Anyone who puts a small gloss on a fundamental technology, calls it proprietary, and then tries to keep others from building on it, is a thief.
-Tim O'Reilly
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23087

PostPosted: Thu Jul 31, 2008 3:12 am    Post subject: Reply with quote

Netcat will forward its input unmodified. Your best bet would be to find or make a tool that reads hexadecimal numbers and prints the corresponding bytes onto its standard output. Then do helper-cmd | nc 192.168.200.34 19000.

If you could write the data in base64 instead, then the coreutils program /usr/bin/base64 could transform your data into raw bytes for the wire.
Back to top
View user's profile Send private message
coolsnowmen
Veteran
Veteran


Joined: 30 Jun 2004
Posts: 1479
Location: No.VA

PostPosted: Thu Jul 31, 2008 7:40 pm    Post subject: Reply with quote

perhaps something like this (compile with g++ filename.c)

Code:
#include <cstdio>
#include <string>
//Written by Coolsnowmen for free
int hex2int(char &hex)
{
   const static std::string key("0123456789abcdef");
   return key.find( hex | 32 );
}

int main(int argnum,char ** argin)
{
  if (argnum==2) {
   int length=strlen(argin[1]);
   if (length%2==0) {
      for (int idx =0; idx<length; idx+=2)
         printf("%c",hex2int(argin[1][idx])*16 + hex2int(argin[1][idx+1]));
      return 0;
   }
   else   {
      fprintf(stderr,"non-even length string");
   }
  } else printf("1 argument expexted, of hexpairs, eg: %s 4a4f4e --> JON",argin[0]);
  return 1;
}


testing:
Code:
./a.out 000102030405 | wc
      0       0       6

_________________
emerge: there are no ebuilds to satisfy "moo"
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum