Can bash be used to create a server?

Is there a way to program the server in bash?

Basically, I want to be able to connect to a bash server from a PHP client and send messages that will be displayed in the console.

+4
source share
3 answers

First bad news

Unfortunately, there seems to be no hope of doing this in pure Bash.

Even making

exec 3<> /dev/tcp/<ip>/<port>

does not work because these special files are implemented on top connect()instead bind(). This is obvious if we look at the source.

In Bash 4.2, for example, a function _netopen4()(or _netopen6()for IPv6) is read as follows ( lib/sh/netopen.c):

  s = socket(AF_INET, (typ == 't') ? SOCK_STREAM : SOCK_DGRAM, 0);
  if (s < 0)
    {
      sys_error ("socket");
      return (-1);
    }

  if (connect (s, (struct sockaddr *)&sin, sizeof (sin)) < 0)
    {
      e = errno;
      sys_error("connect");
      close(s);
      errno = e;
      return (-1);
    }

But

, nc. .

nc -l <port>

localhost:<port>.

+2
+2

Create a process that reads from a socket, runs data through the shell, and prints the response. Perhaps the following script that listens on port 9213:

ncat -l -kp 9213 | while read line; do
    out=$($line)
    # or echo $line
    echo $out
done 

If you want to display data ncat -l -p 9213, but enough.

+1
source

Source: https://habr.com/ru/post/1538022/


All Articles