File: tcp_proxy

package info (click to toggle)
zsh-beta 4.3.0-dev-1%2B20050424-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 18,240 kB
  • ctags: 6,564
  • sloc: ansic: 79,241; sh: 4,874; perl: 748; makefile: 676; awk: 341; sed: 16
file content (31 lines) | stat: -rw-r--r-- 849 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Listen on the given port and for every connection, start a new 
# command (defaults to $SHELL) in the background on the accepted fd.
# WARNING: this can leave your host open to the universe.  For use
# in a restricted fashion on a secure network.
#
# Remote logins are much more efficient...

local TCP_LISTEN_FD
trap '[[ -n $TCP_LISTEN_FD ]] && ztcp -c $TCP_LISTEN_FD; return 1' \
    HUP INT TERM EXIT PIPE

if [[ $1 != <-> ]]; then
    print "Usage: $0 port [cmd args... ]" >&2
    return 1
fi

integer port=$1
shift
ztcp -l $port || return 1
TCP_LISTEN_FD=$REPLY

(( $# )) || set -- ${SHELL:-zsh}
local cmd=$1
shift

while ztcp -a $TCP_LISTEN_FD; do
    # hack to expand aliases without screwing up arguments
    eval $cmd '$*  <&$REPLY >&$REPLY 2>&$REPLY &'
    # Close the session fd; we don't need it here any more.
    ztcp -c $REPLY
done