1 # $OpenBSD: dynamic-forward.sh,v 1.13 2017/09/21 19:18:12 markus Exp $
2 # Placed in the Public Domain.
4 tid="dynamic forwarding"
6 FWDPORT=`expr $PORT + 1`
8 if have_prog nc && nc -h 2>&1 | grep "proxy address" >/dev/null; then
9 proxycmd="nc -x 127.0.0.1:$FWDPORT -X"
10 elif have_prog connect; then
11 proxycmd="connect -S 127.0.0.1:$FWDPORT -"
13 echo "skipped (no suitable ProxyCommand found)"
16 trace "will use ProxyCommand $proxycmd"
23 trace "start dynamic forwarding, fork to background"
25 while [ "$error" -ne 0 -a "$n" -lt 3 ]; do
27 ${SSH} -F $OBJ/ssh_config -f -$d $FWDPORT -q \
28 -oExitOnForwardFailure=yes somehost exec sh -c \
29 \'"echo \$\$ > $OBJ/remote_pid; exec sleep 444"\'
31 if [ "$error" -ne 0 ]; then
32 trace "forward failed attempt $n err $error"
36 if [ "$error" -ne 0 ]; then
37 fatal "failed to start dynamic forwarding"
41 for h in 127.0.0.1 localhost; do
42 trace "testing ssh socks version $s host $h (-$d)"
43 ${SSH} -F $OBJ/ssh_config \
44 -o "ProxyCommand ${proxycmd}${s} $h $PORT" \
45 somehost cat ${DATA} > ${COPY}
46 test -f ${COPY} || fail "failed copy ${DATA}"
47 cmp ${DATA} ${COPY} || fail "corrupted copy of ${DATA}"
51 if [ -f $OBJ/remote_pid ]; then
52 remote=`cat $OBJ/remote_pid`
53 trace "terminate remote shell, pid $remote"
54 if [ $remote -gt 1 ]; then
58 fail "no pid file: $OBJ/remote_pid"