It's a real pity that there's no standard/readily available tool for such basic task, but if you're not on some embedded system and have some scripting language like perl
or python
available, you can quickly put something together:
tcpsrv:
#! /usr/bin/perl
use strict;
use IO::Socket::INET6;
die "usage: $0 host:port { shell_cmd | cmd args ... }\n" unless @ARGV >= 2;
my $h = shift;
my $s=new IO::Socket::INET6(ReusePort=>1, Listen=>6, LocalAddr=>$h)
or die "IO::Socket::INET($h): $!";
warn "listening on ", $s->sockhost, "/", $s->sockport, "\n";
$SIG{CHLD} = sub { use POSIX qw(WNOHANG); 1 while waitpid(-1, WNOHANG) > 0 };
while(1){
my $a = $s->accept or do { die "accept: $!" unless $!{EINTR}; next };
warn "connection from ", $a->peerhost, "/", $a->peerport, "\n";
die unless defined(my $p = fork);
close($a), next if $p;
open STDIN, "<&", $a and open STDOUT, ">&", $a or die "dup: $!";
close $s and close $a or die "close: $!";
exec(@ARGV); die "exec @ARGV: $!";
}
Usage: tcpsrv host:port cmd
This will listen on host:port
, and anytime a client connects to host:host
, it will fork & exec cmd
with its stdin and stdout redirected from/to the connection:
tcpsrv :9999 ls .
tcpsrv 127.0.0.1:7000 uptime
tcpsrv [::]:7000 uptime
tcpsrv 88.109.110.161:2000 'cat > port2000.txt'
haproxy
in front of the servelets to direct tcp streams based on some filterable information or in a load balance configuration with maxconn = 1 – crasic Apr 21 '19 at 20:24