67

I am trying to use netcat to send a simple message over TCP, e.g. I want to send the digit 1.

So I understood that I write the following in the terminal, after installing netcat

netcat [ip-address] [port]

But how do I specify the message to be sent? i.e. where to I write "1" ?

2 Answers2

79

Create file something.txt, content of file is 1

netcat [ip-address] [port] <something.txt

At destination you must have something to listen to this.

or

Server:

netcat -l -p [port]

to listen to the connection

Client:

netcat [server-ip-address] [port]

After connection is establish on cilent simple type 1 and hit enter

dessert
  • 41,116
2707974
  • 10,758
  • Submitted an edit changing the useless cat command to redirection to echo. I use this method to send string data to remote machines that are operating as one for greater bash multi-tasking. – Yokai Nov 14 '17 at 08:27
  • thnks, this is working for me – Buddhika Alwis Jul 11 '18 at 04:52
  • Just wanted to not that -p is not mandatory, port can be specified directly as non-optional argument

    netcat -l [port]

    – Do-do-new Jan 11 '23 at 10:31
39

I use:

echo text | netcat host port

So you would just need to run this:

echo 1 | netcat localhost 12345
dessert
  • 41,116
AndyGee
  • 521