6

I need a very simple web server on a very small embedded system with a MISP processor. I thought that the simplest server could be a shell script listening to a TCP port.

The problem is that the system doesn't have even perl. Only some basic shell /bin/sh. I searched the web for "how to listen to a port from a shell", but answers I found all referred to some other tools like nc, which I don't have.

Is it even possible to do?

The system, it's a router, has busybox installed and some other binaries in the /bin directory, they are:

#ls /bin
chown        df           fatattr      gzip         login        mount
ping         rm           shuf         touch        usleep
busybox      comgt        dmesg        fgrep        hostname     ls
mv           ping6        rmdir        sleep        true         vi
busybox-new  cp           echo         flock        ip           mkdir
netstat      printf       sdparm       split        umount       watch
cat          date         egrep        grep         kill         mknod
nice         ps           sed          sync         uname        wget
chmod        dd           false        gunzip       ln           more
pidof        pwd          sh           tar          unlink       zcat

And the busybox:

Currently defined functions:
[, arp, ash, awk, basename, busybox, cat, chmod, chown, cp, crond,
cut,date, dd, df, dirname, dmesg, du, echo, egrep, env, expr, false,
fdisk, fgrep, find, free, ftpget, getty, grep, head, hexdump,
hostname, ifconfig, init, insmod, ip, kill, killall, klogd, ln,
login, ls, lsmod, md5sum, mkdir, mknod, mkswap, modprobe, more,
mount, mv, netstat, nslookup, passwd, pidof, ping, ping6, ps, pwd,
readlink, reboot, renice, rm, rmdir, rmmod, route, sed, seq, sh,
sleep, sort, swapoff, swapon, sync, sysctl, syslogd, tail, tar,
taskset, test, tftp, time, top, touch, tr, traceroute, true, udhcpd,
umount, uname, unzip, uptime, usleep, vi, watch, wc, which,
xargs, yes

It has some other stuff too in other locations from the PATH, but no nc, no httpd or something like that

5
  • Well, would be useful to know what commands/packages are available. For example: busybox has a build-in http daemon.
    – duenni
    Oct 2, 2019 at 13:38
  • @duenni I've added a list
    – d.k
    Oct 2, 2019 at 13:46
  • @duenni it says httpd: applet not found .
    – d.k
    Oct 2, 2019 at 13:58
  • 1
    Mhh, you also have a busybox-new, I wonder what is available with that. Anyway you can have multiple instances of busybox installed, so you could compile busybox yourself with the httpd option enabled and install it.
    – duenni
    Oct 2, 2019 at 14:04
  • 1
    @duenni I looked into it and it seemed to have nothing useful for me egrep, ether-wake, fatattr, fgrep, flock, grep, gunzip, gzip, nice, printf, shuf, split, udhcpc, unlink, wget, zcat. But the router has a lighthttpd binary, found it with find / -name "*http*". I'll try to research this one
    – d.k
    Oct 2, 2019 at 14:07

1 Answer 1

6

No, a basic Bourne/POSIX shell (/bin/sh) cannot be expected to include any built-in facilities for TCP connections. See comparision of command shells in Wikipedia.

The bash shell would have TCP and UDP client capabilities with a special handling of certain filenames: for example, using /dev/tcp/<hostname>/<port> in input/output redirection on a command line causes bash connect to the specified host and port and use the connection as input source or output destination. But bash cannot listen on a port: it cannot act as a TCP server.

The zsh shell would have both client and server functionality, but only using TCP. On the other hand, zsh is not a simple shell: it's probably the most feature-rich (and so the largest) of the common unix-style shells as far as I know. Finding zsh on a small embedded system would be rather unlikely.

While I was writing my answer, you indicated in the comments that you found a "lighthttpd" binary. Is it perhaps this? https://www.lighttpd.net/

2
  • thank you and duenni , yes, the lighttpd is exactly what I wanted originally, it runs ok, but the answer to the question is useful for general conceptual knowledge
    – d.k
    Oct 2, 2019 at 14:45
  • Last I check, Bash was still, by far, the largest shell. It's had many years to bloat. My bash executable is at 2.1 MB, but zsh is at only 911 KB. Now, zsh does link with 3 additional shared libraries that bash does not, libpthread, libm, and librt, but all those come with glibc.
    – penguin359
    Oct 2, 2019 at 23:25

Not the answer you're looking for? Browse other questions tagged .