wok annotate dropbear/stuff/sshx @ rev 19919

dropbear/sshx: add proxy ssh support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Apr 19 17:06:08 2017 +0200 (2017-04-19)
parents 2c28bb804604
children 0bbf3f773324
rev   line source
pascal@10917 1 #!/bin/sh
pascal@10917 2
pascal@10917 3 xdbclient()
pascal@10917 4 {
pascal@14027 5 read host data
pascal@14027 6 svr=${DISPLAY#*:}
pascal@11597 7 n=$((10+($RANDOM % 90))); dpy=localhost:$n
pascal@14027 8 scr=${DISPLAY/${DISPLAY%.*}/$dpy}
pascal@19919 9 r="" ; p="22"; k=""
pascal@11597 10 while true; do
pascal@14027 11 a="$1"
pascal@14027 12 r="$r $1"; shift
pascal@14027 13 case "$a" in
pascal@19919 14 -p) p=$1 ;;
pascal@19919 15 -i) k="$k-i $1 " ;;
pascal@19919 16 esac
pascal@19919 17 case "$a" in
pascal@14027 18 -[piIlLRWKBJ]) r="$r $1"; shift;;
pascal@11597 19 -*) ;;
pascal@11597 20 *) break ;;
pascal@11597 21 esac
pascal@11597 22 done
pascal@19839 23 xhost +localhost > /dev/null 2>&1
pascal@19919 24 pxypass="${DBPROXY_PASSWORD:+export DROPBEAR_PASSWORD=$DBPROXY_PASSWORD; }"
pascal@15886 25 [ -n "$1" ] || set -- "${TERM:-xterm} -ls || ${TERM:-sakura} -l || xterm -ls"
pascal@19840 26 exec dbclient -f -R $((6000+$n)):localhost:$((6000+${svr%.*})) $r \
pascal@19919 27 ${DBPROXY:+-J "sh -c '${pxypass}dbclient ${k}-B ${a#*@}:$p $DBPROXY'"} \
pascal@14027 28 "xauth add $dpy $data; export DISPLAY=$scr; $@ ; xauth remove $dpy" \
pascal@14027 29 </dev/null >/dev/null
pascal@10917 30 }
pascal@10917 31
pascal@14027 32 pppssh()
pascal@14027 33 {
pascal@18015 34 ssh="dbclient -y"
pascal@18015 35 $ssh $1 true || {
pascal@18015 36 [ -z "$DROPBEAR_PASSWORD" ] && echo -n "ssh password: " &&
pascal@18015 37 read -s -t 30 DROPBEAR_PASSWORD && export DROPBEAR_PASSWORD
pascal@18015 38 $ssh $1 true || exit 1
pascal@18015 39 }
pascal@19355 40 extra="$(route -n | awk -vd=$(nslookup ${1#*@} | sed '/::/d' | \
pascal@18042 41 sed '$!d;s/.*: \([^ ]*\).*/\1/') \
pascal@18036 42 '$1=="0.0.0.0"{ print d " gw " $2 " dev " $8 }')"
pascal@18036 43 for i in ${4/default/128.0.0.0/1 0.0.0.0/1} ; do
pascal@15886 44 echo "route add -net $i dev \$1"
pascal@15886 45 done > /etc/ppp/ip-up.d/pppssh$$
pascal@18042 46 [ "$4" ] && echo "route add $extra" >> /etc/ppp/ip-up.d/pppssh$$
pascal@15886 47 chmod +x /etc/ppp/ip-up.d/pppssh$$
pascal@14027 48 n=10.$(($$%256)).$(($$/256))
pascal@19355 49 n="${2:-$n.1:$n.2} passive"
pascal@15886 50 ppp="/usr/sbin/pppd local noauth nodetach"
pascal@19355 51 arg="$n pty '$ssh $1 $ppp ${3:-proxyarp} notty'"
pascal@19830 52 pppsvr="$ppp $n ${3:-proxyarp} notty >/dev/null"
pascal@19830 53 sshcmd="nc -ulp $5 -e $pppsvr"
pascal@19830 54 udpcmd="nc -u ${1#*@} $5"
pascal@19830 55 [ "$6" == "test" ] && sshcmd="nc -ulp $5 -e nc localhost 22" &&
pascal@19840 56 udpcmd="dbclient -y -J \"nc -u ${1#*@} $5\" ${1%@*}@localhost $pppsvr"
pascal@19830 57 [ -n "$5" ] && arg="noipdefault pty '$udpcmd'" && $ssh -f $1 "$sshcmd"
pascal@19355 58 eval $ppp $arg ipparam pppssh$$
pascal@18036 59 [ "$4" ] && route del $extra
pascal@19355 60 rm -f /etc/ppp/ip-up.d/pppssh$$ 2> /dev/null
pascal@14027 61 }
pascal@14027 62
pascal@18330 63 xfbvnc()
pascal@18330 64 {
pascal@18330 65 p=5900
pascal@18330 66 while netstat -tln | grep -q ":$p " ; do p=$(($p+1)); done
pascal@18331 67 [ -r /dev/input/mice ] || echo "Warning: Cannot read mouse input." 1>&2
pascal@19830 68 svr=${1#*/}; [ "$svr" == "$1" ] && svr=127.0.0.1
pascal@19840 69 dbclient -y -f -N -L $p:$svr:${2:-5900} ${1%/*}
pascal@18330 70 fbvnc 127.0.0.1 $p
pascal@18330 71 kill $(netstat -tlnp 2> /dev/null | \
pascal@18330 72 sed "/:$p /!d;s|.* \\([0-9]*\\)/[^ ]*|\\1|")
pascal@18330 73 }
pascal@18330 74
pascal@14027 75 case "$(basename $0)" in
pascal@18330 76 *vnc) [ -z "$1" ] && cat <<EOT || xfbvnc "$@" ;;
pascal@19830 77 Usage: $0 '[sshargs] user@remote[/lanip]' [vncport]
pascal@18332 78 $(fbvnc --help 2>&1 | sed '1,/Usage:/d')
pascal@18330 79 EOT
pascal@15886 80 ppp*) [ -z "$1" ] && cat <<EOT || pppssh "$@" ;;
pascal@19355 81 Usage: $0 '[sshargs] user@remote' '[localip:remoteip] [localpppargs]' 'remotepppargs' 'routes' [udp-port]
pascal@15886 82 Example: $0 tux@slitaz.org '' '' '192.168.1.0/24 10.1.2.0/24'
pascal@15886 83 EOT
pascal@19840 84 *) [ -z "$DISPLAY" ] && exec dbclient -y "$@"
pascal@19840 85 xauth list $DISPLAY | xdbclient -y "$@"
pascal@10917 86 esac