tokssh 2.2 KB

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