No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

persistent-ssh 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/sh
  2. #
  3. # Released under the terms of GPLv3 or at your option any later version.
  4. # No warranties.
  5. # Copyright 2008-2010 Enrico Tassi <gares@fettunta.org>
  6. #
  7. # lines beginning with a double '#' are considered the documentation
  8. # of the hook, and should use the markdown syntax
  9. #
  10. ## Persistent ssh connection
  11. ## =========================
  12. ##
  13. ## Ssh can share multiple sessions over a single network connection.
  14. ## This feature allows to speedup connections.
  15. ##
  16. ## The `persistent-ssh` script is a `pre-*` hook that
  17. ## starts (if necessary) a mother connection the first time that
  18. ## it is needed. To make this hook work properly, you have to
  19. ## setup ssh as explained in the following.
  20. ##
  21. ## Your `.ssh` directory should have permission `700`, and your
  22. ## `.ssh/config` file should look like this, where `smd-server-foo`
  23. ## is the `SERVERNAME` specified in your smd config file:
  24. ##
  25. ## Host smd-server-foo
  26. ## ControlPath ~/.ssh/master-socket-%l-%r@%h:%p
  27. ## ControlMaster auto
  28. ## PermitLocalCommand yes
  29. ## LocalCommand ln -sf ~/.ssh/master-socket-%l-%r@%h:%p ~/.ssh/master-socket-smd-server-foo
  30. ## BatchMode yes
  31. ## Compression yes
  32. ## Hostname your.real.server.name
  33. ## User you
  34. ##
  35. ## The key ingredient is to obtain standard name for the master socket of a
  36. ## given endpoint, in that case `~/.ssh/master-socket-smd-server-foo` for
  37. ## the endpoint `smd-server-foo`. Refer the `ssh_config` man page for a
  38. ## detailed explanation of `ControlMaster` and `ControlPath`.
  39. ##
  40. ## Note that you may want to put the first four lines also in a more
  41. ## generic configuration entry, so that every ssh connection to your
  42. ## server can benefit from connection sharing. For example, a complete
  43. ## ssh configuration file for `your.real.server.name` may look like
  44. ## the following:
  45. ##
  46. ## Host smd-server-foo
  47. ## ControlPath ~/.ssh/master-socket-%l-%r@%h:%p
  48. ## ControlMaster auto
  49. ## PermitLocalCommand yes
  50. ## LocalCommand ln -sf ~/.ssh/master-socket-%l-%r@%h:%p ~/.ssh/master-socket-smd-server-foo
  51. ## BatchMode yes
  52. ## Compression yes
  53. ## Hostname your.real.server.name
  54. ## User you
  55. ##
  56. ## Host your.real.server.name
  57. ## ControlPath ~/.ssh/master-socket-%l-%r@%h:%p
  58. ## ControlMaster auto
  59. ## PermitLocalCommand yes
  60. ## LocalCommand ln -sf ~/.ssh/master-socket-%l-%r@%h:%p ~/.ssh/master-socket-smd-server-foo
  61. when="$1"
  62. what="$2"
  63. endpoint="$3"
  64. status="$4"
  65. SMD_ROOT=$HOME/.smd
  66. . $SMD_ROOT/config.$endpoint
  67. MASTER_SOCKET=~/.ssh/master-socket-$SERVERNAME
  68. # on failure we send the mail, and create HOOK_STATUS
  69. if [ "$when" = "pre" -a ! -e $MASTER_SOCKET ]; then
  70. # we spawn ssh and put it in the background
  71. # so that all subsequent connection attempts
  72. # reuse the same socket
  73. set +e
  74. ssh -fN $SERVERNAME
  75. set -e
  76. fi
  77. # vim:set ft=sh: