Advertisement
Guest User

persistant ssh

a guest
Jul 28th, 2014
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.37 KB | None | 0 0
  1. #!/bin/bash
  2. # Script: rssht (Reverse SSH Tunnel)
  3. # Author: Khizer Naeem (khizernaeem(x)gmail.com)
  4. # Created : 09 Sep 2012
  5. # Version: 1.03
  6. # Latest Revision: 08 Feb 2013
  7. # Tested on: Centos/RHEL 6, Centos/RHEL 5,
  8. # Description: A bash script to maintain reverse ssh tunnel with remote port forwardings.
  9. # URL: http://kxr.me/scripts/rssht_v1.03
  10. #
  11. # Copyright (C) 2013 Khizer Naeem All rights reserved.
  12. # This copyrighted material is made available to anyone wishing to use,
  13. # modify, copy, or redistribute it subject to the terms and conditions
  14. # of the GNU General Public License v.2.
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18.  
  19. #############################
  20. ## CONFIGURATION VARIABLES ##
  21. #############################
  22.  
  23. REMOTE_HOST=live.host.com # The remote host to which you want to forward the ports
  24. REMOTE_SSH_PORT=22 # SSH port of the remote host
  25. REMOTE_USER=root # SSH user for the remote host (Should have password less login from this machine with this user)
  26. SSH_RUN_DIR=/var/run/rssht/ # Directory to keep ssh socket connection and pid file (Should exist)
  27. PORTS_REMOTELY_ACCESSIBLE=yes # If set to yes, -g switch will be used while making the ssh connection. Read ssh manual for detail of -g
  28. SSH_BIN=/usr/bin/ssh # Location of the ssh client on this host
  29. REFRESH_SOCKET=0 # If set to a non-zero integer, this script will restart the ssh session if its that many minutes old.
  30.  
  31. ###########################
  32. ## PORT FORWARDING TABLE ##
  33. ###########################
  34. REMOTE_FWDS=(
  35. 10122:localhost:22
  36. 10180:localhost:80
  37. )
  38.  
  39. #######################
  40. ## SSH OPTIONS TABLE ##
  41. #######################
  42. # These options will be passed to ssh with the -o switch e.g -o “ControlMaster yes”
  43. # You can comment this out if you want the script to obey the ssh config
  44. SSH_OPTS=(
  45. ‘ControlMaster yes
  46. ‘PreferredAuthentications publickey’
  47. ‘Ciphers arcfour256′
  48. ‘Compression yes
  49. ‘TCPKeepAlive yes
  50. ‘ServerAliveCountMax 3
  51. ‘ServerAliveInterval 10
  52. )
  53.  
  54. # /////////////////////////
  55. # // Do not modify below //
  56. # /////////////////////////
  57.  
  58. ###################
  59. ## SCRIPT CHECKS ##
  60. ###################
  61.  
  62. if [ "$1" == "install" -o "$1" == "--help" -o "$1" == "-h" ]
  63. then
  64. echo “INSTALLATION INSTRUCTIONS:”
  65. echo
  66. echo# Set the configuration variables and forwardings”
  67. echo
  68. echo# Make sure you have ssh keys generated”
  69. echossh-keygen
  70. echo
  71. echo# Setup password-less login to the remote host”
  72. echo “ssh-copy-id ‘$REMOTE_HOST -l $REMOTE_USER -p $REMOTE_SSH_PORT’”
  73. echo
  74. echo# Add the cron job”
  75. echoecho*/5 * * * * root $( cd “$( dirname${BASH_SOURCE[0]})&& pwd )/$(echo ${BASH_SOURCE[0]} | rev | cut -d’/-f1 | rev)> /etc/cron.d/rssht_$REMOTE_HOST
  76. exit 0
  77. fi
  78.  
  79. ###############################
  80. ## SSH CONNECTION EVALUATION ##
  81. ###############################
  82.  
  83. # - If socket exists:
  84. # 1-If the socket is old kill the connection (so that a new one is created)
  85. # 2-Run the check command on the socket
  86. # 2.1- if it passes, do nothing, exit
  87. # 2.2- if it fails kill ssh and remove socket file
  88. # - Run the ssh command
  89.  
  90. # Socket file
  91. SOCK_FILE=”$SSH_RUN_DIR/$REMOTE_HOST.sock”
  92. # PID file
  93. PID_FILE=”$SSH_RUN_DIR/ssh_$REMOTE_HOST.pid”
  94.  
  95. if (mkdir -p $SSH_RUN_DIR)
  96. then :
  97. else
  98. echo “FATAL Error: Cannot create RUN directory $SSH_RUN_DIR/
  99. fi
  100.  
  101. if [ -S "$SOCK_FILE" -o "$1" == "stop" ]
  102. then
  103. # If Socket is older than {REFRESH_SOCKET} minutes OR if stop argument is passed, stop the connection
  104. if [ "$REFRESH_SOCKET" -gt "0" -o "$1" == "stop" ]
  105. then
  106. if [ -n "$(find $SOCK_FILE -mmin +$REFRESH_SOCKET)" -o "$1" == "stop" ]
  107. then
  108. if [ "$1" == "stop" ]
  109. then
  110. echo “Stop argument passed, killing ..”
  111. else
  112. echo “Existing SSH connection is old, killing ..”
  113. fi
  114. # Send the exit command to the existing socket
  115. ssh -O exit -S $SOCK_FILE $REMOTE_HOST &> /dev/null
  116. # Kill the pid if the process some how still exists
  117. if (kill -0 $(cat $PID_FILE) &> /dev/null )
  118. then
  119. echo “killing ssh process $(cat $PID_FILE)
  120. kill -9 $(cat $PID_FILE) &> /dev/null
  121. fi
  122. rm -rf $PID_FILE &> /dev/null
  123. fi
  124. fi
  125. #If the user passed stop, don’t proceed further
  126. [ "$1" == "stop" ] && exit 0
  127.  
  128. # Check the status of the SSH connection through the socket file
  129. if (ssh -O check -S $SOCK_FILE $REMOTE_HOST &> /dev/null)
  130. then
  131. # SSH connection is fine
  132. echossh connection is fine, exiting”
  133. exit 0
  134. else
  135. # SSH socket check failed
  136. # Try killing the PID first
  137. if [ -e "$PID_FILE" ]
  138. then
  139. if (kill -0 $(cat $PID_FILE) &> /dev/null )
  140. then
  141. echo “killing ssh process $(cat $PID_FILE)
  142. kill -9 $(cat $PID_FILE) &> /dev/null
  143. rm -rf $PID_FILE &> /dev/null
  144. fi
  145. fi
  146. # Remove the socket if it still exists
  147. if [ -S "$SOCK_FILE" ]
  148. then
  149. if (rm -rf$SOCK_FILE&> /dev/null)
  150. then
  151. echo “FATAL ERROR: Cannot remove stalled socket file $SOCK_FILE
  152. echo “Exiting..”
  153. exit 9
  154. else
  155. echo “Stalled socket file removed”
  156. fi
  157. fi
  158. fi
  159. fi
  160.  
  161. # The socket and process should be gone by now; If not this is an exception; exit!
  162. # This should not happen
  163. if [ -S "$SOCK_FILE" ]
  164. then
  165. echo “Exception: Cannot remove socket file. SSH connection seems to be stuck”
  166. echo “Exiting”
  167. exit 11
  168. fi
  169.  
  170. ##########################
  171. ## SSH COMMAND CREATION ##
  172. ##########################
  173.  
  174. # Whether to use -g switch or not
  175. P_R_A=””
  176. [ "$PORTS_REMOTELY_ACCESSIBLE" == "yes" ] && P_R_A=”-g”
  177.  
  178. # Remote forwardings
  179. RFWDS=””
  180. for i in${!REMOTE_FWDS[@]}
  181. do
  182. RFWDS=”$RFWDS-R ${REMOTE_FWDS[$i]}
  183. done
  184.  
  185. # SSH options
  186. SOPTS=””
  187. for i in${!SSH_OPTS[@]}
  188. do
  189. SOPTS=”$SOPTS-o ‘${SSH_OPTS[$i]}’ ”
  190. done
  191.  
  192. # SSH final command
  193. SSH_COMMAND=”$SSH_BIN $SOPTS $P_R_A -f -q -N -S $SOCK_FILE $RFWDS -p $REMOTE_SSH_PORT -l $REMOTE_USER $REMOTE_HOST
  194.  
  195. #####################
  196. ## RUN SSH COMMAND ##
  197. #####################
  198.  
  199. eval$SSH_COMMAND
  200. if [ "$?" -ne "0" ]
  201. then
  202. echo “FATAL ERROR: SSH command failed”
  203. echoSSH_COMMAND=$SSH_COMMAND
  204. exit 10
  205. else
  206. #Save the PID
  207. SOCK_CHECK=$(ssh -O check -S $SOCK_FILE $REMOTE_HOST 2>&1)
  208. SPID=$(echo $SOCK_CHECK | cut -d’=’ -f2 | sed ‘s/)//)
  209. echo$SPID> $PID_FILE
  210. echo “RSSHT to host $REMOTE_USER@$REMOTE_HOST:$REMOTE_SSH_PORT started successfully ssh pid: $SPID
  211. exit 0
  212. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement