是否可以仅使用 shell 侦听 TCP 端口,而无需其他工具?

Dmi*_*iov 6 networking shell tcp linux-networking shell-scripting

我需要一个带有 MISP 处理器的非常小的嵌入式系统上的非常简单的 Web 服务器。我认为最简单的服务器可能是一个监听 TCP 端口的 shell 脚本。

问题是系统甚至没有perl。只有一些基本的外壳/bin/sh。我在网上搜索了“如何从 shell 侦听端口”,但我发现的答案都提到了其他一些工具,例如nc,我没有。

甚至有可能做到吗?

系统,它是一个路由器,安装了 busybox 和/bin目录中的其他一些二进制文件,它们是:

#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
Run Code Online (Sandbox Code Playgroud)

和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
Run Code Online (Sandbox Code Playgroud)

它在 PATH 的其他位置也有其他一些东西,但没有nc,没有httpd或类似的东西

tel*_*coM 6

不,基本的 Bourne/POSIX shell ( /bin/sh) 不能包含任何用于 TCP 连接的内置工具。请参阅维基百科中命令外壳的比较

bash外壳将有一定的文件名的特殊处理的TCP和UDP客户端功能:例如,使用/dev/tcp/<hostname>/<port>在输入/输出重定向的命令行的原因bash连接到指定的主机和端口,并使用该连接作为输入源或输出目的地。但bash不能侦听端口:它不能充当 TCP 服务器。

zsh壳将有客户端和服务器的功能,但只使用TCP。另一方面,zsh不是一个简单的 shell:据我所知,它可能是功能最丰富的(也是最大的)常见的 Unix 风格的 shell。zsh在小型嵌入式系统上查找是不太可能的。

在我写答案时,您在评论中指出您发现了一个“ lighthttpd”二进制文件。也许是这个?https://www.lighthttpd.net/