New problem with netcat

I use the command below to send some lines to the udp listening server.

echo "A 192.168.192.168" | nc -u 192.168.2.1 1234

but the server gets the completion of '\ n' in the echo line.

I also tried the command below, but failed

echo "A 192.168.192.168" | nc -uC 192.168.2.1 1234

How to remove this new line character? Do I have some special option in nc ??

+7
source share
2 answers

echo usually provides the -n flag. This is not a standard.

string A string to be written to standard output. If the first operand is -n or if any of the operands contains a backslash ('\') char - acter, the results are determined by the implementation.

On XSI-compatible systems, if the first operand is -n, it should be treated as a string, not an option.


I would suggest using printf

 printf "A 192.168.192.168" | nc -u 192.168.2.1 1234 

printf does not add a new line unless it is said, and this is standard behavior.

+9
source

Try using

 echo -n 

So

 echo -n "A 192.168.192.168" | nc -u 192.168.2.1 1234 

the echo page says: -n do not output the trailing newline

+5
source

All Articles