linux - Passing hex to bash command in C -
i'm trying port bash script c. i'm having trouble passing hex string bash command. have far.
char buffer[512]; char mp_fmt[] = "\x00\x00\x00\x00\x00\x09\x01\x10\x00\x00\x00\x01\02\x00\x01"; sprintf(buffer,"echo -e \"\x00\x00\x00\x00\x00\x09\x01\x10\x00\x00\x00\x01\02\x00\x01\" | nc 192.168.01.22 500"); //sprintf(buffer,"echo -e \"%s\" | nc 192.168.01.22 500"); system((char *)buffer);
when run this, compiler returns
test.c:7:5: warning: embedded ‘\0’ in format [-wformat-contains-nul] sprintf(buffer,"echo \"\x00\x00\x00\x00\x00\x09\x01\x10\x00\x00\x00\x01\02\x00\x01\" | nc 192.168.01.22 500");
but when run other sprintf that's commented out, doesn't complain not working because device isn't responding.
also in bash script, how done , response device.
echo -e "\0\0\0\0\0\x9\x1\x10\0\0\0\01\02\0\x01" | nc 192.168.01.22 500
thanks reading long post.
doing this, don't want 0
bytes literal string \0
. so,
sprintf(buffer, "echo \"\\0\\0\\0\\0\\0\\x9\\x10 [...]
would work.
that being said, there other issues.
the simple 1 is: buffer for? don't have any formatting, use string of command literally!
next: using echo
c? really? fork 3(!) processes this. @ least, use popen()
nc , fwrite()
input string there.
even better: send data socket. there perfecly fine bsd sockets
api that, no need call external tool.
[edit]: using sockets yourself, can send raw 0
bytes.
[edit2]: sketchup code
#include <sys/socket.h> #include <netinet/in.h> [...] int fd; struct sockaddr_in sin; char mp_fmt[] = "\x00\x00\x00\x00\x00\x09\x01\x10\x00\x00\x00\x01\x02\x00\x01"; fd = socket(af_inet, sock_stream, 0); sin.sin_family = af_inet; sin.sin_addr.s_addr = inet_addr("192.168.1.22"); sin.sin_port = htons(500); connect(fd, (struct sockaddr *)&sin,sizeof(struct sockaddr_in)); write(fd, mp_fmt, 15); shutdown(fd, shut_rdwr); close(fd);
Comments
Post a Comment