Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new line issue with netcat [closed]

Tags:

linux

unix

I am using below command to send some strings to udp listening server.

echo "A 192.168.192.168" | nc -u 192.168.2.1 1234

but the server is getting trailing '\n' in echoed string.

I have tried below command too, but failed

echo "A 192.168.192.168" | nc -uC 192.168.2.1 1234

How can I remove that trailing new line character ?? Do I have any special option in nc ??

like image 247
JohnG Avatar asked Jun 30 '12 12:06

JohnG


People also ask

How do I know if Netcat is working?

Install Netcat/NcatLinux and macOS users can quickly check if a port is open in the terminal with pre-installed Nc (and Netcat on Linux). Windows users will need to install Netcat's successor, Ncat, made by the Nmap project. Both are good for seeing if a specific port is open on a local network, VPN, or server.

What does verbose mean in Netcat?

In netcat, Verbose is a mode which can be initiated using [-v] parameter. Now verbose mode generates extended information. Basically, we will connect to a server using netcat two times to see the difference between normal and verbose mode. The command is nc 192.168.17.43 21 -v.


2 Answers

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

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

On XSI-conformant systems, if the first operand is -n, it shall 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 doesnt append a new line unless it is told to, and it's standard behavior.

like image 82
c00kiemon5ter Avatar answered Oct 10 '22 11:10

c00kiemon5ter


Try using

echo -n

so

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

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

like image 23
Levon Avatar answered Oct 10 '22 10:10

Levon