|
1 | 1 | #!/bin/bash
|
| 2 | +set -e |
| 3 | +function help { |
| 4 | + cat <<EOF |
| 5 | +A simple wrapper to use like SSH |
2 | 6 |
|
3 |
| -# |
4 |
| -# A simple wrapper to use like SSH |
5 |
| -# Usage: |
6 |
| -# tokssh user@5A40C3443ABD6E1DDEE682E83F84A4D556C24C22D2230DCC141A4723C123473C171A4D9C4054 |
7 |
| -# tokssh 5A40C3443ABD6E1DDEE682E83F84A4D556C24C22D2230DCC141A4723C123473C171A4D9C4054 |
8 |
| -# tokssh -p 2222 -o ForwardAgent=yes user@5A40C3443ABD6E1DDEE682E83F84A4D556C24C22D2230DCC141A4723C123473C171A4D9C4054 |
9 |
| -# tokssh user@5A40C3443ABD6E1DDEE682E83F84A4D556C24C22D2230DCC141A4723C123473C171A4D9C4054 -s TuNToXSeCreT |
10 |
| -# |
| 7 | +Usage: |
| 8 | + tokssh [ssh options] [user@]address [-s secret] |
| 9 | +
|
| 10 | +where |
| 11 | +
|
| 12 | + ssh options: options to pass to ssh process |
| 13 | + user: login on remote host |
| 14 | + address: either a ToxID or a hostname. ~/.tuntox/hosts is read to map |
| 15 | + hostname to ToxID. hostname MUST resolve to 127.0.0.1 |
| 16 | +
|
| 17 | + -s optional secret to use to connect to tuntox server |
| 18 | +
|
| 19 | +examples: |
| 20 | + tokssh user@5A40C3443ABD6E1DDEE682E83F84A4D556C24C22D2230DCC141A4723C123473C171A4D9C4054 |
| 21 | + tokssh 5A40C3443ABD6E1DDEE682E83F84A4D556C24C22D2230DCC141A4723C123473C171A4D9C4054 |
| 22 | + tokssh -p 2222 -o ForwardAgent=yes user@5A40C3443ABD6E1DDEE682E83F84A4D556C24C22D2230DCC141A4723C123473C171A4D9C4054 |
| 23 | + tokssh user@5A40C3443ABD6E1DDEE682E83F84A4D556C24C22D2230DCC141A4723C123473C171A4D9C4054 -s TuNToXSeCreT |
| 24 | +
|
| 25 | +files: |
| 26 | + ~/.tuntox/hosts maps hostname to ToxID and optional secret. |
| 27 | + format is |
| 28 | + hostname ToxID secret(optional) |
| 29 | +EOF |
| 30 | +} |
| 31 | + |
| 32 | +strargs="'$*'" |
| 33 | +if [ -z "${strargs##*-h*}" ] || [ -z "${strargs##*--help*}" ] ;then |
| 34 | + help |
| 35 | + exit |
| 36 | +fi |
11 | 37 |
|
12 | 38 | array=( $@ )
|
13 | 39 | len=${#array[@]}
|
14 |
| -userhost=${array[$len-1]} |
15 |
| -args=${array[@]:0:$len-1} |
16 | 40 |
|
17 |
| -if [ "${array[$len-2]}" == "-s" ] |
| 41 | +if [ $len -lt 1 ]; then |
| 42 | + help |
| 43 | + exit |
| 44 | +fi |
| 45 | + |
| 46 | + |
| 47 | +# look for secret and remvove it from args |
| 48 | +if [ $len -gt 2 ] && [ "${array[$len-2]}" == "-s" ] |
18 | 49 | then
|
19 |
| - secret="${array[@]:$len-2:$len-1}" |
20 |
| - len=$[len-2] |
| 50 | + secret="${array[@]:$len-2:$len-1}" |
| 51 | + len=$[len-2] |
21 | 52 | fi
|
22 | 53 |
|
| 54 | +userhost=${array[$len-1]} |
| 55 | +args=${array[@]:0:$len-1} |
23 | 56 |
|
| 57 | +# check for user@id |
24 | 58 | arruserhost=(${userhost//@/ })
|
25 | 59 | arruserhostlen=${#arruserhost[@]}
|
26 | 60 |
|
27 | 61 | if [ $arruserhostlen -gt 1 ]
|
28 | 62 | then
|
29 |
| -# last argument is user@toxid |
30 |
| - user=${arruserhost[0]} |
31 |
| - toxid=${arruserhost[1]} |
32 |
| - ssh -o ProxyCommand="tuntox -i $toxid -W 127.0.0.1:%p $secret" $args $user@localhost |
| 63 | + # last argument is user@toxid |
| 64 | + user="${arruserhost[0]}@" |
| 65 | + toxid=${arruserhost[1]} |
| 66 | + hostname=localhost |
33 | 67 | else
|
34 |
| -# last argument is just toxid |
35 |
| - ssh -o ProxyCommand="tuntox -i $userhost -W 127.0.0.1:%p $secret" $args localhost |
| 68 | + # last argument is just toxid |
| 69 | + user="" |
| 70 | + toxid=$userhost |
| 71 | + hostname=localhost |
| 72 | +fi |
| 73 | + |
| 74 | +#search toxid in ~/.tuntox/hosts and map it to toxid |
| 75 | +if [ -f ~/.tuntox/hosts ]; then |
| 76 | + while read c_hostname c_toxid c_secret; do |
| 77 | + if [ "${c_hostname:0:1}" != "#" ] && [ "$c_hostname" == "$toxid" ]; then |
| 78 | + toxid="$c_toxid" |
| 79 | + if [ "$secret" == "" ]; then |
| 80 | + secret="-s $c_secret" |
| 81 | + fi |
| 82 | + break |
| 83 | + fi |
| 84 | + done < ~/.tuntox/hosts |
36 | 85 | fi
|
37 | 86 |
|
| 87 | +ssh -o ProxyCommand="tuntox -i $toxid -W 127.0.0.1:%p $secret" $args ${user}${hostname} |
0 commit comments