dynamic-forward.sh 1.5 KB

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