reverse_ssh.sh 689 B

12345678910111213141516171819202122232425
  1. #!/bin/sh
  2. ## A script to set up reverse ssh to server and be run with cron_key.sh
  3. # Run in crontab with:
  4. # */10 * * * * /home/demure/projects/personal/scripts/reverse_ssh.sh
  5. PORT="2001"
  6. SERVER="demu.red"
  7. CON_PORT="500"
  8. ## Set command as a var to save time
  9. SSH_COMMAND="ssh -F /dev/null -p ${CON_PORT} -f -N -R ${PORT}:localhost:22 ${SERVER} -q"
  10. ## Check how many ssh sessions are running on the port
  11. CHECK="$(pgrep -f "${SSH_COMMAND}" | wc -l)"
  12. ## If no sessions, start one; if more than one, kill them and start a new one.
  13. if [ "${CHECK}" -eq "0" ]; then
  14. ${SSH_COMMAND}
  15. else
  16. if [ "${CHECK}" -gt "1" ]; then
  17. pkill -f "${SSH_COMMAND}"
  18. ${SSH_COMMAND}
  19. fi
  20. fi