Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an Echo server with Bash?

Tags:

bash

tcp

netcat

How to write a echo server bash script using tools like nc, echo, xargs, etc capable of simultaneously processing requests from multiple clients each with durable connection?

The best that I've came up so far is

nc -l -p 2000 -c 'xargs -n1 echo' 

but it only allows a single connection.

like image 235
Roskoto Avatar asked Dec 04 '11 14:12

Roskoto


People also ask

What is a TCP Echo server?

TCP Echo Server In the TCP Echo server , we create a socket and bind to a advertized port number. After binding , the process listens for incoming connections. Then an infinite loop is started to process the client requests for connections.

What is the default port for the echo server?

TCP: The target port number of the TCP echo server. This port in decimal is 9001. UDP: The target port number of the UDP echo server.

How to get the path of the ECHO command in Bash?

One is bash builtin and the second one is an external command. NOTE: Always builtin version takes precedence over external command. Use the type command to get the path information about the echo command. To get the list of options supported for the echo command, use the help option.

How to display the initial Server Message in simple echo server?

In the main method, an initial server message will be displayed: public class SimpleEchoServer { public static void main (String [] args) { System.out.println ("Simple Echo Server"); ... } } The remainder of the method's body consists of a series of try blocks to handle exceptions.

How do I create a Bash web server?

A Bash web server can be created using the nc or netcat, the networking utility: This Bash statement echo’s to port 8080, the output is an HTTP header with the file content length defined. The cat command is used to show the HTML file.

How do I Turn Off backslash in echo command?

By default when you run the echo command new line character is automatically appended at the end. If you want to suppress this behavior use -n flag. By using the -E flag, the echo statement will treat all backslash-escaped characters as plain text.


2 Answers

If you use ncat instead of nc your command line works fine with multiple connections but (as you pointed out) without -p.

ncat -l 2000 -k -c 'xargs -n1 echo' 

ncat is available at http://nmap.org/ncat/.

P.S. with the original the Hobbit's netcat (nc) the -c flag is not supported.

Update: -k (--keep-open) is now required to handle multiple connections.

like image 195
David Costa Avatar answered Sep 23 '22 17:09

David Costa


Here are some examples. ncat simple services

TCP echo server

ncat -l 2000 --keep-open --exec "/bin/cat" 

UDP echo server

ncat -l 2000 --keep-open --udp --exec "/bin/cat" 
like image 23
douyw Avatar answered Sep 22 '22 17:09

douyw